/* * @(#)Killer.java * * Copyright (c) 1998 Karl Moss. All Rights Reserved. * * You may study, use, modify, and distribute this software for any * purpose provided that this copyright notice appears in all copies. * * This software is provided WITHOUT WARRANTY either expressed or * implied. * * @author Karl Moss * @version 1.0 * @date 20Jan99 * */ package javaservlets.session; import javax.servlet.*; import javax.servlet.http.*; /** *

This servlet gathers all of the information about all of * the sessions and returns a formatted table as part of a * form. The user can then kill any of the sessions by * clicking a checkbox. This servlet on functions * prior to version 2.1 of the Servlet API */ public class Killer extends HttpServlet { /** *

Performs the HTTP GET operation * * @param req The request from the client * @param resp The response from the servlet */ public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, java.io.IOException { // Requesting more informatin about a particular // session? String info = req.getParameter("info"); if (info != null) { getInfo(info, req, resp); return; } // Set the content type of the response resp.setContentType("text/html"); // Force the browser not to cache resp.setHeader("Expires", "Tues, 01 Jan 1980 00:00:00 GMT"); // Get the PrintWriter to write the response java.io.PrintWriter out = resp.getWriter(); // Write the page header out.println(""); out.println(""); out.println("Servlet Session Killer"); out.println(""); out.println(""); out.println("This page lists all of the current session "); out.println("information. Check the session and press "); out.println("'Kill' to remove the session. WARNING - "); out.println("Killing an active session my cause problems "); out.println("for some clients.
"); // If the user presses 'kill' send the request to ourselves out.println("

"); out.println("
"); out.println("" + ""); // Get the HttpSessionContext object which holds // all of the session data HttpSession session = req.getSession(true); HttpSessionContext context = session.getSessionContext(); // Get the session IDs to kill String toKill[] = req.getParameterValues("id"); if (toKill != null) { // Loop through and kill them for (int i = 0; i < toKill.length; i++) { HttpSession curSession = context.getSession(toKill[i]); // Invalidate the session if (curSession != null) { getServletContext().log("Killing session " + curSession.getId()); curSession.invalidate(); } } } // Enumerate through the list of sessions java.util.Enumeration enum = context.getIds(); while (enum.hasMoreElements()) { String sessionID = (String) enum.nextElement(); // Format the table entry out.println(""); out.println(""); // Get the last time accessed String time = ""; HttpSession curSession = context.getSession(sessionID); if (curSession != null) { long last = curSession.getLastAccessedTime(); time = (new java.util.Date(last)).toString(); } out.println(""); } out.println("
KillSession IDLast Accessed
" + sessionID + "" + time + "

"); out.println(""); out.println("
"); // Wrap up out.println(""); out.println(""); out.flush(); } /** *

Displays a page with detailed session info * @param id The session id * @param req The request from the client * @param resp The response from the servlet */ public void getInfo(String id, HttpServletRequest req, HttpServletResponse resp) throws ServletException, java.io.IOException { // Set the content type of the response resp.setContentType("text/html"); // Force the browser not to cache resp.setHeader("Expires", "Tues, 01 Jan 1980 00:00:00 GMT"); // Get the PrintWriter to write the response java.io.PrintWriter out = resp.getWriter(); // Write the page header out.println(""); out.println(""); out.println("Servlet Session Information"); out.println(""); out.println(""); // Get the HttpSessionContext object which holds // all of the session data HttpSession session = req.getSession(true); HttpSessionContext context = session.getSessionContext(); // Attempt to find the session HttpSession curSession = context.getSession(id); if (curSession == null) { out.println("Session " + id + " not found"); } else { out.println("

Information for session " + id + "

"); // Display a table with all of the info out.println(""); // Creation time long creationTime = curSession.getCreationTime(); out.println(""); // Last accessed time long lastTime = curSession.getLastAccessedTime(); out.println(""); out.println("
Creation Time" + (new java.util.Date(creationTime)) + "
Last Access Time" + (new java.util.Date(lastTime)) + "
"); // Get an array of value names String names[] = curSession.getValueNames(); if ((names != null) && (names.length > 0)) { out.println("

Bound objects

"); // Display a table with all of the bound values out.println(""); for (int i = 0; i < names.length; i++) { out.println(""); } out.println("
" + names[i] + "" + curSession.getValue(names[i]) + "
"); } out.println("
"); } // Wrap up out.println(""); out.println(""); out.flush(); } /** *

Initialize the servlet. This is called once when the * servlet is loaded. It is guaranteed to complete before any * requests are made to the servlet * * @param cfg Servlet configuration information */ public void init(ServletConfig cfg) throws ServletException { super.init(cfg); } /** *

Destroy the servlet. This is called once when the servlet * is unloaded. */ public void destroy() { super.destroy(); } }