Pr.Pg Next Pg

Setting Cookies with JSP tutorials

  • Setting cookies with servlet involves three steps:

  • Creating a cookies object: You create a cookie by calling the Cookie constructor, which takes two strings, the

  • cookie name and the cookie value as below.

Cookie cookie = new Cookie(“key”,”value”);

  • Cookie name and value must not contain white space or any of the following characters:

[ ] ( ) = , “ / ? @ : ;

 

  • Setting the maximum age: If you create a cookie and send it to the browser, by default it is a session-level

  • cookie: a cookie that is stored in the browser’s memory and deleted when the user quits the browser. If you want the browser to store the cookie on disk, use setMaxAge with a time in seconds, as below.

Cookie.setMaxAge(60*60*24*7); // One week

  • Sending the Cookie into the HTTP response headers: You use response.addCookie to add cookies in the HTTP response header as follows:

response.addCookie(cookie);


 

JSP program for creating a cookie

Sam Sir

<%--Createcookies.jsp--%>

<%--JSP program for creating a cookie--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Cookie Creation</title>

</head>

<body>

<%

Cookie cookie = new Cookie("url","cbtsam dot com");

cookie.setMaxAge(60*60); //1 hour

response.addCookie(cookie);

out.println("Cookies created");

%>

</body>

</html>

Output

cookies creation

 

Note: To check in Firefox browser that cookies is been created, click on tool àthan click on Options in options click on Privacy tab than in History group select Use custom setting for history from a dropdown box à than click on Show Cookies button which will display cookies information as shown below:

 

view cookies


 

 

JSP program to set a cookies in form processing parameters

Sam Sir

<%-- cookiesprocessing.jsp --%>

<%-- JSP program to set a cookies in form processing parameters --%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Cookies</title>

</head>

<body>

<center><h3>Setting Up Cookies</h3></center>

<%

Cookie firstName = new Cookie("first_name",request.getParameter("firstname"));

Cookie lastName = new Cookie("last_name",

request.getParameter("lastname"));

Cookie sex = new Cookie("sex",

request.getParameter("sex"));

Cookie email = new Cookie("email",

request.getParameter("email"));

 

firstName.setMaxAge(60*60*24);

lastName.setMaxAge(60*60*24);

 

response.addCookie( firstName );

response.addCookie( lastName );

response.addCookie(sex);

response.addCookie(email);

out.println("First Name : "+request.getParameter("firstname")+"<BR>");

out.println("Last Name : "+request.getParameter("lastname")+"<BR>");

out.println("Sex : "+request.getParameter("sex")+"<BR>");

out.println("Email : "+request.getParameter("email")+"<BR>");

%>

</body>

</html>

 

 

 


 

HTML form for user input which will be processed by cookiesprocessing.jsp

Sam Sir

<!-- index007.html -->

<html>

<head>

<title>

Setting cookies

</title>

</head>

<body>

<form action="cookiesprocessing.jsp" method="get">

<table>

<tr>

<td>First Name :</td>

<td><input type="text" name="firstname"></td>

</tr>

<tr>

<td>Last Name :</td>

<td><input type="text" name="lastname"></td>

</tr>

<tr>

<td>Email :</td>

<td><input type="text" name="email"></td>

</tr>

<tr>

<td><input type="radio" name="sex" value="Male">Male</td>

<td><input type="radio" name="sex" value="Female">Female</td>

</tr>

<tr>

<td><input type="submit" value="Send"></td>

<td><input type="reset"></td>

</tr>

</table>

</form>

</body>

</html>

Output

creating cookies in jsp

 

creating cookies in jsp

 

Note: To check in Firefox browser that cookies is been created, click on tool àthan click on Options in options click on Privacy tab than in History group select Use custom setting for history from a dropdown box à than click on Show Cookies button which will display cookies information as shown below:

 

Viewing cookies

Pr.Pg border                                              Next Pg