Novocode
Software & Networking
Previous Page Contents
 NetForge 1.0 Manual:   NFSessionDemoServlet.java 

import java.io.*;
import java.util.Enumeration;
import java.util.Date;
import java.util.TimeZone;
import java.util.SimpleTimeZone;
import java.text.SimpleDateFormat;

import javax.servlet.*;
import javax.servlet.http.*;


/**
 * NetForge Session Demo Servlet, demonstrates how to use Sessions.
 *
 * This source is in the public domain. Do with it whatever you want and
 * don't blame me if it doesn't work :-)
 *
 * @author Stefan Zeiger
 */

public final class NFSessionDemoServlet extends HttpServlet
{
  public void doGet(HttpServletRequest req, HttpServletResponse res)
         throws ServletException, IOException
  {
    HttpSession session = req.getSession(false);
    if(session == null) sendPage(req, res, null, true);
    else sendPage(req, res, session, false);
  }


  public void doPost(HttpServletRequest req, HttpServletResponse res)
         throws ServletException, IOException
  {
    if(req.getParameter("login") != null)
    {
      HttpSession session = req.getSession(true);
      String name = req.getParameter("name");
      if((name == null) || (name.length() == 0)) name = "Mr. Anonymous";
      session.putValue("name", name);
      session.putValue("counter", new int[] { 1 });
      sendPage(req, res, session, false);
    }
    else
    {
      HttpSession session = req.getSession(false);
      if(session != null) session.invalidate();
      sendPage(req, res, null, true);
    }
  }


  private void sendPage(HttpServletRequest req, HttpServletResponse res,
                        HttpSession session, boolean login)
          throws ServletException, IOException
  {
    res.setContentType("text/html");
    PrintWriter o = res.getWriter();
    o.print("<HTML><HEAD><TITLE>" + getServletInfo() + "</TITLE></HEAD>"+
            "<BODY BGCOLOR=\"#B2B2B2\"><H1>" + getServletInfo() +
            "</H1><HR><P>");

    if(login)
      o.print("<FORM METHOD=POST>Please enter your name: "+
                "<INPUT TYPE=TEXT NAME=\"name\">"+
                "<INPUT TYPE=SUBMIT NAME=\"login\" VALUE=\"Log in\"></FORM>");
    else
    {
      o.print("Hi <B>" + session.getValue("name") +
              "</B>!<P>This is your request #<B>" +
              (((int[])session.getValue("counter"))[0]++) +
              "</B> in this session. Click <A HREF=\"" +
              res.encodeUrl(req.getServletPath()) +
              "\">here</A> to continue the session.<P><H3>Session Data:" +
              "</H3>Session ID: " + session.getId() + "<BR>Session Type: " +
              (req.isRequestedSessionIdFromCookie()? "Cookie":
               (req.isRequestedSessionIdFromUrl()? "URL":"Unknown")) + "<BR>" +
              "Session is new: " + (session.isNew()? "Yes":"No") + "<BR>" +
              "Creation Time: " +
              toDateString(session.getCreationTime()) + "<BR>" +
              "Last Accessed Time: " +
              toDateString(session.getLastAccessedTime()));

      o.print("<H3>Valid Sessions:</H3>");
      Enumeration ids = session.getSessionContext().getIds();
      while(ids.hasMoreElements()) o.print(ids.nextElement() + "<br>");

      o.print("<P><FORM METHOD=POST><INPUT TYPE=SUBMIT NAME=\"logout\" "+
              "VALUE=\"Log out\"></FORM>");
    }

    o.print("</BODY></HTML>");
    o.close();
  }


  private String toDateString(long millis)
  {
    SimpleDateFormat format =
      new SimpleDateFormat ("d MMM yyyy H:mm:ss 'GMT'");
    format.setTimeZone(new SimpleTimeZone(0, "GMT"));
    return format.format(new Date(millis));
  }


  public String getServletInfo() { return "NetForge Session Demo Servlet"; }
}


Last modified Feb/17/1999 by Stefan Zeiger