HttpSession Methods
(Continued)
isNew
Determines if session is new to client (not to page)
getCreationTime
Returns time at which session was first created
getLastAccessedTime
Returns time session was last sent from client
getMaxInactiveInterval,
setMaxInactiveInterval
Gets or sets the amount of time session should go without
access before being invalidated
invalidate
Invalidates the session and unbinds all
objects associated with it
91
Servlets
www.corewebprogramming.com
A Servlet Showing Per Client
Access Counts
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "Session Tracking Example";
HttpSession session = request.getSession(true);
String heading;
Integer accessCount =
(Integer)session.getAttribute("accessCount");
if (accessCount == null) {
accessCount = new Integer(0);
heading = "Welcome, Newcomer";
} else {
heading = "Welcome Back";
accessCount = new Integer(accessCount.intValue() + 1);
}
session.setAttribute("accessCount", accessCount);
92
Servlets
www.corewebprogramming.com
46