Deal with the cookie : Cookie « JSP « Java

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 Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » JSP » CookieScreenshots 
Deal with the cookie

// cookieReader.jsp
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<html>
<body>
<c:choose>
  <c:when test="${empty cookie}" >
  <h2>We did not find any cookies in the request</h2>
  </c:when>
  <c:otherwise>
<h2>The name and value of each found cookie</h2>
<c:forEach var="cookieVal" items="${cookie}">
<strong>Cookie name:</strong> <c:out value="${cookieVal.key}" /><br>
<strong>Cookie value:</strong> <c:out value="${cookieVal.value.value}" /><br><br>
</c:forEach>
</c:otherwise>
</c:choose>

</body>
</html>


// cookieSetter.jsp
<jsp:useBean id="cookieBean" class="com.java2s.CookieBean" />
<jsp:setProperty name="cookieBean" property="name"  value="bakedcookie" />
<jsp:setProperty name="cookieBean" property="maxAge"  value="<%=(365*24*60*60) %>" />
<jsp:setProperty name="cookieBean" property="path"  value="<%= request.getContextPath() %>" />
<jsp:setProperty name="cookieBean" property="cookieHeader"  value="<%= response %>" />
<html>
<head><title>Cookie Maker</title></head>
<body>
<h2>Here is information about the new cookie</h2>
Name: <jsp:getProperty name="cookieBean" property="name" /><br>
Value: <jsp:getProperty name="cookieBean" property="value" /><br>
Path: <jsp:getProperty name="cookieBean" property="path" />
</body>
</html>

// put the class file to WEB-INF/classes/com/java2s
//CookieBean.java
package com.java2s;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

public class CookieBean {

  private Cookie cookie = null;

  public CookieBean() {
  }

  public void setName(String name) {

    if (name == null || (name.equals("")))
      throw new IllegalArgumentException("Invalid cookie name set in: "
          + getClass().getName());

    cookie = new Cookie(name, "" new java.util.Date().getTime());
  }

  public void setValue(String value) {

    if (value == null || (value.equals("")))
      throw new IllegalArgumentException("Invalid cookie value set in: "
          + getClass().getName());

    if (cookie != null)
      cookie.setValue(value);

  }

  public void setMaxAge(int maxAge) {

    if (cookie != null)
      cookie.setMaxAge(maxAge);

  }

  public void setPath(String path) {

    if (path == null || (path.equals("")))
      throw new IllegalArgumentException("Invalid cookie path set in: "
          + getClass().getName());

    if (cookie != null)
      cookie.setPath(path);
  }

  public void setCookieHeader(HttpServletResponse response) {

    if (response == null)
      throw new IllegalArgumentException(
          "Invalid HttpServletResponse set in: "
              + getClass().getName());
    if (cookie != null)
      response.addCookie(cookie);
  }

  public String getName() {

    if (cookie != null)
      return cookie.getName();
    else
      return "unavailable";

  }

  public String getValue() {

    if (cookie != null)
      return cookie.getValue();
    else
      return "unavailable";

  }

  public String getPath() {

    if (cookie != null)
      return cookie.getPath();
    else
      return "unavailable";

  }




           
       
Related examples in the same category
1. JSP Create and List Cookie
2. JSP List All Cookie
3. Setting a Cookie
4. Setting and Reading Cookies
5. Cookie display in a JSP page
6. JSP: deal with cookie
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.