ServletContextTesterServlet.java
/*
* @(#)ServletContextTesterServlet.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.File;
import java.io.IOException;
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.ServletContext;
import javax.servlet.ServletConfig;
public class ServletContextTesterServlet extends HttpServlet
{
public void init(ServletConfig _config) throws ServletException
{
super.init(_config);
ServletContext servlet_context = getServletContext();
servlet_context.setAttribute("init_key1", "init_value1");
servlet_context.setAttribute("init_key2", "init_value2");
}
public void service(HttpServletRequest _request, HttpServletResponse _response) throws ServletException, IOException
{
ServletContext servlet_context = getServletContext();
String key;
String value;
String action = _request.getParameter("action");
if(action != null)
{
if(action.equalsIgnoreCase("set value"))
{
key = _request.getParameter("key");
if(key == null)
{
key = "";
}
value = _request.getParameter("value");
if(value == null)
{
value = "";
}
if(key.length() > 0)
{
servlet_context.setAttribute(key, value);
}
}
else if(action.equalsIgnoreCase("remove value"))
{
key = _request.getParameter("key");
if(key == null)
{
key = "";
}
servlet_context.removeAttribute(key);
}
else if(action.equalsIgnoreCase("dump servlet context"))
{
String url = _response.encodeRedirectURL("./dumpservletcontext.html");
_response.sendRedirect(url);
return;
}
}
_response.setContentType("text/html");
ServletOutputStream out = _response.getOutputStream();
out.println("");
out.println("Servlet Context Tester Servlet");
out.println("");
out.println("Servlet Context Tester Servlet
");
out.println("
");
DumpServletContextServlet.dump_servlet_context(out, servlet_context);
out.println("
");
String url = _response.encodeURL("./servletcontexttester.html");
out.println("");
out.println("");
out.println("");
out.close();
}
public String getServletInfo()
{
return "ServletContextTesterServlet";
}
}
==================================================
DumpServletContextServlet.java
/*
* @(#)DumpServletContextServlet.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.File;
import java.io.IOException;
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.ServletContext;
public class DumpServletContextServlet extends HttpServlet
{
public void service(HttpServletRequest _request, HttpServletResponse _response) throws ServletException, IOException
{
ServletContext servlet_context = getServletContext();
String key;
String value;
_response.setContentType("text/html");
ServletOutputStream out = _response.getOutputStream();
out.println("");
out.println("Dump Servlet Context Servlet");
out.println("");
out.println("Dump Servlet Context Servlet
");
out.println("
");
out.println("Servlet Name: " + getServletName() + "
");
out.println("Servlet Context Path: " + _request.getContextPath());
dump_servlet_context(out, servlet_context);
out.println("
");
out.println("Servlet Context Tester");
out.println("");
out.println("");
out.close();
}
public String getServletInfo()
{
return "DumpServletContextServlet";
}
public static void dump_servlet_context(ServletOutputStream _out, ServletContext _servlet_context) throws IOException
{
_out.println("Parameters:
");
String key;
String value;
Enumeration e = _servlet_context.getInitParameterNames();
while(e.hasMoreElements())
{
key = (String)e.nextElement();
value = _servlet_context.getInitParameter(key);
_out.println(key + " = [" + value + "]
");
}
_out.println("
");
_out.println("Attributes:
");
Object obj;
e = _servlet_context.getAttributeNames();
while(e.hasMoreElements())
{
key = (String)e.nextElement();
obj = _servlet_context.getAttribute(key);
if(obj instanceof File)
{
value = ((File)obj).getCanonicalPath();
}
else if(obj instanceof String)
{
value = (String)obj;
}
else
{
value = obj.toString();
}
_out.println(key + " = [" + value + "]
");
}
}
}