JSP Setting cookies

In the previous chapter we have learnt the basic concepts of the cookies. In this chapter we will be learning how to set the cookies in JSP.

JSP Setting cookies

In order to demonstrate the cooking setting example consider the following index.html page -

<html>
<body>
   <form action="main.jsp" method="GET">
      <table>
         <tr>
            <td>Name</td>
            <td><input type="text" name="fname"></td>
         </tr>
         <tr>
            <td>City</td>
            <td><input type="text" name="city" /></td>
         </tr>
         <tr>
            <td>Pin</td>
            <td><input type="text" name="pin" /> &nbsp; 
                 <input type="submit" value="Submit" /></td>
         </tr>
      </table>
   </form>
</body>
</html>

Following screen will be created when we run above page on server -

form in jsp
Action mentioned in the above form is main.jsp

<form action="main.jsp" method="GET">

Below is the content of the main.jsp file.

<%
   // Creating the cookies object      
   Cookie fname = new Cookie("fname", request.getParameter("fname"));
   Cookie city = new Cookie("city", request.getParameter("city"));
   Cookie pin = new Cookie("pin", request.getParameter("pin"));
   // Setting expiry date of cookies = 24 Hr
   fname.setMaxAge(60 * 60 * 24);
   city.setMaxAge(60 * 60 * 24);
   pin.setMaxAge(60 * 60 * 24);
   // Adding cookies to the response header.
   response.addCookie(fname);
   response.addCookie(city);
   response.addCookie(pin);
%>
<html>
<head>
<title>JSP Setting Cookies</title>
</head>
<body>
   <h1>Values stored in cookies -</h1>
   <table>
      <tr>
         <td>Name</td>
         <td><%=request.getParameter("fname")%></td>
      </tr>
      <tr>
         <td>City</td>
         <td><%=request.getParameter("city")%></td>
      </tr>
      <tr>
         <td>Pin</td>
         <td><%=request.getParameter("pin")%></td>
      </tr>
   </table>
</body>
</html>

Steps for Setting cookies in JSP

There are following 3 steps involved for Setting cookies in JSP -

  1. Creating the cookies object
  2. Setting expiry date of cookies
  3. Adding cookies to the response header

Consider all the steps one by one -

Step 1 : Creating the cookies object

For Setting cookies in JSP we need to create the cookies in the JSP. Syntax for creating the cookie object is as follow -

Cookie cookie = new Cookie("key","value");

We need to pass Cookies name and cookie value as parameter to the Cookie constructor. Parameters passed to the constructor are of type string.

Following are some of the illegal characters which cannot be used for creating the cookie name -

  1. [ ]
  2. ( )
  3. = ,
  4. ” /
  5. ? @
  6. : ;

Step 2 : Setting expiry date of cookies

In this step we need to set the maximum age of the cookie after which cookies will be destroyed automatically. Below is the syntax to set maximum age of the cookie in JSP -

cookie.setMaxAge(int seconds)

below is the code snippet from the above example where we have set maximum age of the cookies to 25 hours.

fname.setMaxAge(60 * 60 * 24);
city.setMaxAge(60 * 60 * 24);
pin.setMaxAge(60 * 60 * 24);

Consider the above example -

setMaxAge() function is used for setting the maximum age of the cookie. Parameter passed to the setMaxAge() method is value in seconds for which cookies will be considered to be valid

Step 3 : Adding cookies to response header

In this step we add the cookie objects to the HTTP response header. Below is the syntax to add the cookies to the response header -

response.addCookie(cookie);