/* * SessionServlet.java - Edited by Sara Bouchenak 2007/10/31 */ // SessionServlet.java // edited by Jeff Schmitt // Towson University // October, 1998 /* * @(#)SessionServlet.java 1.21 97/05/22 * * Copyright (c) 1996-1997 Sun Microsystems, Inc. All Rights Reserved. * * This software is the confidential and proprietary information of Sun * Microsystems, Inc. ("Confidential Information"). You shall not * disclose such Confidential Information and shall use it only in * accordance with the terms of the license agreement you entered into * with Sun. * * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING * THIS SOFTWARE OR ITS DERIVATIVES. * * CopyrightVersion 1.0 */ import java.io.*; import java.util.Enumeration; import javax.servlet.*; import javax.servlet.http.*; /** * This is a simple example of an HTTP Servlet that uses the HttpSession * class. * * Note that in order to gaurentee that session response headers are * set correctly, the session must be retrieved before any output is * sent to the client. */ public class SessionServlet extends HttpServlet { public void doGet (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { //Get the session object HttpSession session = req.getSession(true); //Get the output stream ServletOutputStream out = res.getOutputStream(); res.setContentType("text/html"); out.println(" SessionServlet Example "); out.println("

SessionServlet Example

"); Integer ival = (Integer) session.getAttribute("sessiontest.counter"); if (ival==null) { ival = new Integer(1); } else { ival = new Integer(ival.intValue() + 1); } session.setAttribute("sessiontest.counter", ival); out.println("You have hit this page " + ival + " times.

"); out.println("

Request and Session Data:

"); out.println("New Session: " + session.isNew()); out.println("
Session ID: " + req.getRequestedSessionId()); out.println("
Valid Session ID: " + req.isRequestedSessionIdValid()); out.println("
Session creation Time: " + session.getCreationTime()); out.println("
Last Accessed Time: " + session.getLastAccessedTime()); out.println("
Session ID in Request from Cookie: " + req.isRequestedSessionIdFromCookie()); out.println("
Session ID in Request from URL: " + req.isRequestedSessionIdFromURL()); out.println("

Reload the page to ensure that session tracking is working."); out.println(""); out.close(); } public String getServletInfo() { return "A simple session servlet"; } }