Cookies in JSP

Cookies in JSP


Posted in : Java Posted on : May 8, 2012 at 7:21 PM Comments : [ 0 ]

This tutorial will help you in understanding the JSP Cookies.

Cookies in JSP

This tutorial will help you in understanding the JSP Cookies.

Cookies, are a simple text information that are sent by a server to a web browser which stores them at the client side. Web browser provides these information to the server, when/if asked, helps in to identify the regular user/visitor of a website. In perspective to Cookies these informations are unique for each user, so it helps in to identify the user uniquely which makes the cookies useful for session management. Cookies informations can be saved by their name, a single value, with optional attributes like comment, path and domain qualifiers, with their maximum age, and a version number. The main thing with Cookies is how to send to and retrieve from the web browser ? To add cookies to the browser addCookie() method of HttpServletResponse interface is used, it adds the field to HTTP response headers that can be sent to the browser one at a time. To return cookies browser adds fields to the HTTP request headers so, to retrieve them from the browser getCookies() method of HttpServletRequest interface is used. This method makes the request for returning the cookies.

NOTE : Name of various cookies may be the same, but their path attributes will be different.

Example :

Here we will give the example which will illustrate you about how to add cookies to the response and retrieve them by making request. For this we will use here two methods : addCookie() to add the cookies and getCookies() to retrieve the cookies. To create a specific cookie we would be required to specified a cookie for this we will have to use the Cookie constructor. And remember you will have to import javax.servlet.http.Cookie.

Syntax :

1. Cookie() : A constructor that is used to create a specific cookie. Argument of this constructor is of java.lang.String type.

Cookie(String name, String value)

1. addCookie() : This method adds cookies to the response and can be called a number of times to add number of cookies.

public void addCookie(Cookie cookie)

2. getCookies() : This method returns either an array of all Cookie objects sent through the client request or null if there were no cookies sent.

public Cookie[] getCookies()

In this example at first I have created a JSP page where I have designed a form and used two input type textboxes for taking input by the user and a submit button to submit the form. Then checked for whether the null value of input box and print their values. In further statements I have constructed a cookie object using constructor Cookie and specified a name with value. Then added this cookie to the response header using the response.addCookie(). Next to retrieve this specified Cookie used request.getCookies(). And to find out the name and the value of the specified cookie used the getName() and getValue() method of Cookie class respectively.

cookiesInJsp.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="javax.servlet.http.Cookie" %>
<!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>Cookies In JSP</title>
</head>
<body>
<form>
<table>
<tr>
<td>Enter Your Name :</td>
<td><input type="text" name="name"/></td>
</tr>
<tr>
<td>Enter Your Age :</td>
<td><input type="text" name="age"/></td>
</tr>
<tr>
<td></td><td><input type="submit" value="submit"/></td>
</tr>
</table>
</form>
<%
String nm = request.getParameter("name");
String ag = request.getParameter("age");

if(nm != null)
{
String trim = ag.trim();
int a = Integer.parseInt(ag);
%>
<table>
<tr>
<td>Name = </td> <td><%=nm %></td>
</tr>
<tr>
<td>Age = </td> <td><%=a %></td>
</tr>
</table>
<%
Cookie nCookie = new Cookie("nam", nm);
//Name cookie add to the response header
response.addCookie(nCookie);
}
%>

<%
Cookie cookie = null;
Cookie[] cookieArray = request.getCookies();
if(cookieArray != null)
{
%>
<p><b>Cookies Information</b></p>
<%
for(int i=0; i<cookieArray.length; i++)
{
cookie = cookieArray[i];
out.println("CookieName :" + cookie.getName());
out.println("CookieValue :" + cookie.getValue()); 
}
}
else
{
out.println("Cookies is not added in the response");
}
%>
</body>
</html>

Output :

1. When you will made request first time you will get the output as follows :

2. After clicking on submit button output will be :

3. But when you will made the request second time for the same page then the cookies will be as follows :

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics