File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes session timeouts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "session timeouts" Watch "session timeouts" New topic
Author

session timeouts

Alan Shiers
Ranch Hand

Joined: Sep 24, 2003
Posts: 237
Hi there,

I'm working on a web application and in my web.xml file I have the following at the bottom of the file:

<session-config>
<!-- Value is in minutes -->
<session-timeout>15</session-timeout>
</session-config>

For general purposes this is needed, however, is there some way of dictating a longer timeout period depending on a special circumstance. For instance, let's say a student is going to write an online test which may take longer than 15 minutes - perhaps the test may take an hour or more to complete. Is there some way of setting up the timeout period based on the special circumstance?

Alan
Paul Bourdeaux
Ranch Hand

Joined: May 24, 2004
Posts: 783
You can set the timeout for a specific session using.


“Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.” - Rich Cook
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
Note that session timeout is the inactive time - ANY request to the servlet that gets the session will reset the countdown.
WBB
Jignesh Patel
Ranch Hand

Joined: Nov 03, 2001
Posts: 626

Yes you can do it by using Session Listerns.
Just explore it.
Alan Shiers
Ranch Hand

Joined: Sep 24, 2003
Posts: 237
So, are you saying that I can do the following:

1. Since the default in the deployment descriptor is set for 15 minutes, when a client (a student) logs in the 15 minute countdown begins.

2. The student then decides to take a test. At this time I can override and reset the timeout period simply by calling: session.setMaxInactiveInterval(int seconds) precisely at the moment he begins the test?

Alan
[ August 06, 2005: Message edited by: Alan Shiers ]
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
You still seem to be missing the point about session timeouts. Consider the following:

The student logs in - your program puts login information in the session and sets the time out for 15 minutes.
Possible results:
a. the student does nothing for 16 minutes - then attempts to take a test - the session having timed out, the login infomation has been lost - your code must detect this and send the user back to login.
b. the student starts a test before 15 minutes expires, your code puts test info in the session and sets a new timeout - say 10 minutes. All subsequent request - response cycles reset the servlet container's countdown mechanism for this session to the new time.

- AT ANY POINT in the student's taking of the test - after an hour or after 6 hours - if there is more than a 10 minute delay from one request-response cycle to the next - the session will be timed out and the student login information lost.

Each request resets the countdown timer.
Bill
Alan Shiers
Ranch Hand

Joined: Sep 24, 2003
Posts: 237
Well, when the student begins the test before the default 15 minute timeout period and knowing the test shouldn't take any longer than an hour, then resetting the timeout, at that moment, for say 1 hour 15 minutes shouldn't present a problem should it?

The student would complete the test within the hour and 15 minutes then submit before the session times out. Upon successful submission, I thought to reset the timeout period again back to the default of 15 minutes.

Alan
sinasi susam
Ranch Hand

Joined: Jul 15, 2005
Posts: 67
Hi,
i am having a similar problem of question.In fact i am not sure if i am having a problem too.

My contradiction is,

I am running a servlet(or jsp) for a long timed database query
(for examle for a report of a month,or year.It takes about 8 hours.).

I wonder whether session will die or not 8 hours later?

Default time out value is 30 mins passed in conf/web.xml.
Paul Bourdeaux
Ranch Hand

Joined: May 24, 2004
Posts: 783
The student would complete the test within the hour and 15 minutes then submit before the session times out. Upon successful submission, I thought to reset the timeout period again back to the default of 15 minutes.

Sounds like you've got it to me. You also may want to write code to reset the session timeout in the case of the student not finishing the test or having to quit the test.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
I am running a servlet(or jsp) for a long timed database query
(for examle for a report of a month,or year.It takes about 8 hours.).

You should not be thinking in terms of doing all this in a single request-response cycle. The first request should start the query in a separate Thread or even a separate application. It will be up to you to create some mechanism that will let the user come back later and recover the requested report. (Possibly a unique job number stored as a Cookie?)
Bill
stan ioffe
Greenhorn

Joined: Jun 30, 2005
Posts: 16
Depending on your application and how much you care about each answered question in the test, it might make sense to keep saving data for each completed question. This way, if the session times out, you'll at least have some of the answered questions.

Another benefit is that you don't have to keep too much information in the session object ... especially if there are a lot of Strings or other immutable objects that can easily drain JVM's memory.

-stan
sinasi susam
Ranch Hand

Joined: Jul 15, 2005
Posts: 67
I am still not sure.
But process is still active in servlet's service method?So it will not timed out,will it.

How does the time out mechanism work?
Is it something setted when requested new sevlet or jsp?
lexander Bosco
Ranch Hand

Joined: Feb 21, 2005
Posts: 65
well
why dont you just check for the session at every request
or better use a session,session listener and a flag for the particular user
to check if his/her session is almost over.
if it is, you reset it to something you like.
[ August 09, 2005: Message edited by: lexander Bosco ]

there is no knowledge that is not power<br />-<br />SCJP 1.4<br />SCWCD in progress<br />SCMAD in progress
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
I am still not sure.
But process is still active in servlet's service method? So it will not timed out,will it.

You appear to continue to be thinking of a single request that starts a multiple hour process using the request Thread.
The whole web server / servlet architecture is designed around a request-response cycle that occurs fairly rapidly. Don't try to turn it into something it is not designed for.
As I explained before, a new session gets the default timeout - which can be changed by the programmer. The servlet container is responsible for occasionally glancing at the sessions it holds (may be thousands of them) and destroying those that have not had activity within the timeout period.

Lexander -
please read this FAQ entry.
Bill
lexander Bosco
Ranch Hand

Joined: Feb 21, 2005
Posts: 65
thanks will
i will correct myself henceforth

thanks
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: session timeouts