ServletContextTesterServlet.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.File;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.ServletContext;
import javax.servlet.ServletConfig;
import stec.iws.Request;
import stec.iws.Response;
public class ServletContextTesterServlet extends BaseServlet
{
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(Request _request, Response _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;
}
}
PrintWriter writer = _response.getWriter();
writer.println("");
writer.println("Servlet Context Tester Servlet");
writer.println("");
writer.println("Servlet Context Tester Servlet
");
writer.println("
");
DumpServletContextServlet.dump_servlet_context(writer, servlet_context);
writer.println("
");
writer.println("");
writer.println("");
writer.println("");
}
}
==================================================
DumpServletContextServlet.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.File;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.ServletContext;
import stec.iws.Request;
import stec.iws.Response;
public class DumpServletContextServlet extends BaseServlet
{
public void service(Request _request, Response _response) throws ServletException, IOException
{
ServletContext servlet_context = getServletContext();
String key;
String value;
PrintWriter writer = _response.getWriter();
writer.println("");
writer.println("Dump Servlet Context Servlet");
writer.println("");
writer.println("Dump Servlet Context Servlet
");
writer.println("
");
writer.println("Servlet Name: " + getServletName() + "
");
writer.println("Servlet Context Path: " + _request.getContextPath());
dump_servlet_context(writer, servlet_context);
writer.println("
");
writer.println("Servlet Context Tester");
writer.println("");
writer.println("");
}
public static void dump_servlet_context(PrintWriter writer, ServletContext servlet_context) throws IOException
{
writer.println("Parameters:
");
String key;
String value;
Enumeration e = servlet_context.getInitParameterNames();
while(e.hasMoreElements())
{
key = (String)e.nextElement();
value = servlet_context.getInitParameter(key);
writer.println(key + " = [" + value + "]
");
}
writer.println("
");
writer.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();
}
writer.println(key + " = [" + value + "]
");
}
}
}
==================================================
BaseServlet.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.Locale;
import java.io.IOException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import stec.iws.iws;
import stec.iws.Request;
import stec.iws.Response;
public abstract class BaseServlet extends HttpServlet
{
public void service(HttpServletRequest _request, HttpServletResponse _response) throws ServletException, IOException
{
Request request = (Request)_request;
Response response = (Response)_response;
String charset = request.getCharset();
if(charset == null)
{
charset = iws.getDefaultCharset();
}
String content_type = "text/html";
if(charset != null)
{
content_type = content_type + "; charset=" + charset;
}
_response.setContentType(content_type);
String language;
Locale locale = request.getLocale();
if(locale == null)
{
language = iws.getDefaultLanguage();
}
else
{
language = locale.toString();
}
if(language != null)
{
_response.setHeader("Content-Language", language.replace('_', '-'));
}
service(request, response);
}
public abstract void service(Request _request, Response _response) throws ServletException, IOException;
}