Retaining session variables on page redirect - java

I have a JSP page from which I'm calling another JSP page using response.sendRedirect(recordUrl2). I have some session variables from the first JSP page which get lost after the redirect. Is there anything that can be done in the web.xml configuration xml file to preserve the session variables?
I added the following in the xml, but the variables weren't maintained:
<session-descriptor>
<persistent-store-type>memory</persistent-store-type>
<sharing-enabled>true</sharing-enabled>
</session-descriptor>
I also tried response.encodeRedirectURL(recordUrl2) instead, but sessions variables weren't maintained.

Are you by any chance redirecting to a JSP hosted in a different JavaEE container or within a different WAR/deployment than the one doing the redirect? That's the only reason, short of you having code somewhere that clears the session, that the second JSP would not be able to access the session.

Related

J2ee Session Tracking

In my servlet (after login) I set the session timeout interval as 30 seconds and also note the sessionID as say X
session.setMaxInactiveInterval(30);
The servlet then forwards to a JSP page (intermediate1) which has a link to the second intermediate page (intermediate2). I don't do anything on the page for around 30 seconds (timeout interval) and then forward to the second jsp page (intermediate 2). Here I print the sessionID and it is NOT X. It is another value. How did this happen? Does the container automatically assign a session object to a JSP page if no session already exists? Kindly help.
JSP spec (for JSP 2.1/JEE6 it is found in chapter "JSP.1.10.1 The page Directive") describes that the session implicit object is on by default, so that every call to a JSP will participate in an existing session/create a new session if needed. It can be turned off as:
<%# page session="false" %>
Because of jsp implicit-object, then see one of them is 'session'.
So, jsp's implicate object always be there.
In your case, already current session object went off, but jsp api make it newly available.
Here is an interesting link I found on the web that answers my own question. FYI.
http://www.xyzws.com/jspfaq/can-i-prevent-the-creation-of-a-session-in-a-jsp-page/20

HttpSession variable null after being set in the Servlet

Here's my environment. Windows 7, Tomcat 8.x and Java 7.x
I'm writing an application, that has an entry point in the servlet, as opposed to a JSP. In this servlet I create a session, assign a variable to it, and then redirect the user to a JSP page. Here's the JAVA code:
HttpSession session = request.getSession();
logger.debug("Session Id: "+session.getId()+
"New Session? "+session.isNew()+
"Created: "+new java.util.Date(session.getCreationTime()));
session.setAttribute("implementation", sImplementation);
session.setAttribute("RequestId", sRequestId);
response.reset();
response.sendRedirect(request.getContextPath()+"/jsp/ProductSelection.jsp");
In the JSP page that I just redirected to I try to retrieve the session attribute that I just set in the Servlet and the value comes back as null, but only the first time that I use the JSP page. If I call it again by resubmitting the URL in the browser then everything works. :O :( Here's the relevant JSP code, the value of sImplementation is null the first time this JSP is hit.
<% System.out.println("Session Id: "+session.getId()+
"New Session? "+session.isNew()+
"Created: "+new java.util.Date(session.getCreationTime()));
String sImplementation = (String)session.getAttribute("implementation"); %>
Also if I make an entry point into my system to be a JSP page that does nothing but redirect the user to the Servlet everything works as expected. So the session created in the servlet is not valid until. Only when a JSP page is hit. :(
Lastly I tried using dispatcher.forward instead of response.sendRedirect and the session variable is there, however, the bootstrap framework that I'm using to render my pages do not render properly at all. :( I tried this in both Internet Exploder 11.x and Chromium 33.x
So my question is whether the behavior that I'm seeing is normal and expected or if there's something wrong here and there's a solution out there somewhere? :)
Thanks to all in advance, and let me know if anything is unclear or needs more code.
If im not wrong (I might), you should redirect to your jsp using
request.getRequestDispatcher("ProductSelection.jsp").forward(request, response);
This way it will be the same request and you will be able to get your session. By the way if you are creating sessions on your own you should disable the default session made by all jsp's
<%# page session=“false”%>
Make me know if it worked. :·)

True difference between pageContext.getSession().setAttribute() and pageContext.setAttribute()

I have been going crazy about resetting some validation error on one of my jsp pages. This is a project inherited from people I cannot reach anymore (dead or unavailable). I have a jsp page with lots of custom taglibs where further pages are added as tabs and the parent page has action buttons to open things like forms. There is a validation error and some configuration parameter being set/modified both in the tabs section and the parent page. But the interesting thing is that I can see the heavy use of pageContext.setAttribute(), session.setAttribute() and pageContext.getSession().setAttribute(). If my initial knowledge is right, pageContext is quite heavily used in servlet-based implementations. but how different things would be if I use those following three on my JSp pages to set attributes?
** Example Scenario (my problem): **
I have some attributes set in the parent page, which are also being set/modifed in the tabs page (embedded in the parent page). I want to remove them such that if there is a validation error, I will simply remove what I have in the tabs page i.e. next time the page will simply load those attributes from the parent page. Do I use pageContext.setAttribute() in the tabs page, but use pageContext.getSession().setAttribute() in the parent page?
KR,
Page scope
When we put in our JSP page, scope is available only for the JSP page that put it.
This is the default scope, so is the same to call pageContext.setAttribute("", "", PageContext.PAGE_SCOPE); same as pageContext.setAttribute("", "");
Session scope
session.setAttribute() and pageContext.getSession().setAttribute() both are same.
What you put on your session scope is available across all requests on the same user session.
Is the same to call pageContext.setAttribute("", "", PageContext.SESSION_SCOPE); same as session.setAttribute("", "");

Change of scope from session to request

I am using a session scope to store the bean,and i want to project the bean value to the jsp page when needed like this way
request.getSession().setAttribute("bean", bean);
response.sendRedirect("test.jsp");
And in the jsp i am using the below code to get the value on jsp
<% bean1 bean = (bean1) session.getAttribute("bean");
%>
<%= bean.getValue() %>
Instead of using a session scope i want to use a request scope,so can i set my attribute in my servlet in this way
request.setAttribute("bean", bean);
So how can i call it on my jsp
can i say
<% bean1 bean = (bean1) request.getAttribute("bean");
But it is showing error.Or instead of using scriplet how can i show my output using JSTL.
You're not understanding what a redirect is. A redirect is a response you send to the browser so that the browser sends another, new request to the location you redirected to. So, when you call sendRedirect("test.jsp"), the browser will send a new request to test.jsp. And obviously, all the attributes you have stored in the current request won't be available anymore.
It's impossible, without context, to say if a redirect is something you should do in this case, or if you should instead forward to the JSP. A forward is very different from a redirect, since it only transfers the responsibility of the current request and response to another component. In that case, there would be a unique request, and the JSP could find the attribute set by the servlet in the request.
The only thing I can say is that, in a properly designed MVC application, the JSP is used as a view, and there should never be a direct request to the view. Each request should go through a controller.

why use<bean:include instead of <jsp:include in struts?

why use<bean:include instead of <jsp:include in struts?
from the documentation for bean:include
Perform an internal dispatch to the specified application component (or external URL) and make the response data from that request available as a bean of type String. This tag has a function similar to that of the standard jsp:include tag, except that the response data is stored in a page scope attribute instead of being written to the output stream. If the current request is part of a session, the generated request for the include will also include the session identifier (and thus be part of the same session).
first hit on google
bean:include works almost like jsp:include except that the result is stored in the page scope. This means that your code on the current page can access the results and manipulate it. See this page.

Categories