View Javadoc

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