View Javadoc

1   /*
2   * Licensed to the Apache Software Foundation (ASF) under one or more
3   * contributor license agreements.  See the NOTICE file distributed with
4   * this work for additional information regarding copyright ownership.
5   * The ASF licenses this file to You under the Apache License, Version 2.0
6   * (the "License"); you may not use this file except in compliance with
7   * the License.  You may obtain a copy of the License at
8   *
9   *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17  /* $Id: CookieExample.java 664175 2008-06-06 22:43:44Z djencks $
18   *
19   */
20  
21  import java.io.IOException;
22  import java.io.PrintWriter;
23  import java.util.ResourceBundle;
24  
25  import javax.servlet.ServletException;
26  import javax.servlet.http.Cookie;
27  import javax.servlet.http.HttpServlet;
28  import javax.servlet.http.HttpServletRequest;
29  import javax.servlet.http.HttpServletResponse;
30  
31  import util.HTMLFilter;
32  
33  /**
34   * Example servlet showing request headers
35   *
36   * @author James Duncan Davidson <duncan@eng.sun.com>
37   */
38  
39  public class CookieExample extends HttpServlet {
40  
41      ResourceBundle rb = ResourceBundle.getBundle("LocalStrings");
42  
43      public void doGet(HttpServletRequest request,
44                        HttpServletResponse response)
45              throws IOException, ServletException {
46          response.setContentType("text/html");
47  
48          PrintWriter out = response.getWriter();
49          out.println("<html>");
50          out.println("<body bgcolor=\"white\">");
51          out.println("<head>");
52  
53          String title = rb.getString("cookies.title");
54          out.println("<title>" + title + "</title>");
55          out.println("</head>");
56          out.println("<body>");
57  
58          // relative links
59  
60          // XXX
61          // making these absolute till we work out the
62          // addition of a PathInfo issue 
63  
64          out.println("<a href=\"../cookies.html\">");
65          out.println("<img src=\"../images/code.gif\" height=24 " +
66                  "width=24 align=right border=0 alt=\"view code\"></a>");
67          out.println("<a href=\"../index.html\">");
68          out.println("<img src=\"../images/return.gif\" height=24 " +
69                  "width=24 align=right border=0 alt=\"return\"></a>");
70  
71          out.println("<h3>" + title + "</h3>");
72  
73          Cookie[] cookies = request.getCookies();
74          if ((cookies != null) && (cookies.length > 0)) {
75              out.println(rb.getString("cookies.cookies") + "<br>");
76              for (int i = 0; i < cookies.length; i++) {
77                  Cookie cookie = cookies[i];
78                  out.print("Cookie Name: " + HTMLFilter.filter(cookie.getName())
79                          + "<br>");
80                  out.println("  Cookie Value: "
81                          + HTMLFilter.filter(cookie.getValue())
82                          + "<br><br>");
83              }
84          } else {
85              out.println(rb.getString("cookies.no-cookies"));
86          }
87  
88          String cookieName = request.getParameter("cookiename");
89          String cookieValue = request.getParameter("cookievalue");
90          if (cookieName != null && cookieValue != null) {
91              Cookie cookie = new Cookie(cookieName, cookieValue);
92              response.addCookie(cookie);
93              out.println("<P>");
94              out.println(rb.getString("cookies.set") + "<br>");
95              out.print(rb.getString("cookies.name") + "  "
96                      + HTMLFilter.filter(cookieName) + "<br>");
97              out.print(rb.getString("cookies.value") + "  "
98                      + HTMLFilter.filter(cookieValue));
99          }
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