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("<html><head><title>Cookie Tester Servlet</title></head><body><h1>Cookie Tester Servlet</h1><hr>"); if(key != null) { if(key.length() > 0) { if(Utils.equalsIgnoreCase(action, "set cookie")) { out.print("Cookie to add or update:<br>"); out.print(key + " = [" + value + "]<p>"); } else if(Utils.equalsIgnoreCase(action, "remove cookie")) { out.print("Cookie to remove:<br>"); out.print(key + "<p>"); } } } out.print("Cookies received from browser:<br>"); 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("<br>"); } } out.print("<br><br><form action=\"./cookietester.html\" method=\"get\">"); out.print("Key: "); out.print("<input type=\"text\" name=\"key\" value=\"\">"); out.print("<br>Value: "); out.print("<input type=\"text\" name=\"value\" value=\"\">"); out.print("<p><input type=\"submit\" name=\"action\" value=\"Set Cookie\">"); out.print("<input type=\"submit\" name=\"action\" value=\"Remove Cookie\">"); out.print("</form></body></html>"); out.close(); } }