J. Blustein

Web-centric Computing

Some Example Code

Servlet Examples: ShowSession

Source Code

/* Adapted from Listings
 *    19.26 `Simple example of session tracking'
 *    19.12 `Shows all the request headers sent on this request' (ShowRequestHeaders.java)
 * in
 *  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 4173 : Web-centric Computing\n" +
                "  </title>\n" +
                "  <link rev='Made'    href='mailto:jamie@cs.dal.ca' />\n" +
                "  <link rel='stylesheet' type='text/css'\n" +
                "        href='/~jamie/course/CS/4173/CS4173.css' />\n" +
                "</head>\n");
    out.println("<body>\n" +
                "<h1><a href='/~jamie/course/CS/4173/'           \n" +
                "       title='course homepage'                  \n" +
                "       >Web-centric Computing</a></h1>       \n" +
                "<h2><a href='/~jamie/course/CS/4173/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/4173/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(getScriptURI(request)) + "'\n" +
                "    title='reload this webpage'\n" +
                "    >" +
                getScriptURI(request) +
                "</a>\n" +
                "</kbd>\n" + 
                "</h6>\n" +
                " <dl style='float:left'>\n" +
                "  <dt>Version:</dt>" +
                "   <dd>07 February 2007</dd>" +
                "  <dt><a href='/~jamie/course/CS/4173/' \n" +
                "         title='course homepage' \n" +
                "         ><acronym>CS</acronym> 4173</a> Prof:</dt> \n" +    
                "  <dd><a title=\"prof's homepage\" \n" +
                "         href='/~jamie/' \n" +
                "         ><abbr>J.</abbr> Blustein</a> <kbd>&lt;jamie@cs.dal.ca&gt;</kbd></dd> \n" +
                " </dl> \n\n" +
                "<p style='float:right'> \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\n" +
                "<p style='clear:both'>\n" +
                " Adapted from Listings 19.26 <q><code>ShowSession.java</code></q> " +
                " and \n19.12 <q><code>ShowRequestHeaders.java</code></q>" +
                " 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" +
                "</p> \n");
    out.println("</body>\n</html>");
  }

    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()

}

http://www.cs.dal.ca/~jamie/course/CS/4173/examples/servlets/SessionTrack/source.html
Version:
07 Feb. 2007
CS 4173 Prof:
J. Blustein <jamie@cs.dal.ca>

Valid XHTML 1.0!