Working with Cookies and sessions


A session is an object associated with a visitor. Data can be put in the session and retrieved from it, much like a Hashtable. A different set of data is kept for each visitor to the site.
Here is a set of pages that put a user’s name in the session, and display it elsewhere. Try out installing and using these. First we have a form, let us call it GetName.html.

What’s your name? <INPUT TYPE=TEXT NAME=username SIZE=20>

The target of the form is “SaveName.jsp”, which saves the user’s name in the session. Note the variable “session”. This is another variable that is normally made available in JSPs, just like out and request variables. (In the @page directive, you can indicate that you do not need sessions, in which case the “session” variable will not be made available.)

String name = request.getParameter( “username” );
session.setAttribute( “theName”, name );
<A HREF=”NextPage.jsp”>Continue</A>

The SaveName.jsp saves the user’s name in the session, and puts a link to another page, NextPage.jsp. NextPage.jsp shows how to retrieve the saved name.

Hello, <%= session.getAttribute( “theName” ) %>

If you bring up two different browsers (not different windows of the same browser), or run two browsers from two different machines, you can put one name in one browser and another name in another browser, and both names will be kept track of.
The session is kept around until a timeout period. Then it is assumed the user is no longer visiting the site, and the session is discarded.

<% // Create cookies for first and last names. Cookie firstName = new Cookie(“first_name”, request.getParameter(“first_name”));

Cookie lastName = new Cookie(“last_name”, request.getParameter(“last_name”));

// Set expiry date after 24 Hrs for both the cookies. firstName.setMaxAge(60*60*24);


// Add both the cookies in the response header. response.addCookie( firstName ); response.addCookie( lastName ); %>



<title>Setting Cookies</title> </head>

<body> <center>

<h1>Setting Cookies</h1> </center>

<ul> <li><p><b>First Name:</b>
<%= request.getParameter(“first_name”)%>
<li><p><b>Last Name:</b>
<%= request.getParameter(“last_name”)%>

Put above code in main.jsp file and use it in the following HTML page:

<form action=”main.jsp” method=”GET”>
First Name: <input type=”text” name=”first_name”>
<br />
Last Name: <input type=”text” name=”last_name” />
<input type=”submit” value=”Submit” />

Try to enter First Name and Last Name and then click submit button. This would display first name and last name

on your screen and same time it would set two cookies firstName and lastName which would be passed back to
the server when next time you would press Submit button.

Delete Cookies with JSP

To delete cookies is very simple. If you want to delete a cookie then you simply need to follow up following three steps:

Read an already existing cookie and store it in Cookie object.

Set cookie age as zero using setMaxAge() method to delete an existing cookie.

Add this cookie back into response header.


Constructive and genuine appreciation and/or criticism most welcome

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s