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("");
out.println("Session Tester Servlet");
out.println("");
out.println("Session Tester Servlet
");
out.println("
");
out.print("Session Status: ");
if(session == null)
{
out.println("does not exist
");
}
else
{
try
{
if(session.isNew())
{
out.println("is new
");
}
else if(_request.isRequestedSessionIdValid())
{
out.println("is valid
");
}
else
{
out.println("is invalid
");
}
out.println("Session Id: " + session.getId() + "
");
out.println("Creation Time: " + new Date(session.getCreationTime()).toString() + "
");
out.println("Last Accessed Time: " + new Date(session.getLastAccessedTime()).toString() + "
");
out.println("Maximum Inactive Interval: " + session.getMaxInactiveInterval() + " seconds
");
out.print("Session From: ");
if(_request.isRequestedSessionIdFromURL())
{
out.println("URL
");
}
else if(_request.isRequestedSessionIdFromCookie())
{
out.println("Cookie
");
}
else
{
out.println("Other
");
}
out.println("
");
out.println("Session values:
");
int count = 0;
Enumeration e = session.getAttributeNames();
String name;
while(e.hasMoreElements())
{
count++;
name = (String)e.nextElement();
out.println(name + " = " + session.getAttribute(name) + "
");
}
if(count == 0)
{
out.println("None");
}
}
catch(Exception ex)
{
out.println("is invalid
");
}
}
out.println("
");
String url = _response.encodeURL("./sessiontester.html");
out.println("");
out.println("");
out.println("");
out.close();
}
public String getServletInfo()
{
return "SessionTesterServlet";
}
}