开发者

Using Post when doing a sendRedirect

开发者 https://www.devze.com 2022-12-13 15:48 出处:网络
I have a requirement that a jsf page needs to be launched from a custom thin client in user browser (thin client does a http post). Since the jsf page may be invoked multiple t开发者_运维技巧imes, I u

I have a requirement that a jsf page needs to be launched from a custom thin client in user browser (thin client does a http post). Since the jsf page may be invoked multiple t开发者_运维技巧imes, I use a jsp to redirect to the jsf page with params on url. In jsp I do a session invalidation. The jsp code is below:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@page session="true" %>

<%
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Pragma", "no-cache");
session.invalidate();

String outcome = request.getParameter("outcome");


String queryString = "outcome=" + outcome ;

response.sendRedirect("./faces/MyPage.jspx?"  + queryString);


%>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"></meta>
    <title>title here</title>
  </head>
  <body></body>
</html>

My jsf page uses mutiple drop down items with autoSubmit enabled. I set the params on session when the form first inits and then use it from there when an action button is ultimately clicked by user. Following is the code I use to get the param in jsf backing bean constructor:

FacesContext ctx = getFacesContext();
Map sessionState = ctx.getExternalContext().getSessionMap();
outcome = (String)sessionState.get("outcome");
if(outcome == null)  //if not on session, get from url
    outcome = (String)JSFUtils.getManagedBeanValue("param.outcome");

This way I can get the param even after multiple autoSubmits of drop downs.

My problem is that I cannot have parameters show up on browser address bar. I need a way so that the parameters can be passed to the jsp page. I cannot post direct to jsp from thin client since I need the jsf page to have a new session each time the client launches user browser. This is imp due to the above code snippet on how I use params in the jsf page.

Is there nay way to use a post when doing a sendRedirect so that I do not have to pass params on url? I cannot use forward since when an autoSubmit fires on jsf page, it causes the browser to refresh the jsp page instead.

Is there any better way to handle the jsf page itself so that I don't have to rely on storing params on session between successive autoSubmit events?


Since you invalidate the session, no, you cannot.

The only way would be putting it in the session scope. Why are you by the way invalidating the session on first request? This makes really no sense.

Unrelated to your actual problem, doing sendRedirect() in a scriptlet instead of a Filter and having a bunch of HTML in the same JSP page is receipt for big trouble. Do not write raw Java code in JSP files, you don't want to have that. Java code belongs in Java classes. Use taglibs/EL in JSP only.


No. Because sendRedirect send the web-browser/client a 302 with the new location and according to the following it will usually be a GET, no matter what the original request was.

According to HTTP/1.1 RFC 2616 http://www.ietf.org/rfc/rfc2616.txt:

If the 302 status code is received in response to a request other
than GET or HEAD, **the user agent MUST NOT automatically redirect the
request unless it can be confirmed by the user**, since this might
change the conditions under which the request was issued.

Note: RFC 1945 and RFC 2068 specify that the client is not allowed
to change the method on the redirected request.  However, ***most
existing user agent implementations treat 302 as if it were a 303
response, performing a GET on the Location field-value regardless
of the original request method***. The status codes 303 and 307 have
been added for servers that wish to make unambiguously clear which
kind of reaction is expected of the client.

Maybe playing carefully with ajax can get you something.

Update: Then again, as user: BalusC pointed out, you can redirect by manually setting the headers, as in:

response.setStatus(307);
response.setHeader("Location", "http://google.com");
0

精彩评论

暂无评论...
验证码 换一张
取 消