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("");
out.print("Session Tester Servlet");
out.print("");
out.print("Session Tester Servlet
");
out.print("
");
out.print("Session Status: ");
if(session == null)
{
out.print("does not exist
");
}
else
{
try
{
if(session.isNew())
{
out.print("is new");
}
else if(_request.isRequestedSessionIdValid())
{
out.print("is valid");
}
else
{
out.print("is invalid");
}
out.print("
Session Id: ");
out.print(session.getId());
out.print("
Creation Time: ");
out.print(new Date(session.getCreationTime()).toString());
out.print("
Last Accessed Time: ");
out.print(new Date(session.getLastAccessedTime()).toString());
out.print("
Maximum Inactive Interval: ");
out.print(session.getMaxInactiveInterval());
out.print(" seconds");
out.print("
Session From: ");
if(_request.isRequestedSessionIdFromURL())
{
out.print("URL");
}
else if(_request.isRequestedSessionIdFromCookie())
{
out.print("Cookie");
}
else
{
out.print("Other");
}
out.print("
Session values:
");
String[] value_names = session.getValueNames();
if(value_names.length == 0)
{
out.print("None
");
}
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("
");
}
}
}
catch(Exception ex)
{
out.print("is invalid
");
}
}
out.print("
");
out.close();
}
}