blob: 5d7d5cee9a95f1d13b1551c2bca2d28b17736427 [file] [log] [blame]
Hongqing Liufd5ee812014-05-10 16:32:51 +08001/*
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
18import java.io.*;
19import java.util.*;
20import javax.servlet.*;
21import javax.servlet.http.*;
22
23import util.HTMLFilter;
24
25/**
26 * Example servlet showing request headers
27 *
28 * @author James Duncan Davidson <duncan@eng.sun.com>
29 */
30
31public class CookieExample extends HttpServlet {
32
33 ResourceBundle rb = ResourceBundle.getBundle("LocalStrings");
34
35 public void doGet(HttpServletRequest request,
36 HttpServletResponse response)
37 throws IOException, ServletException
38 {
39 response.setContentType("text/html");
40
41 PrintWriter out = response.getWriter();
42 out.println("<html>");
43 out.println("<head>");
44
45 String title = rb.getString("cookies.title");
46 out.println("<title>" + title + "</title>");
47 out.println("</head>");
48 out.println("<body bgcolor=\"white\">");
49
50 // relative links
51
52 // XXX
53 // making these absolute till we work out the
54 // addition of a PathInfo issue
55
56 out.println("<a href=\"../cookies.html\">");
57 out.println("<img src=\"../images/code.gif\" height=24 " +
58 "width=24 align=right border=0 alt=\"view code\"></a>");
59 out.println("<a href=\"../index.html\">");
60 out.println("<img src=\"../images/return.gif\" height=24 " +
61 "width=24 align=right border=0 alt=\"return\"></a>");
62
63 out.println("<h3>" + title + "</h3>");
64
65 Cookie[] cookies = request.getCookies();
66 if ((cookies != null) && (cookies.length > 0)) {
67 out.println(rb.getString("cookies.cookies") + "<br>");
68 for (int i = 0; i < cookies.length; i++) {
69 Cookie cookie = cookies[i];
70 out.print("Cookie Name: " + HTMLFilter.filter(cookie.getName())
71 + "<br>");
72 out.println(" Cookie Value: "
73 + HTMLFilter.filter(cookie.getValue())
74 + "<br><br>");
75 }
76 } else {
77 out.println(rb.getString("cookies.no-cookies"));
78 }
79
80 String cookieName = request.getParameter("cookiename");
81 String cookieValue = request.getParameter("cookievalue");
82 if (cookieName != null && cookieValue != null) {
83 Cookie cookie = new Cookie(cookieName, cookieValue);
84 response.addCookie(cookie);
85 out.println("<P>");
86 out.println(rb.getString("cookies.set") + "<br>");
87 out.print(rb.getString("cookies.name") + " "
88 + HTMLFilter.filter(cookieName) + "<br>");
89 out.print(rb.getString("cookies.value") + " "
90 + HTMLFilter.filter(cookieValue));
91 }
92
93 out.println("<P>");
94 out.println(rb.getString("cookies.make-cookie") + "<br>");
95 out.print("<form action=\"");
96 out.println("CookieExample\" method=POST>");
97 out.print(rb.getString("cookies.name") + " ");
98 out.println("<input type=text length=20 name=cookiename><br>");
99 out.print(rb.getString("cookies.value") + " ");
100 out.println("<input type=text length=20 name=cookievalue><br>");
101 out.println("<input type=submit></form>");
102
103
104 out.println("</body>");
105 out.println("</html>");
106 }
107
108 public void doPost(HttpServletRequest request,
109 HttpServletResponse response)
110 throws IOException, ServletException
111 {
112 doGet(request, response);
113 }
114
115}
116
117