Cookie reader : Cookie « Servlets « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » Servlets » CookieScreenshots 
Cookie reader
    

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CookieReader extends HttpServlet {

  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, java.io.IOException {

    Cookie cookie = null;
    //Get an array of Cookies associated with this domain
    Cookie[] cookies = request.getCookies();
    boolean hasCookies = false;

    if (cookies != null)
      hasCookies = true;

    // display the name/value of each cookie
    response.setContentType("text/html");
    java.io.PrintWriter out = response.getWriter();

    out.println("<html>");
    out.println("<head>");
    out.println("<title>Cookie information</title>");
    out.println("</head>");
    out.println("<body>");
    if (hasCookies) {
      out.println("<h2> The name and value of each found cookie</h2>");
      for (int i = 0; i < cookies.length; i++) {
        cookie = cookies[i];
        out.println("Name of cookie #" (i + 1": "
            + cookie.getName() "<br>");
        out.println("Value of cookie #" (i + 1": "
            + cookie.getValue() "<br><br>");

      }
    else {
      out.println("<h2> This request did not include any cookies</h2>");
    }

    out.println("</body>");
    out.println("</html>");

    out.close();
  }

  public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, java.io.IOException {

    doGet(request, response);
  }
}

           
         
    
    
    
  
Related examples in the same category
1.Setting and Reading Cookies
2.Cookie Demo
3.Use cookie to save session data
4.A utility class for parsing HTTP dates as used in cookies and other headers
5.Cookie Utilities
6.Utilities for finding and manipulating cookies
7.Parsing and formatting HTTP dates as used in cookies and other headers.
8.Parse a Cookie: header into individual tokens according to RFC 2109.
9.Cookie Utility
10.Cookie Util
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.