SS.
SS.

Reputation: 109

How do you persist a tomcat session?

i have a JSP web page that refreshes every 1 minute. on each refresh, the session object is checked for validity. When the tomcat web server restarts, the session goes away...and when the page refreshes, it says "invalid". anyone has a solution to my problem?

Upvotes: 4

Views: 16473

Answers (2)

Guillaume
Guillaume

Reputation: 18865

Have a look at the configuration in your Tomcat config file. The documentation is at http://tomcat.apache.org/tomcat-6.0-doc/config/manager.html Look for the section on persistent managers ...

Upvotes: 4

Clinton
Clinton

Reputation: 2837

You have to make sure that ALL your objects your store in your Session are Serializable. If one of them isn't (or doesn't meet the Serializable requirements) you will lose your session on web app reload or tomcat restart.

EG: The following works fine for a Servlet:

public class MainServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException
    {
        HttpSession session = request.getSession();
        Date date = (Date) session.getAttribute("date");
        if (date == null) {
                date = new Date();
                session.setAttribute("date", date);
        }
        response.setContentType("text/plain");
        PrintWriter pw = response.getWriter();
        pw.println("New Session? " + session.isNew());
        pw.println("Date : " + date);
        pw.flush();
    }

}

Upvotes: 2

Related Questions