J. Blustein
Source Code
/* Adapted from Figure 19.26 `Simple example of session tracking'
* Core Web Programming Java 2 Edition
* By Marty Hall and Larry Brown
* Published by Prentice Hall and Sun Microsystems Press,
* <URL:http://www.corewebprogramming.com/>.
*/
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("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"" +
" \"DTD/xhtml1-strict.dtd\">\n" +
"<html xmlns=\"http://www.w3.org/1999/xhtml\" " +
"xml:lang=\"en\" lang=\"en\">\n");
out.println("<head>\n" +
" <title>\n" +
" Session Tracking With Servlets Example " +
" for Dal CS 3172 : Web-centric Computing\n" +
" </title>\n" +
" <meta name='ROBOTS' content='NOINDEX' />\n" +
" <meta name='ROBOTS' content='NOFOLLOW' />\n" +
" <link rev='Made' href='mailto:jamie@cs.dal.ca' />\n" +
" <link rel='stylesheet' type='text/css'\n" +
" href='/~jamie/course/CS/3172/CS3172.css' />\n" +
"</head>\n");
out.println("<body>\n" +
"<h1><a href='/~jamie/course/CS/3172/' \n" +
" title='course homepage' \n" +
" >Web-centric Computing</a></h1> \n" +
"<h2><a href='/~jamie/course/CS/3172/examples/list.html'\n" +
" >Example</a>: Session Tracking Servlet</h2> \n");
out.println(" <h3>" + heading + "</h3>\n" +
" <h4>Information on Your Session:</h4>\n" +
" <table border='1'>\n" +
" <tr>" +
" <th>Info Type</th> " +
" <th>Value</th> " +
" </tr>\n" +
" <tr>" +
" <td>ID</td> " +
" <td>" + session.getId() + "</td>" +
" </tr>\n" +
" <tr>" +
" <td>Creation Time</td>" +
" <td>" + new Date(session.getCreationTime()) + "</td>\n" +
" </tr>\n" +
" <tr>\n" +
" <td>Time of Last Access</td>\n" +
" <td>" + new Date(session.getLastAccessedTime()) + "</td>\n" +
" </tr>\n" +
" <tr>\n" +
" <td>Number of Previous Accesses</td>\n" +
" <td>" + accessCount + "</td>\n" +
" </tr>\n" +
" <tr>\n" +
" <td>\n" +
" MaxInactiveInterval" +
" </td>\n" +
" <td>\n" + session.getMaxInactiveInterval() + " seconds </td>\n" +
" </tr>\n" +
" </table>\n");
out.println(" <p>[<a\n" +
" href='/~jamie/course/CS/3172/examples/servlets/SessionTrack/source.html'\n"+
" >view source code</a>]\n" +
" </p>");
out.println("<hr title='end of document proper' />\n" +
"<h6 title='document identification information'\n" +
"><kbd>\n" +
"<a href='" +
response.encodeURL(
"http://borg.cs.dal.ca/~jamie/servlet/ShowSession"
) + "'\n" +
" title='reload this webpage'\n" +
" >" +
response.encodeURL("http://borg.cs.dal.ca/~jamie/servlet/ShowSession") +
"</a>\n" +
"</kbd>\n" +
"</h6>\n" +
" <dl style='display : compact'>\n" +
" <dt>Version:</dt>" +
" <dd>30 Oct 2001</dd>" +
" <dt><a href='/~jamie/course/CS/3172/' \n" +
" title='course homepage' \n" +
" >CS 3172</a> Prof:</dt> \n" +
" <dd><a title=\"prof's homepage\" \n" +
" href='/~jamie/' \n" +
" >J. Blustein</a> <kbd><jamie@cs.dal.ca></kbd></dd> \n" +
" </dl> \n\n" +
"<p> \n" +
" Adapted from Figure 19.26 `Simple example of session\n" +
" tracking' in <cite>Core Web Programming Java</cite>\n" +
" (2nd ed.)" +
" By Marty Hall and Larry Brown\n" +
" Published by Prentice Hall and Sun Microsystems Press.\n" +
"<a href='http://validator.w3.org/check/referer'><img \n" +
"src='/~jamie/image/vxhtml10.gif' \n" +
"alt='Valid XHTML 1.0!' height='31' width='88' /></a> \n" +
"</p> \n");
out.println("</body>\n</html>");
}
/** Handle GET and POST requests identically. */
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
- Version:
- 30 Oct 2001
- CS 3172 Prof:
- J. Blustein <jamie@cs.dal.ca>