I did some testing to make sure that my sessions were getting timed out
correctly. It appears that it's working. However, it appears to be off by 10
seconds. Not a big deal at all, but, it's not what I expected. You can use
the following application to test it.

First you need to add the following to your web.xml, or, you can call
setMaxInactiveInterval() to do it within the application.

 <session-config>
  <session-timeout>1</session-timeout>
 </session-config>

To test it, access the servlet via a Web browser. Then, add a session
variable such as name1, value1 and then click Submit. Look at the Current
Time value. Then, bring up the clock on your computer. Attempt to Submit the
form again, at say 1 to 5 seconds after what was displayed on the Current
Time. If you do, you're session won't be expired. I found that you have to
wait around 10 seconds afterward.

Also, the Last Accessed Time, threw me off a little. That time is the time
that the last request was made. Initially, I thought it would have been the
current time in this application. Since, I just called getSession().

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class SessionServlet extends HttpServlet
{
 public void doGet(HttpServletRequest req, HttpServletResponse resp) throws
IOException
 {
  try
  {
   resp.setContentType("text/html");

   PrintWriter pw = resp.getWriter();

   HttpSession hs = req.getSession();

   if(req.getParameter("name") != null && !
req.getParameter("name").equals(""))
   {
    hs.setAttribute(req.getParameter("name"), req.getParameter("value"));
   }

   pw.println("<html><head><title>SessionServlet</title></head><body>");

   pw.println("<form action=" + resp.encodeURL("/servlet/SessionServlet") +
" method=get>");

   pw.println("<p>New Session: " + hs.isNew() + "</p>");

   pw.println("<p>Session Id: " + hs.getId() + "</p>");

   pw.println("<p>Session Creation Time: " + new Date(hs.getCreationTime())
+ "</p>");

   pw.println("<p>Session Last Accessed Time: " + new
Date(hs.getLastAccessedTime()) + "</p>");

   pw.println("<p>Current Time: " + new Date() + "</p>");

   pw.println("<p>Session Maximum Inactive Time: " +
hs.getMaxInactiveInterval() + " seconds</p>");

   pw.println("<p>Session from Cookie: " +
req.isRequestedSessionIdFromCookie() + "</p>");

   pw.println("<p>Session from URL: " + req.isRequestedSessionIdFromURL() +
"</p>");

   Enumeration e = hs.getAttributeNames();

   pw.println("<p>Session Objects:</p>");

   while(e.hasMoreElements() == true)
   {
    String name = (String)e.nextElement();

    String value = (String)hs.getAttribute(name);

    pw.println("<p>" + name + " = " + value + "</p>");
   }

   pw.println("<p>Name: <input maxLength=32 name=name size=24></p>");

   pw.println("<p>Value: <input maxLength=32 name=value size=24></p>");

   pw.println("<p><input name=submit type=submit value=Submit></p>");

   pw.println("</form>");

   pw.println("</body></html>");

   pw.close();
  }
  catch(Exception e)
  {
   System.out.println(e);
  }
 }
}


Reply via email to