Source Code : JSP: deal with cookie

Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com. Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com. Click Here or search from google with Libraries Name you get jar file related it

JSP: deal with cookie

<%@ page import="javax.servlet.http.Cookie" %>

<html>
<head>
<title>This page leaves a cookie</title>
</head>

<body>
<h1>Cookies</h1>

<%
  Cookie[] allCookies = request.getCookies();
  Cookie ourCookie = null;
  if (allCookies!=null)
  {
    for (int i=0; i<allCookies.length; i++)
    {
      if (allCookies[i].getName().equals("TestCookie"))
        {
          ourCookie = allCookies[i];
        }
    }
  }

  if (ourCookie == null)
  {
    Cookie cookie = new Cookie("TestCookie", "hello from cookie");
    //cookie.setMaxAge(1800);
    //cookie.setDomain("alex:8080");
    cookie.setPath("/");
    response.addCookie(cookie);
%>
    A cookie has been added to your machine!
    <br>Select refresh to see the details of this cookie.

<%
  }
  else
  {
%>
    The following cookie was added earlier to your machine:
    <br>Version: <%=ourCookie.getVersion() %>
    <br>Name: <%=ourCookie.getName() %>
    <br>Value: <%=ourCookie.getValue() %>
    <br>MaxAge: <%=ourCookie.getMaxAge() %>
<%
  }
%>

</body>
</html>
           
       

Thank with us