CookieTesterServlet.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.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.http.Cookie;
import stec.iws.Utils;
public final class CookieTesterServlet extends HttpServlet
{
public final void service(HttpServletRequest _request, HttpServletResponse _response) throws ServletException, IOException
{
_response.setContentType("text/html");
String key = null;
String value = null;
String action = _request.getParameter("action");
if(action == null)
{
action = "";
}
if(Utils.equalsIgnoreCase(action, "set cookie"))
{
key = _request.getParameter("key");
if(key != null)
{
key = key.trim();
if(key.length() > 0)
{
value = _request.getParameter("value");
if(value == null)
{
value = "";
}
_response.addCookie(new Cookie(key, value));
}
}
}
else if(Utils.equalsIgnoreCase(action, "remove cookie"))
{
key = _request.getParameter("key");
if(key != null)
{
key = key.trim();
if(key.length() > 0)
{
Cookie cookie = new Cookie(key, "");
cookie.setMaxAge(0);
_response.addCookie(cookie);
}
}
}
ServletOutputStream out = _response.getOutputStream();
out.print("Cookie Tester ServletCookie Tester Servlet
");
if(key != null)
{
if(key.length() > 0)
{
if(Utils.equalsIgnoreCase(action, "set cookie"))
{
out.print("Cookie to add or update:
");
out.print(key + " = [" + value + "]");
}
else if(Utils.equalsIgnoreCase(action, "remove cookie"))
{
out.print("Cookie to remove:
");
out.print(key + "
");
}
}
}
out.print("Cookies received from browser:
");
Cookie cookies[] = _request.getCookies();
if(cookies.length == 0)
{
out.print("None");
}
else
{
int length = cookies.length;
for(int i = 0; i < length; i++)
{
out.print(cookies[i].getName());
out.print(" = ");
out.print(cookies[i].getValue());
out.print("
");
}
}
out.print("
");
out.close();
}
}