Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 771 | blopes | 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 | import java.io.IOException; |
||
| 18 | import java.io.PrintWriter; |
||
| 19 | import java.util.ResourceBundle; |
||
| 20 | |||
| 21 | import javax.servlet.ServletException; |
||
| 22 | import javax.servlet.http.Cookie; |
||
| 23 | import javax.servlet.http.HttpServlet; |
||
| 24 | import javax.servlet.http.HttpServletRequest; |
||
| 25 | import javax.servlet.http.HttpServletResponse; |
||
| 26 | import javax.servlet.http.HttpSession; |
||
| 27 | |||
| 28 | import util.CookieFilter; |
||
| 29 | import util.HTMLFilter; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Example servlet showing request headers |
||
| 33 | */ |
||
| 34 | public class CookieExample extends HttpServlet { |
||
| 35 | |||
| 36 | private static final long serialVersionUID = 1L; |
||
| 37 | |||
| 38 | @Override |
||
| 39 | public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { |
||
| 40 | ResourceBundle rb = ResourceBundle.getBundle("LocalStrings", request.getLocale()); |
||
| 41 | |||
| 42 | String cookieName = request.getParameter("cookiename"); |
||
| 43 | String cookieValue = request.getParameter("cookievalue"); |
||
| 44 | Cookie aCookie = null; |
||
| 45 | if (cookieName != null && cookieValue != null) { |
||
| 46 | aCookie = new Cookie(cookieName, cookieValue); |
||
| 47 | aCookie.setPath(request.getContextPath() + "/"); |
||
| 48 | response.addCookie(aCookie); |
||
| 49 | } |
||
| 50 | |||
| 51 | response.setContentType("text/html"); |
||
| 52 | response.setCharacterEncoding("UTF-8"); |
||
| 53 | |||
| 54 | PrintWriter out = response.getWriter(); |
||
| 55 | out.println("<!DOCTYPE html><html>"); |
||
| 56 | out.println("<head>"); |
||
| 57 | out.println("<meta charset=\"UTF-8\" />"); |
||
| 58 | |||
| 59 | String title = rb.getString("cookies.title"); |
||
| 60 | out.println("<title>" + title + "</title>"); |
||
| 61 | out.println("</head>"); |
||
| 62 | out.println("<body bgcolor=\"white\">"); |
||
| 63 | |||
| 64 | out.println("<a href=\"../cookies.html\">"); |
||
| 65 | out.println( |
||
| 66 | "<img src=\"../images/code.gif\" height=24 " + "width=24 align=right border=0 alt=\"view code\"></a>"); |
||
| 67 | out.println("<a href=\"../index.html\">"); |
||
| 68 | out.println( |
||
| 69 | "<img src=\"../images/return.gif\" height=24 " + "width=24 align=right border=0 alt=\"return\"></a>"); |
||
| 70 | |||
| 71 | out.println("<h3>" + title + "</h3>"); |
||
| 72 | |||
| 73 | Cookie[] cookies = request.getCookies(); |
||
| 74 | if ((cookies != null) && (cookies.length > 0)) { |
||
| 75 | HttpSession session = request.getSession(false); |
||
| 76 | String sessionId = null; |
||
| 77 | if (session != null) { |
||
| 78 | sessionId = session.getId(); |
||
| 79 | } |
||
| 80 | out.println(rb.getString("cookies.cookies") + "<br>"); |
||
| 81 | for (Cookie cookie : cookies) { |
||
| 82 | String cName = cookie.getName(); |
||
| 83 | String cValue = cookie.getValue(); |
||
| 84 | out.print("Cookie Name: " + HTMLFilter.filter(cName) + "<br>"); |
||
| 85 | out.println(" Cookie Value: " + HTMLFilter.filter(CookieFilter.filter(cName, cValue, sessionId)) + |
||
| 86 | "<br><br>"); |
||
| 87 | } |
||
| 88 | } else { |
||
| 89 | out.println(rb.getString("cookies.no-cookies")); |
||
| 90 | } |
||
| 91 | |||
| 92 | if (aCookie != null) { |
||
| 93 | out.println("<P>"); |
||
| 94 | out.println(rb.getString("cookies.set") + "<br>"); |
||
| 95 | out.print(rb.getString("cookies.name") + " " + HTMLFilter.filter(cookieName) + "<br>"); |
||
| 96 | out.print(rb.getString("cookies.value") + " " + HTMLFilter.filter(cookieValue)); |
||
| 97 | } |
||
| 98 | |||
| 99 | out.println("<P>"); |
||
| 100 | out.println(rb.getString("cookies.make-cookie") + "<br>"); |
||
| 101 | out.print("<form action=\""); |
||
| 102 | out.println("CookieExample\" method=POST>"); |
||
| 103 | out.print(rb.getString("cookies.name") + " "); |
||
| 104 | out.println("<input type=text length=20 name=cookiename><br>"); |
||
| 105 | out.print(rb.getString("cookies.value") + " "); |
||
| 106 | out.println("<input type=text length=20 name=cookievalue><br>"); |
||
| 107 | out.println("<input type=submit></form>"); |
||
| 108 | |||
| 109 | |||
| 110 | out.println("</body>"); |
||
| 111 | out.println("</html>"); |
||
| 112 | } |
||
| 113 | |||
| 114 | @Override |
||
| 115 | public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { |
||
| 116 | doGet(request, response); |
||
| 117 | } |
||
| 118 | |||
| 119 | } |
||
| 120 |