SessionTesterServlet.java /* * Copyright (c) 1998-2005 Servertec. All Rights Reserved. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * THIS NOTICE MUST NOT BE ALTERED NOR REMOVED. * * CopyrightVersion 1.0 */ import java.util.Enumeration; import java.io.IOException; import java.util.Date; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.ServletOutputStream; import javax.servlet.ServletException; import javax.servlet.http.HttpSession; public final class SessionTesterServlet extends HttpServlet { public final void service(HttpServletRequest _request, HttpServletResponse _response) throws ServletException, IOException { HttpSession session = _request.getSession(false); String action = _request.getParameter("action"); if(action != null) { if(action.equalsIgnoreCase("create session")) { session = _request.getSession(true); } else if(action.equalsIgnoreCase("remove session")) { if(session != null) { try { session.invalidate(); } catch(Exception ex) { // ignore } session = null; } } else if(action.equalsIgnoreCase("set value")) { String key = _request.getParameter("key"); if(key == null) { key = ""; } String value = _request.getParameter("value"); if(value == null) { value = ""; } if(key.length() > 0) { if(session != null) { try { session.putValue(key, value); } catch(Exception ex) { // ignore } } } } else if(action.equalsIgnoreCase("remove value")) { String key = _request.getParameter("key"); if(key == null) { key = ""; } try { session.removeValue(key); } catch(Exception ex) { // ignore } } else if(action.equalsIgnoreCase("redirect test")) { _response.sendRedirect(_response.encodeRedirectURL(_response.encodeURL("./sessionredirecttester.html"))); return; } } _response.setContentType("text/html"); ServletOutputStream out = _response.getOutputStream(); out.print("<html><head><title>"); out.print("Session Tester Servlet"); out.print("</title></head><body>"); out.print("<h1>Session Tester Servlet</h1>"); out.print("<hr>"); out.print("Session Status: "); if(session == null) { out.print("does not exist<br>"); } else { try { if(session.isNew()) { out.print("is new"); } else if(_request.isRequestedSessionIdValid()) { out.print("is valid"); } else { out.print("is invalid"); } out.print("<br>Session Id: "); out.print(session.getId()); out.print("<br>Creation Time: "); out.print(new Date(session.getCreationTime()).toString()); out.print("<br>Last Accessed Time: "); out.print(new Date(session.getLastAccessedTime()).toString()); out.print("<br>Maximum Inactive Interval: "); out.print(session.getMaxInactiveInterval()); out.print(" seconds"); out.print("<br>Session From: "); if(_request.isRequestedSessionIdFromURL()) { out.print("URL"); } else if(_request.isRequestedSessionIdFromCookie()) { out.print("Cookie"); } else { out.print("Other"); } out.print("<br><br>Session values:<br>"); String[] value_names = session.getValueNames(); if(value_names.length == 0) { out.print("None<br>"); } else { int length = value_names.length; for(int i = 0; i < length; i++) { out.print(value_names[i]); out.print(" = "); out.print((String)session.getValue(value_names[i])); out.print("<br>"); } } } catch(Exception ex) { out.print("is invalid<br>"); } } out.print("<br><form action=\""); out.print(_response.encodeURL("./sessiontester.html")); out.print("\" method=\"post\">"); out.print("Key: "); out.print("<input type=\"text\" name=\"key\" value=\"\">"); out.print("<br>Value: "); out.print("<input type=\"text\" name=\"value\" value=\"\">"); out.print("<p><input type=\"submit\" name = \"action\" value=\"Set Value\">"); out.print("<input type=\"submit\" name = \"action\" value=\"Remove Value\">"); out.print("<input type=\"submit\" name = \"action\" value=\"Create Session\">"); out.print("<input type=\"submit\" name = \"action\" value=\"Remove Session\">"); out.print("<input type=\"submit\" name = \"action\" value=\"Redirect Test\">"); out.print("</form></body></html>"); out.close(); } }