Free Data Structures and Algorithms Course









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










OR



Subscribe to all free courses

How to add and retrieve Cookies in JSP and Servlets ?.


A simple web application demonstrating how to add and retrieve 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.

body {
  font-family: Verdana, Geneva, Arial, helvetica, sans-serif;
  background-color: #FDF4DF; 
  font-size:12px; 
  color:#FFFFFF;
}

td {
  font-family: Verdana, Geneva, Arial, helvetica, sans-serif;
  font-size:12px;
}

th {
  font-family: Verdana, Geneva, Arial, helvetica, sans-serif;
  font-size:12px;
}

.black {
  color: #000000;
  font-weight: bold;
}



Step 3 : Create cookies.jsp


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.

<%@ 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);
  // 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 
  %>
  <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 some Cookies name and values and submit : 




















 3. After adding few Cookies , on each submit you get to retrieve Cookies below :




 
© 2021 Learn Java by Examples Template by Hubberspot