CookieExample.java
上传用户:bj_pst
上传日期:2019-07-07
资源大小:7353k
文件大小:4k
源码类别:

Java编程

开发平台:

Java

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