/* Adapted from Listings
* 19.26 `Simple example of session tracking'
* and
* 19.12 `ShowRequestHeaders.java'
* Core Web Programming Java 2 Edition
* By Marty Hall and Larry Brown
* Published by Prentice Hall and Sun Microsystems Press,
* .
*/
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.net.*;
import java.util.*;
public class ShowSession extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
HttpSession session = request.getSession(true);
Integer accessCount =
(Integer)session.getAttribute("accessCount");
String heading;
if (accessCount == null) {
accessCount = new Integer(0);
heading = "Welcome, Newcomer";
} else {
heading = "Welcome Back";
accessCount = new Integer(accessCount.intValue() + 1);
}
session.setAttribute("accessCount", accessCount);
out.println("\n" +
"\n" +
"\n");
out.println("
\n" +
" \n" +
" Session Tracking With Servlets Example " +
" for Dal CS 3172 : Web-centric Computing\n" +
" \n" +
" \n" +
" \n" +
"\n");
out.println("\n" +
" \n" +
"Example : Session Tracking Servlet \n");
out.println(" " + heading + " \n" +
" Information on Your Session: \n" +
" \n" +
" " +
" Info Type " +
" Value " +
" \n" +
" " +
" ID " +
" " + session.getId() + " " +
" \n" +
" " +
" Creation Time " +
" " + new Date(session.getCreationTime()) + " \n" +
" \n" +
" \n" +
" Time of Last Access \n" +
" " + new Date(session.getLastAccessedTime()) + " \n" +
" \n" +
" \n" +
" Number of Previous Accesses \n" +
" " + accessCount + " \n" +
" \n" +
" \n" +
" \n" +
" MaxInactiveInterval" +
" \n" +
" \n" + session.getMaxInactiveInterval() + " seconds \n" +
" \n" +
"
\n");
out.println(" [view source code ]\n" +
"
");
out.println(" \n" +
"\n" +
" \n" +
" Version: " +
" 07 February 2007 " +
" CS 3172 Prof: \n" +
" J. Blustein <jamie@cs.dal.ca> \n" +
" \n\n" +
" \n" +
" \n" +
"
\n\n" +
"\n" +
" Adapted from Listings 19.26 ShowSession.java
" +
" and \n19.12 ShowRequestHeaders.java
" +
" in Core Web Programming Java \n" +
" (2nd ed.)" +
" By Marty Hall and Larry Brown\n" +
" Published by Prentice Hall and Sun Microsystems Press.\n" +
"
\n");
out.println("\n");
}
private String getScriptURI(HttpServletRequest request)
throws ServletException {
String Header = "SCRIPT_URI";
String scriptURIheader = request.getHeader(Header);
if (null != scriptURIheader &&
!(scriptURIheader.equals("null"))) {
return scriptURIheader;
} else {
String hostname = request.getHeader("host");
String URI = request.getRequestURI();
if (null != URI && null != hostname &&
request.getProtocol().startsWith("HTTP/")) {
return "http://" + hostname + URI;
} else {
return ""; // empty string
}
}
}// getScriptURI()
/** Handle GET and POST requests identically. */
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}// doPost()
}