J2ee Session Tracking - java

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

Related

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. :·)

Retaining session variables on page redirect

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.

How to pass a parameter to previous page?

I am working on a JSP project. I have a page that calls another JSP.
Now the problem is, how to pass or use a variable in the called JSP page in its calling page?
You can save the variable in the HTTPSession or ServletContext object.
And in the calling JSP page, use or check session attribute for the variable.
session.setAttribute(objectId, Object); to set the variable.
session.getAttribute(objectId); to get the variable.
I have a page that calls another jsp
page
The problem lies here in this sentence.
Try to follow MVC. Use JSP just for rendering the View, and Servlet as a Controller.
Here, simple.souther.us, you find simple and awesome tutorials for newbies.
Your design should be
take one input param on page1.jsp
post it to some servlet , process it there, forward request to page1 jsp and pass param taken from page 1 as attribute
See Also
why-business-logic-should-be-moved-out-of-jsp ?

How to set a value in JSP session using javascript?

Is it possible to set/assign value in session using java script?
Basically, I have few links on my JSP page and on click of those links, I need to set a variable in session.
session is server side concept, while javascript plays on browser
You can either use ajax on click of those link make a request to server and pass the info. and set it to session # server
Or
If those links on whichuser is going to click is your server's then pass some param with it. and track it at server
The easiest approach is to use a hidden input element bound to a bean property in session scope, and put a JavaScript onclick handler in a commandlink that sets the hidden element's value before submitting the page.

C:url not appending jsessionid when cookies are disabled

I tried:
<c:url value="/web/pclub/userprofile" var="test">
<c:param name="userid" value="${user.id}"/>
</c:url>
${test}
But when I check the page with cookies disabled then the jsessionid is not appended.
Does anyone know why this happens?
The jsessionid is only appended when the cookies are disabled and when a session has been created on the server side (and thus the server side needs to notify the client about this somehow, for which a cookie would is the default approach). If no session has been created, then there's no point of appending the jsessionid.
Try adding the following line to top of your JSP, at least before the <c:url> line is called. It not only prints/debugs the session ID for you, but it also implicitly creates the session if not done yet.
${pageContext.session.id}
Do this for testing purposes only. If it works, then the cause of your problem is that there was just no means of a session. Just keep it as is. There's no need to unnecessarily create the session. For the case whenever there's as session and the browser doesn't support cookies, the c:url will work fine.
By the way, to verify if the cookies are indeed disabled, track the request headers by Firebug. If there's no Cookie header in the request even though the server has set a Set-Cookie header in the response, then it means that the cookies are indeed disabled in the client side.
I think BalusC is right, however to force creating a session on the server, you can add a directive page on the top of your JSP, like this:
<%# page session="true" %>

Categories