This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Servlets and the fly likes session keeps timing out Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "session keeps timing out" Watch "session keeps timing out" New topic
Author

session keeps timing out

david allen
Ranch Hand

Joined: Sep 27, 2002
Posts: 185
Hi all,
I am using sessions on my site to store a few objects. When the user logs on I create a session ie.
session.setAttribute("user", sessionUser);
session.setMaxInterval(3600);
In my web.xml file I have written the following
<session-config>
<session-timeout>60</session-timeout>
</session-config>
So as far as I can tell I have set the session to expire in one hour.
Could someone offer and advice as to why it keeps timing out
thank you very much
david
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
david -
you need to provide us with more information:
- this might sound dumb, but are you actually creating the session? how are you doing it?
- Are you using setMaxInactiveInterval in your code?


I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
david allen
Ranch Hand

Joined: Sep 27, 2002
Posts: 185
Here is my code for creating the session. This will be called when the user logs in.
HttpSession session = request.getSession(true);
User user = new User();
String error = "Invalid login or password";

if(user.dbInitialise((String)request.getParameter("login"))) {

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

if(password.equals(user.getPassword())) {


User sessionUser = (User)session.getAttribute("user");
if(sessionUser == null) {
sessionUser = new User(user);
session.setAttribute("user",sessionUser);
session.setMaxInactiveInterval(3600);
}
}
else{
session.setAttribute("error",error);
}
}
else {
session.setAttribute("error",error);
}
This code works fine. After the user logs in it will redirect them to the login page and display their name but when I move around the site and leave it idle for 5 min the site will time out. I then have to go back to the login page and login again.
So the session is created but from some reason it does not last one hour. I hope I have provided enough information.
Thank you very much
david
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
hmm.. the code looks ok by me..
I was just wondering, why do you use setMaxInactiveInterval? if you're already declaring in your web.xml that the session timeout is 60 minutes, why are you putting it again...
I'd also look at my others servlets/jsp pages, to see if I'm using this method with a different value.
I wouldn't use it at all, unless I want to specify a different value..
let me know ...
david allen
Ranch Hand

Joined: Sep 27, 2002
Posts: 185
So if I set it in web.xml then that should do the trick is that right?
I will try it and let you know.
Thanks for your help.
david
Scott Duncan
Ranch Hand

Joined: Nov 01, 2002
Posts: 363
just out of curiosity, what app server are you using?


No more rhymes! I mean it!<br /> <br />Does anybody want a peanut?
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
Originally posted by david allen:
So if I set it in web.xml then that should do the trick is that right?

yep, that's why the web.xml is used for. You have a single place to modify your settings and all servlets will behave based on this.
But, if you want a particular session to behave differently, you use setMaxInactiveInterval(). this method affects *only* the session on which it is called. other sessions will still be mandated by the value you specify in web.xml.
david allen
Ranch Hand

Joined: Sep 27, 2002
Posts: 185
I think I may have found the probem but please let me know.
I am working on a test machine.
When I compile a servlet I have it set up so that I dont have to turn tomcat off for the changes to be registered.
While testing my site I have been making changes and then recompiling the servelts. When a servlet is recompiled and then registered by tomcat I think that the sessions are deleted (without stopping tomcat).
I think this is the problem that I have been having.
What do you think?
Regards David
David
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
That could be the problem, but not sure about it. What the container does once it starts, is read the configuration files (including web.xml) and loading the servlets to memory (creating an instance). This is one of the steps in the life cycle of the servlet.
I don't know if tomcat allows hot deployment,but this could be a solution. What you're basically doing now is modifying the code while running the same web application. this might result in unexpected behaviour.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Yes, tomcat has Hot deployment.
And it probably does invalidate current sessions that would use that Servlet.
Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
 
Don't get me started about those stupid light bulbs.
 
subject: session keeps timing out