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 664175 2008-06-06 22:43:44Z djencks $
018     *
019     */
020    
021    import java.io.IOException;
022    import java.io.PrintWriter;
023    import java.util.ResourceBundle;
024    
025    import javax.servlet.ServletException;
026    import javax.servlet.http.Cookie;
027    import javax.servlet.http.HttpServlet;
028    import javax.servlet.http.HttpServletRequest;
029    import javax.servlet.http.HttpServletResponse;
030    
031    import util.HTMLFilter;
032    
033    /**
034     * Example servlet showing request headers
035     *
036     * @author James Duncan Davidson <duncan@eng.sun.com>
037     */
038    
039    public class CookieExample extends HttpServlet {
040    
041        ResourceBundle rb = ResourceBundle.getBundle("LocalStrings");
042    
043        public void doGet(HttpServletRequest request,
044                          HttpServletResponse response)
045                throws IOException, ServletException {
046            response.setContentType("text/html");
047    
048            PrintWriter out = response.getWriter();
049            out.println("<html>");
050            out.println("<body bgcolor=\"white\">");
051            out.println("<head>");
052    
053            String title = rb.getString("cookies.title");
054            out.println("<title>" + title + "</title>");
055            out.println("</head>");
056            out.println("<body>");
057    
058            // relative links
059    
060            // XXX
061            // making these absolute till we work out the
062            // addition of a PathInfo issue 
063    
064            out.println("<a href=\"../cookies.html\">");
065            out.println("<img src=\"../images/code.gif\" height=24 " +
066                    "width=24 align=right border=0 alt=\"view code\"></a>");
067            out.println("<a href=\"../index.html\">");
068            out.println("<img src=\"../images/return.gif\" height=24 " +
069                    "width=24 align=right border=0 alt=\"return\"></a>");
070    
071            out.println("<h3>" + title + "</h3>");
072    
073            Cookie[] cookies = request.getCookies();
074            if ((cookies != null) && (cookies.length > 0)) {
075                out.println(rb.getString("cookies.cookies") + "<br>");
076                for (int i = 0; i < cookies.length; i++) {
077                    Cookie cookie = cookies[i];
078                    out.print("Cookie Name: " + HTMLFilter.filter(cookie.getName())
079                            + "<br>");
080                    out.println("  Cookie Value: "
081                            + HTMLFilter.filter(cookie.getValue())
082                            + "<br><br>");
083                }
084            } else {
085                out.println(rb.getString("cookies.no-cookies"));
086            }
087    
088            String cookieName = request.getParameter("cookiename");
089            String cookieValue = request.getParameter("cookievalue");
090            if (cookieName != null && cookieValue != null) {
091                Cookie cookie = new Cookie(cookieName, cookieValue);
092                response.addCookie(cookie);
093                out.println("<P>");
094                out.println(rb.getString("cookies.set") + "<br>");
095                out.print(rb.getString("cookies.name") + "  "
096                        + HTMLFilter.filter(cookieName) + "<br>");
097                out.print(rb.getString("cookies.value") + "  "
098                        + HTMLFilter.filter(cookieValue));
099            }
100    
101            out.println("<P>");
102            out.println(rb.getString("cookies.make-cookie") + "<br>");
103            out.print("<form action=\"");
104            out.println("CookieExample\" method=POST>");
105            out.print(rb.getString("cookies.name") + "  ");
106            out.println("<input type=text length=20 name=cookiename><br>");
107            out.print(rb.getString("cookies.value") + "  ");
108            out.println("<input type=text length=20 name=cookievalue><br>");
109            out.println("<input type=submit></form>");
110    
111    
112            out.println("</body>");
113            out.println("</html>");
114        }
115    
116        public void doPost(HttpServletRequest request,
117                           HttpServletResponse response)
118                throws IOException, ServletException {
119            doGet(request, response);
120        }
121    
122    }
123    
124