Hongqing Liu | fd5ee81 | 2014-05-10 16:32:51 +0800 | [diff] [blame] | 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 |
|
刘洪青 | 6266f99 | 2017-05-15 21:21:03 +0800 | [diff] [blame^] | 18 | import java.io.IOException;
|
| 19 | import java.io.PrintWriter;
|
| 20 | import java.util.ResourceBundle;
|
| 21 |
|
| 22 | import javax.servlet.ServletException;
|
| 23 | import javax.servlet.http.Cookie;
|
| 24 | import javax.servlet.http.HttpServlet;
|
| 25 | import javax.servlet.http.HttpServletRequest;
|
| 26 | import javax.servlet.http.HttpServletResponse;
|
Hongqing Liu | fd5ee81 | 2014-05-10 16:32:51 +0800 | [diff] [blame] | 27 |
|
| 28 | import util.HTMLFilter;
|
| 29 |
|
| 30 | /**
|
| 31 | * Example servlet showing request headers
|
| 32 | *
|
| 33 | * @author James Duncan Davidson <duncan@eng.sun.com>
|
| 34 | */
|
| 35 |
|
| 36 | public class CookieExample extends HttpServlet {
|
| 37 |
|
刘洪青 | 6266f99 | 2017-05-15 21:21:03 +0800 | [diff] [blame^] | 38 | private static final long serialVersionUID = 1L;
|
| 39 |
|
| 40 | private static final ResourceBundle RB = ResourceBundle.getBundle("LocalStrings");
|
| 41 |
|
| 42 | @Override
|
Hongqing Liu | fd5ee81 | 2014-05-10 16:32:51 +0800 | [diff] [blame] | 43 | public void doGet(HttpServletRequest request,
|
| 44 | HttpServletResponse response)
|
| 45 | throws IOException, ServletException
|
| 46 | {
|
刘洪青 | 6266f99 | 2017-05-15 21:21:03 +0800 | [diff] [blame^] | 47 |
|
| 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 Liu | fd5ee81 | 2014-05-10 16:32:51 +0800 | [diff] [blame] | 57 | response.setContentType("text/html");
|
| 58 |
|
| 59 | PrintWriter out = response.getWriter();
|
| 60 | out.println("<html>");
|
| 61 | out.println("<head>");
|
| 62 |
|
刘洪青 | 6266f99 | 2017-05-15 21:21:03 +0800 | [diff] [blame^] | 63 | String title = RB.getString("cookies.title");
|
Hongqing Liu | fd5ee81 | 2014-05-10 16:32:51 +0800 | [diff] [blame] | 64 | out.println("<title>" + title + "</title>");
|
| 65 | out.println("</head>");
|
| 66 | out.println("<body bgcolor=\"white\">");
|
| 67 |
|
刘洪青 | 6266f99 | 2017-05-15 21:21:03 +0800 | [diff] [blame^] | 68 | // relative links
|
Hongqing Liu | fd5ee81 | 2014-05-10 16:32:51 +0800 | [diff] [blame] | 69 |
|
| 70 | // XXX
|
| 71 | // making these absolute till we work out the
|
刘洪青 | 6266f99 | 2017-05-15 21:21:03 +0800 | [diff] [blame^] | 72 | // addition of a PathInfo issue
|
| 73 |
|
Hongqing Liu | fd5ee81 | 2014-05-10 16:32:51 +0800 | [diff] [blame] | 74 | 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)) {
|
刘洪青 | 6266f99 | 2017-05-15 21:21:03 +0800 | [diff] [blame^] | 85 | out.println(RB.getString("cookies.cookies") + "<br>");
|
Hongqing Liu | fd5ee81 | 2014-05-10 16:32:51 +0800 | [diff] [blame] | 86 | for (int i = 0; i < cookies.length; i++) {
|
| 87 | Cookie cookie = cookies[i];
|
| 88 | out.print("Cookie Name: " + HTMLFilter.filter(cookie.getName())
|
| 89 | + "<br>");
|
刘洪青 | 6266f99 | 2017-05-15 21:21:03 +0800 | [diff] [blame^] | 90 | out.println(" Cookie Value: "
|
Hongqing Liu | fd5ee81 | 2014-05-10 16:32:51 +0800 | [diff] [blame] | 91 | + HTMLFilter.filter(cookie.getValue())
|
| 92 | + "<br><br>");
|
| 93 | }
|
| 94 | } else {
|
刘洪青 | 6266f99 | 2017-05-15 21:21:03 +0800 | [diff] [blame^] | 95 | out.println(RB.getString("cookies.no-cookies"));
|
Hongqing Liu | fd5ee81 | 2014-05-10 16:32:51 +0800 | [diff] [blame] | 96 | }
|
| 97 |
|
刘洪青 | 6266f99 | 2017-05-15 21:21:03 +0800 | [diff] [blame^] | 98 | if (aCookie != null) {
|
Hongqing Liu | fd5ee81 | 2014-05-10 16:32:51 +0800 | [diff] [blame] | 99 | out.println("<P>");
|
刘洪青 | 6266f99 | 2017-05-15 21:21:03 +0800 | [diff] [blame^] | 100 | out.println(RB.getString("cookies.set") + "<br>");
|
| 101 | out.print(RB.getString("cookies.name") + " "
|
Hongqing Liu | fd5ee81 | 2014-05-10 16:32:51 +0800 | [diff] [blame] | 102 | + HTMLFilter.filter(cookieName) + "<br>");
|
刘洪青 | 6266f99 | 2017-05-15 21:21:03 +0800 | [diff] [blame^] | 103 | out.print(RB.getString("cookies.value") + " "
|
Hongqing Liu | fd5ee81 | 2014-05-10 16:32:51 +0800 | [diff] [blame] | 104 | + HTMLFilter.filter(cookieValue));
|
| 105 | }
|
刘洪青 | 6266f99 | 2017-05-15 21:21:03 +0800 | [diff] [blame^] | 106 |
|
Hongqing Liu | fd5ee81 | 2014-05-10 16:32:51 +0800 | [diff] [blame] | 107 | out.println("<P>");
|
刘洪青 | 6266f99 | 2017-05-15 21:21:03 +0800 | [diff] [blame^] | 108 | out.println(RB.getString("cookies.make-cookie") + "<br>");
|
Hongqing Liu | fd5ee81 | 2014-05-10 16:32:51 +0800 | [diff] [blame] | 109 | out.print("<form action=\"");
|
| 110 | out.println("CookieExample\" method=POST>");
|
刘洪青 | 6266f99 | 2017-05-15 21:21:03 +0800 | [diff] [blame^] | 111 | out.print(RB.getString("cookies.name") + " ");
|
Hongqing Liu | fd5ee81 | 2014-05-10 16:32:51 +0800 | [diff] [blame] | 112 | out.println("<input type=text length=20 name=cookiename><br>");
|
刘洪青 | 6266f99 | 2017-05-15 21:21:03 +0800 | [diff] [blame^] | 113 | out.print(RB.getString("cookies.value") + " ");
|
Hongqing Liu | fd5ee81 | 2014-05-10 16:32:51 +0800 | [diff] [blame] | 114 | out.println("<input type=text length=20 name=cookievalue><br>");
|
| 115 | out.println("<input type=submit></form>");
|
刘洪青 | 6266f99 | 2017-05-15 21:21:03 +0800 | [diff] [blame^] | 116 |
|
| 117 |
|
Hongqing Liu | fd5ee81 | 2014-05-10 16:32:51 +0800 | [diff] [blame] | 118 | out.println("</body>");
|
| 119 | out.println("</html>");
|
| 120 | }
|
| 121 |
|
刘洪青 | 6266f99 | 2017-05-15 21:21:03 +0800 | [diff] [blame^] | 122 | @Override
|
Hongqing Liu | fd5ee81 | 2014-05-10 16:32:51 +0800 | [diff] [blame] | 123 | public void doPost(HttpServletRequest request,
|
| 124 | HttpServletResponse response)
|
| 125 | throws IOException, ServletException
|
| 126 | {
|
| 127 | doGet(request, response);
|
| 128 | }
|
| 129 |
|
| 130 | }
|
| 131 |
|
| 132 |
|