I setup my Session time out.
<session-config>
<session-timeout>11520</session-timeout>
</session-config>
Each time when I close the browser and open it again by calling the servlet, I see that new session is created. It can be seen from SessionCreated method executed in HttpSessionListener each time when browser reopened.
I’m new in tomcat/Java, but if I were working in ASP.NET environment, I would work around it be setting cookie with the same name as session name.
What is the best practice to work around it in Tomcat?
thank you in advance.
Danny.
That’s conform the specified behaviour. The session cookie doesn’t have an age, so it lives as long as the client has the webbrowser instance open or until the client hasn’t visited the website for long as specified in the
session-timeoutsetting in the server side.You basically want a cookie which lives longer than the session cookie. You can create a new long-living cookie using
CookieAPI, set its age usingCookie#setMaxAge(), add it to the HTTP response usingHttpServletResponse#addCookie(). On the subsequent HTTP requests you can determine the presence of the cookie usingHttpServletRequest#getCookies().This is by the way not Tomcat specific. You can do the same on every other servletcontainer.