SessionTesterServlet.java /* * @(#)SessionTesterServlet.java * * Copyright (c) 1998-2000 Servertec. All Rights Reserved. * * This software is the proprietary and confidential property of Servertec. * Use only in accordance with the terms of the license agreement. * * SERVERTEC 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. SERVERTEC SHALL NOT BE LIABLE FOR ANY * DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. * * 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 class SessionTesterServlet extends HttpServlet { public 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.setAttribute(key, value); } catch(Exception ex) { // ignore } } } } else if(action.equalsIgnoreCase("remove value")) { String key = _request.getParameter("key"); if(key == null) { key = ""; } try { session.removeAttribute(key); } catch(Exception ex) { // ignore } } else if(action.equalsIgnoreCase("redirect test")) { String url = _response.encodeRedirectURL("./hello.html"); _response.sendRedirect(url); return; } } _response.setContentType("text/html"); ServletOutputStream out = _response.getOutputStream(); out.println("<html>"); out.println("<head><title>Session Tester Servlet</title></head>"); out.println("<body>"); out.println("<h1>Session Tester Servlet</h1>"); out.println("<hr>"); out.print("Session Status: "); if(session == null) { out.println("does not exist<br>"); } else { try { if(session.isNew()) { out.println("is new<br>"); } else if(_request.isRequestedSessionIdValid()) { out.println("is valid<br>"); } else { out.println("is invalid<br>"); } out.println("Session Id: " + session.getId() + "<br>"); out.println("Creation Time: " + new Date(session.getCreationTime()).toString() + "<br>"); out.println("Last Accessed Time: " + new Date(session.getLastAccessedTime()).toString() + "<br>"); out.println("Maximum Inactive Interval: " + session.getMaxInactiveInterval() + " seconds<br>"); out.print("Session From: "); if(_request.isRequestedSessionIdFromURL()) { out.println("URL<br>"); } else if(_request.isRequestedSessionIdFromCookie()) { out.println("Cookie<br>"); } else { out.println("Other<br>"); } out.println("<hr>"); out.println("Session values:<br>"); int count = 0; Enumeration e = session.getAttributeNames(); String name; while(e.hasMoreElements()) { count++; name = (String)e.nextElement(); out.println(name + " = " + session.getAttribute(name) + "<br>"); } if(count == 0) { out.println("None"); } } catch(Exception ex) { out.println("is invalid<br>"); } } out.println("<hr>"); String url = _response.encodeURL("./sessiontester.html"); out.println("<form action=\"" + url + "\" method=\"post\">"); out.println("Key: "); out.println("<input type=\"text\" name=\"key\" value=\"\">"); out.println("<br>"); out.println("Value: "); out.println("<input type=\"text\" name=\"value\" value=\"\">"); out.println("<p>"); out.println("<input type=\"submit\" name = \"action\" value=\"Set Value\">"); out.println("<input type=\"submit\" name = \"action\" value=\"Remove Value\">"); out.println("<input type=\"submit\" name = \"action\" value=\"Create Session\">"); out.println("<input type=\"submit\" name = \"action\" value=\"Remove Session\">"); out.println("<input type=\"submit\" name = \"action\" value=\"Redirect Test\">"); out.println("</form>"); out.println("</body>"); out.println("</html>"); out.close(); } public String getServletInfo() { return "SessionTesterServlet"; } }