blob: b3e1c09735374a4572cbe89e59454091f8ce9090 [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
刘洪青6266f992017-05-15 21:21:03 +080018import java.io.IOException;
19import java.io.PrintWriter;
20import java.util.ResourceBundle;
21
22import javax.servlet.ServletException;
23import javax.servlet.http.Cookie;
24import javax.servlet.http.HttpServlet;
25import javax.servlet.http.HttpServletRequest;
26import javax.servlet.http.HttpServletResponse;
Hongqing Liufd5ee812014-05-10 16:32:51 +080027
28import util.HTMLFilter;
29
30/**
31 * Example servlet showing request headers
32 *
33 * @author James Duncan Davidson <duncan@eng.sun.com>
34 */
35
36public class CookieExample extends HttpServlet {
37
刘洪青6266f992017-05-15 21:21:03 +080038 private static final long serialVersionUID = 1L;
39
40 private static final ResourceBundle RB = ResourceBundle.getBundle("LocalStrings");
41
42 @Override
Hongqing Liufd5ee812014-05-10 16:32:51 +080043 public void doGet(HttpServletRequest request,
44 HttpServletResponse response)
45 throws IOException, ServletException
46 {
刘洪青6266f992017-05-15 21:21:03 +080047
48 String cookieName = request.getParameter("cookiename");
49 String cookieValue = request.getParameter("cookievalue");
50 Cookie aCookie = null;
51 if (cookieName != null && cookieValue != null) {
52 aCookie = new Cookie(cookieName, cookieValue);
53 aCookie.setPath(request.getContextPath() + "/");
54 response.addCookie(aCookie);
55 }
56
Hongqing Liufd5ee812014-05-10 16:32:51 +080057 response.setContentType("text/html");
58
59 PrintWriter out = response.getWriter();
60 out.println("<html>");
61 out.println("<head>");
62
刘洪青6266f992017-05-15 21:21:03 +080063 String title = RB.getString("cookies.title");
Hongqing Liufd5ee812014-05-10 16:32:51 +080064 out.println("<title>" + title + "</title>");
65 out.println("</head>");
66 out.println("<body bgcolor=\"white\">");
67
刘洪青6266f992017-05-15 21:21:03 +080068 // relative links
Hongqing Liufd5ee812014-05-10 16:32:51 +080069
70 // XXX
71 // making these absolute till we work out the
刘洪青6266f992017-05-15 21:21:03 +080072 // addition of a PathInfo issue
73
Hongqing Liufd5ee812014-05-10 16:32:51 +080074 out.println("<a href=\"../cookies.html\">");
75 out.println("<img src=\"../images/code.gif\" height=24 " +
76 "width=24 align=right border=0 alt=\"view code\"></a>");
77 out.println("<a href=\"../index.html\">");
78 out.println("<img src=\"../images/return.gif\" height=24 " +
79 "width=24 align=right border=0 alt=\"return\"></a>");
80
81 out.println("<h3>" + title + "</h3>");
82
83 Cookie[] cookies = request.getCookies();
84 if ((cookies != null) && (cookies.length > 0)) {
刘洪青6266f992017-05-15 21:21:03 +080085 out.println(RB.getString("cookies.cookies") + "<br>");
Hongqing Liufd5ee812014-05-10 16:32:51 +080086 for (int i = 0; i < cookies.length; i++) {
87 Cookie cookie = cookies[i];
88 out.print("Cookie Name: " + HTMLFilter.filter(cookie.getName())
89 + "<br>");
刘洪青6266f992017-05-15 21:21:03 +080090 out.println(" Cookie Value: "
Hongqing Liufd5ee812014-05-10 16:32:51 +080091 + HTMLFilter.filter(cookie.getValue())
92 + "<br><br>");
93 }
94 } else {
刘洪青6266f992017-05-15 21:21:03 +080095 out.println(RB.getString("cookies.no-cookies"));
Hongqing Liufd5ee812014-05-10 16:32:51 +080096 }
97
刘洪青6266f992017-05-15 21:21:03 +080098 if (aCookie != null) {
Hongqing Liufd5ee812014-05-10 16:32:51 +080099 out.println("<P>");
刘洪青6266f992017-05-15 21:21:03 +0800100 out.println(RB.getString("cookies.set") + "<br>");
101 out.print(RB.getString("cookies.name") + " "
Hongqing Liufd5ee812014-05-10 16:32:51 +0800102 + HTMLFilter.filter(cookieName) + "<br>");
刘洪青6266f992017-05-15 21:21:03 +0800103 out.print(RB.getString("cookies.value") + " "
Hongqing Liufd5ee812014-05-10 16:32:51 +0800104 + HTMLFilter.filter(cookieValue));
105 }
刘洪青6266f992017-05-15 21:21:03 +0800106
Hongqing Liufd5ee812014-05-10 16:32:51 +0800107 out.println("<P>");
刘洪青6266f992017-05-15 21:21:03 +0800108 out.println(RB.getString("cookies.make-cookie") + "<br>");
Hongqing Liufd5ee812014-05-10 16:32:51 +0800109 out.print("<form action=\"");
110 out.println("CookieExample\" method=POST>");
刘洪青6266f992017-05-15 21:21:03 +0800111 out.print(RB.getString("cookies.name") + " ");
Hongqing Liufd5ee812014-05-10 16:32:51 +0800112 out.println("<input type=text length=20 name=cookiename><br>");
刘洪青6266f992017-05-15 21:21:03 +0800113 out.print(RB.getString("cookies.value") + " ");
Hongqing Liufd5ee812014-05-10 16:32:51 +0800114 out.println("<input type=text length=20 name=cookievalue><br>");
115 out.println("<input type=submit></form>");
刘洪青6266f992017-05-15 21:21:03 +0800116
117
Hongqing Liufd5ee812014-05-10 16:32:51 +0800118 out.println("</body>");
119 out.println("</html>");
120 }
121
刘洪青6266f992017-05-15 21:21:03 +0800122 @Override
Hongqing Liufd5ee812014-05-10 16:32:51 +0800123 public void doPost(HttpServletRequest request,
124 HttpServletResponse response)
125 throws IOException, ServletException
126 {
127 doGet(request, response);
128 }
129
130}
131
132