Cookie Demo : 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.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Servlets » Cookie 




Cookie Demo
    

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 CookieServlet extends HttpServlet {
  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, java.io.IOException {

    Cookie cookie = null;
    Cookie[] cookies = request.getCookies();
    boolean newCookie = false;

    if (cookies != null) {
      for (int i = 0; i < cookies.length; i++) {
        if (cookies[i].getName().equals("mycookie")) {
          cookie = cookies[i];
        }
      }
    }
    if (cookie == null) {
      newCookie = true;
      int maxAge;
      try {
        maxAge = new Integer(getServletContext().getInitParameter(
            "cookie-age")).intValue();
      catch (Exception e) {
        maxAge = -1;
      }

      cookie = new Cookie("mycookie""" + getNextCookieValue());
      cookie.setPath(request.getContextPath());
      cookie.setMaxAge(maxAge);
      response.addCookie(cookie);
    }
    response.setContentType("text/html");
    java.io.PrintWriter out = response.getWriter();

    out.println("<html>");
    out.println("<head>");
    out.println("<title>Cookie info</title>");
    out.println("</head>");
    out.println("<body>");

    out
        .println("<h2> Information about the cookie named \"mycookie\"</h2>");

    out.println("Cookie value: " + cookie.getValue() "<br>");
    if (newCookie) {
      out.println("Cookie Max-Age: " + cookie.getMaxAge() "<br>");
      out.println("Cookie Path: " + cookie.getPath() "<br>");
    }

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

    out.close();
  }

  private long getNextCookieValue() {
    return new java.util.Date().getTime();

  }

  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 reader
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.