home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1999 November / PCONLINE_11_99.ISO / filesbbs / OS2 / APCHSSL2.ZIP / OS2HTTPD / servlets / SessionServlet.java < prev    next >
Encoding:
Java Source  |  1998-12-12  |  3.3 KB  |  90 lines

  1. /*
  2.  * @(#)SimpleServlet.java    1.21 97/05/22
  3.  *
  4.  * Copyright (c) 1996-1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  *
  19.  * CopyrightVersion 1.0
  20.  */
  21.  
  22. import java.io.*;
  23. import java.util.Enumeration;
  24.  
  25. import javax.servlet.*;
  26. import javax.servlet.http.*;
  27.  
  28.  
  29. /**
  30.  * This is a simple example of an HTTP Servlet that uses the HttpSession
  31.  * class
  32.  *
  33.  * Note that in order to gaurentee that session response headers are
  34.  * set correctly, the session must be retrieved before any output is
  35.  * sent to the client.
  36.  */
  37. public class SessionServlet extends HttpServlet {
  38.  
  39.     public void doGet (HttpServletRequest req, HttpServletResponse res)
  40.       throws ServletException, IOException
  41.       {
  42.  
  43.       //Get the session object
  44.       HttpSession session = req.getSession(true);
  45.  
  46.       //Get the output stream
  47.       ServletOutputStream out = res.getOutputStream();
  48.  
  49.       res.setContentType("text/html");
  50.  
  51.       out.println("<HEAD><TITLE> SessionServlet Output </TITLE></HEAD><BODY>");
  52.       out.println("<h1> SessionServlet Output </h1>");
  53.  
  54.       Integer ival = (Integer) session.getValue("sessiontest.counter");
  55.       if (ival==null) ival = new Integer(1);
  56.       else ival = new Integer(ival.intValue() + 1);
  57.       session.putValue("sessiontest.counter", ival);
  58.       out.println("You have hit this page <b>" + ival + "</b> times.<p>");
  59.       out.println("Click <a href=" + res.encodeUrl("/servlet/SessionServlet") + ">here</a>");
  60.       out.println(" to ensure that session tracking is working even if cookies aren't supported.<br>");
  61.       out.println(" Note that by default URL rewriting is not enabled due to it's expensive");
  62.       out.println(" overhead.");
  63.       out.println("<p>");
  64.  
  65.       out.println("<h3>Request and Session Data:</h3>");
  66.       out.println("Session ID in Request: " + req.getRequestedSessionId());
  67.       out.println("<br>Session ID in Request from Cookie: " + req.isRequestedSessionIdFromCookie());
  68.       out.println("<br>Session ID in Request from URL: " + req.isRequestedSessionIdFromUrl());
  69.       out.println("<br>Valid Session ID: " + req.isRequestedSessionIdValid());
  70.       out.println("<h3>Session Data:</h3>");
  71.       out.println("New Session: " + session.isNew());
  72.       out.println("<br>Session ID: " + session.getId());
  73.       out.println("<br>Creation Time: " + session.getCreationTime());
  74.       out.println("<br>Last Accessed Time: " + session.getLastAccessedTime());
  75.       out.println("<h3>Session Context Data:</h3>");
  76.       HttpSessionContext context = session.getSessionContext();
  77.  
  78.       for (Enumeration e = context.getIds(); e.hasMoreElements() ;) {
  79.           out.println("Valid Session: " + (String)e.nextElement()+ "<br>");
  80.       }
  81.  
  82.       out.println("</BODY>");
  83.       out.close();
  84.       }
  85.  
  86.     public String getServletInfo() {
  87.         return "A simple servlet";
  88.     }
  89. }
  90.