Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

How to expire and destroy Cookies in JSP and Servlets ?

A simple application to demonstrate how to expire and destroy Cookies in JSP and Servlets

Step 1 : Create index.jsp

Here index.jsp just transfer control / request to cookies.jsp


1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Refresh" content="0; URL=cookies.jsp">
    </head>
    <body>
    </body>
</html>

Step 2 : Create mystyle.css

Here mystyle.css is just for providing style to JSP page. Things will also work without using it.


1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.
88.
89.
90.
91.
92.
93.
94.
95.
96.
97.
98.
99.
100.
101.
102.
103.
104.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Adding Cookies</title>
<link href="mystyle.css" rel="stylesheet" type="text/css">
</head>
<body>
 <%@ page import="javax.servlet.http.Cookie;"%>
 <form method="post" action="cookies.jsp">
  <p>&nbsp;</p>
  <p align="center" class="black">Enter cookie name and cookie
   value.</p>
  <table align="center" bgcolor="#728DCF">
   <tr>
    <td>Cookie Name</td>
    <td><input name=name size=20 class="smalltext"></td>
   </tr>
   <tr>
    <td>Cookie Value</td>
    <td><input name=value size=20 class="smalltext"></td>
   </tr>

   <tr>
    <td colspan="2" align="center"><input type="submit"
     value="Add Cookie" /></td>
   </tr>
  </table>
 </form>
 </td>
 </tr>
 <tr>
  <td>&nbsp;</td>
 </tr>
 </table>

 <%
  // Checks for the value of name that it is null or not
  String name = request.getParameter("name");
  if (name != null && name.length() > 0) {
   String value = request.getParameter("value");
   Cookie retrieveCookies = new Cookie(name, value);
   // Setting the max age of Cookie to 5 minute 
   retrieveCookies.setMaxAge(5 * 60);
   // add cookies to response object of jsp 
   response.addCookie(retrieveCookies);
   // redirects control to same page for new request
   response.sendRedirect("cookies.jsp");
  }
 %>

 <p>&nbsp;</p>
 <p align="center" class="black">Cookies used so far ...</p>
 <table align="center" cellpadding="1" cellspacing="1" border="1"
  bgcolor="#728DCF">
  <tr bgcolor="#000000">
   <td>Cookie Name</td>
   <td>Cookie Value</td>
  </tr>

  <%
   // getCookies() method returns array of Cookie 
   // we iterate over it and extract the name and value
   // into seperate variables cookieName and cookieValue
   Cookie[] cookies = request.getCookies();
   for (int i = 0; i < cookies.length; i++) {
    Cookie cookie = cookies[i];
    String cookieName = cookie.getName();
    String cookieValue = cookie.getValue();
    // display each value on jsp by iterating one by one

    // Above we have set the max age to be 5 minutes, in order
    // to destroy a cookie completely now say before expiration
    // we perform a check over the cookie name we want to destroy
    // let for example we want to destroy cookie with name "BOOKS"
    // after 1 minute ... below is the procedure to destroy it  ... 
    if (cookieName.equals("BOOKS")) {
     // first creating a new cookie with name same as our old cookie
     // setting the value of cookie to empty or null
     Cookie booksCookie = new Cookie("BOOKS", "");
     // second setting the max age to be 1 minutes here books 
     // should have stayed 5 minutes but we are destorying it
     // early ... both steps are necessary because if we not 
     // follow the first step cookie value will remain there 
     // till brower window is closed
     booksCookie.setMaxAge(1 * 60);
     response.addCookie(booksCookie);
    }
  %>
  <tr>
   <td><%=cookieName%></td>
   <td><%=cookieValue%></td>
  </tr>

  <%
   }
  %>
 </table>
</body>
</html>

Output of the program :

1. Run index.jsp to see the following output :



















2. Add few of the Cookies name and value by regular submits






















 3. Just after 1 min BOOKS cookie gets destroyed




















4. After 5 minutes all the rest cookies gets destroyed ... 


 
© 2021 Learn Java by Examples Template by Hubberspot