Starting Session, Closing Session and Session Timeout

Hey guys on my JSP pages im currently using sessions and working fine. What I have noticed though is that im not actually starting or closing my sessions on start and end page.

WHat are the actual commands to do this ?

Also If I leave a page idle for like five minutes and refresh it i get Null pointer exceptions im guessing because the session has run out.

What I would like to do when the session runs out and user refreshed page that It will take them to the original page.

Again how would I do this ?

Btw new to this so dont shoot me :P

Thanks

[591 byte] By [Lunnya] at [2007-10-2 23:59:52]
# 1

> Hey guys on my JSP pages im currently using sessions

> and working fine. What I have noticed though is that

> im not actually starting or closing my sessions on

> start and end page.

>

> WHat are the actual commands to do this ?

Not sure what you mean by starting and closing sessions. Sessions are created when you do the request.getSession() and there is no current session for the user (if one was previously created then it is returned). They are removed by the server under some circumstances (like when they time out or you invalidate them).

>

> Also If I leave a page idle for like five minutes and

> refresh it i get Null pointer exceptions im guessing

> because the session has run out.

>

> What I would like to do when the session runs out and

> user refreshed page that It will take them to the

> original page.

>

> Again how would I do this ?

The first thing you would put in your controller Servlet (or at the top of your JSP if you don't have a controller servlet) would be:

HttpSession session = request.getSession(false); //get the current session, if there is no session yet, return null

if (session == null) //forward to first page

else //do normal work

If you are doing an all JSP page, you would have to do this at the top of each page, and it might be better to use:

session = reguest.getSession(true); //create a new session if it doesn't exist yet

if (session.isNew()) //forward to first page

else //do normal work

Of course your first page wouldn't have this, and would just create a new session and use it...

This is also best used in a Filter (look up the API and tutorials). Filters are run before each request so you can write the code once and have it always executed.

Steve

> Btw new to this so dont shoot me :P

>

> Thanks

stevejlukea at 2007-7-14 16:47:07 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

By default a JSP will create a session when first accessed. The best thing to do is on the first JSP add a user object to the session. On the last JSP remove the user object from the session or invalidate the session using the session.invalidate. On all JSP (including the last) you check for the presence of the user object in the session. If it is missing you redirect the user to the first JSP.

tolmanka at 2007-7-14 16:47:08 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

Two pages

pageOne.jsp

pageTwo.jsp

I set an Session Attribute on pageOne.jsp

session.setAttribute("refresh", "false");

And on pageTwo.jsp I call it

String refresh1 = (String)session.getAttribute("refresh");

and then update it

session.setAttribute("refresh", "true");

now when I press a button I made that links back from pageTwo to pageOne.jsp

the Attribute "refresh" is succesfully brought back to false,and when user clicks submit (theres some input forms aswell on pageOne), the

String refresh1 = (String)session.getAttribute("refresh");

starts with False as it should be.

Now I thought users might now always wanna look for my custome "back" button and press the back button in windows explorer.

Problem is that when I do that and go back to pageOne and click submit to go to pageTwo Session Variable Refresh was never brought back to false.

It only works if i press the back button in the internet explorer go back to pageOne.jsp and actually refresh the page myself.

Is it the way i'm handling sessions thats giving the problem ?

I had it working before using

<%

session.invalidate();

%>

on pageOne.jsp but that didnt allow me to use,

session.setAttribute("refresh", "false"); anymore giving error

org.apache.jasper.JasperException: setAttribute: Session already invalidated

Any Ideas ?

Lunnya at 2007-7-14 16:47:08 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
**** sorry this was meant to be new thread
Lunnya at 2007-7-14 16:47:08 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

Sounds like the browser is caching the pages so when you use the back button it just retrieves the page from cache and does not call the server to re-execute the code. There are meta data tags that you can add to the JSP to stop the browser from caching the JSP. I do not have them easily available at the moment but if you search the forum you should find many examples.

tolmanka at 2007-7-14 16:47:08 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...