001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 /* $Id: CookieExample.java 514091 2007-03-03 06:26:39Z jdillon $ 018 * 019 */ 020 021 import java.io.*; 022 import java.util.*; 023 import javax.servlet.*; 024 import javax.servlet.http.*; 025 026 import util.HTMLFilter; 027 028 /** 029 * Example servlet showing request headers 030 * 031 * @author James Duncan Davidson <duncan@eng.sun.com> 032 */ 033 034 public class CookieExample extends HttpServlet { 035 036 ResourceBundle rb = ResourceBundle.getBundle("LocalStrings"); 037 038 public void doGet(HttpServletRequest request, 039 HttpServletResponse response) 040 throws IOException, ServletException 041 { 042 response.setContentType("text/html"); 043 044 PrintWriter out = response.getWriter(); 045 out.println("<html>"); 046 out.println("<body bgcolor=\"white\">"); 047 out.println("<head>"); 048 049 String title = rb.getString("cookies.title"); 050 out.println("<title>" + title + "</title>"); 051 out.println("</head>"); 052 out.println("<body>"); 053 054 // relative links 055 056 // XXX 057 // making these absolute till we work out the 058 // addition of a PathInfo issue 059 060 out.println("<a href=\"../cookies.html\">"); 061 out.println("<img src=\"../images/code.gif\" height=24 " + 062 "width=24 align=right border=0 alt=\"view code\"></a>"); 063 out.println("<a href=\"../index.html\">"); 064 out.println("<img src=\"../images/return.gif\" height=24 " + 065 "width=24 align=right border=0 alt=\"return\"></a>"); 066 067 out.println("<h3>" + title + "</h3>"); 068 069 Cookie[] cookies = request.getCookies(); 070 if ((cookies != null) && (cookies.length > 0)) { 071 out.println(rb.getString("cookies.cookies") + "<br>"); 072 for (int i = 0; i < cookies.length; i++) { 073 Cookie cookie = cookies[i]; 074 out.print("Cookie Name: " + HTMLFilter.filter(cookie.getName()) 075 + "<br>"); 076 out.println(" Cookie Value: " 077 + HTMLFilter.filter(cookie.getValue()) 078 + "<br><br>"); 079 } 080 } else { 081 out.println(rb.getString("cookies.no-cookies")); 082 } 083 084 String cookieName = request.getParameter("cookiename"); 085 String cookieValue = request.getParameter("cookievalue"); 086 if (cookieName != null && cookieValue != null) { 087 Cookie cookie = new Cookie(cookieName, cookieValue); 088 response.addCookie(cookie); 089 out.println("<P>"); 090 out.println(rb.getString("cookies.set") + "<br>"); 091 out.print(rb.getString("cookies.name") + " " 092 + HTMLFilter.filter(cookieName) + "<br>"); 093 out.print(rb.getString("cookies.value") + " " 094 + HTMLFilter.filter(cookieValue)); 095 } 096 097 out.println("<P>"); 098 out.println(rb.getString("cookies.make-cookie") + "<br>"); 099 out.print("<form action=\""); 100 out.println("CookieExample\" method=POST>"); 101 out.print(rb.getString("cookies.name") + " "); 102 out.println("<input type=text length=20 name=cookiename><br>"); 103 out.print(rb.getString("cookies.value") + " "); 104 out.println("<input type=text length=20 name=cookievalue><br>"); 105 out.println("<input type=submit></form>"); 106 107 108 out.println("</body>"); 109 out.println("</html>"); 110 } 111 112 public void doPost(HttpServletRequest request, 113 HttpServletResponse response) 114 throws IOException, ServletException 115 { 116 doGet(request, response); 117 } 118 119 } 120 121