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 500674 2007-01-27 23:15:00Z markt $ |
---|
18 | * |
---|
19 | */ |
---|
20 | |
---|
21 | import java.io.*; |
---|
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 | |
---|