Chapter 08 - Sessions

This web application shows how to receive session related events and how to implement session support if the browser has disabled cookies.
  1. SessionCounter class implements HttpSessionListener and is configured in the web.xml. TestServlet creates a session and prints out the number of sessions with the help of SessionCounter on the Tomcat console. You'll see the debug message when you click on this link for the first time. For all subsequent clicks, no new session is created, therefore no message is printed.

    To create another session, close all your browser windows (but do not shutdown the Tomcat server). Open a new browser window, go to http://localhost:8080/chapter08 and then click on this link again. This time it will create a new session.
     
  2. URLReWriterServlet shows the usage of HttpServletResponse.encodeURL(). It is used to force the servlet container to append the SESSIONID to the URL, whenever the cookies are turned off in the browsers. To turn off cookies in your browser, go to Tools -> Internet Options -> Security -> Custom Level.
    After you turn off cookies, click on the above link to URLReWriterServlet.