Lecture 31
Lecture 31
Lecture 31
Session Tracking
We have discussed the importance of session tracking in the previous handout. Now, we’ll
discover the basic techniques used for session tracking. Cookies are one of these techniques
and remain our focus in this handout. Cookies can be used to put small information on the
client’s machine and can be used for various other purposes besides session tracking. An
example of simple “Online Book Store”, using cookies, will also be surveyed.
Storing state on server side makes server really complicated as states needed to be stored for
each client. Some one can imagine how much space and processing is required in this
scenario as some web servers are hit more than hundred times in a second. E.g. Google,
Yahoo etc.
What if states are stored on client side in order to maintain a conversation? Do all the clients
permit you doing that? What if client (user) wiped out these states from the machine?
Concluding this discussion, state is stored neither completely on server side nor on client.
States are maintained by the mutual cooperation of both client & server. Generally modern
servers give the capability to store state on the server side and some information (e.g. client
ID/state ID) passed from the client will relate each client with its corresponding state.
Post–Notes
In order to maintain the conversational state, server puts little notes (some text, values etc) on
the client slide. When client submits the next form, it also unknowingly submits these little
notes. Server reads these notes and able to recall who the client is.
1. Cookies
2. URL Rewriting
3. Hidden Fields
- 377 -
Handout 31
Web Design & Development CS-506
Cookies
What a cookie is?
Don’t be tempted? These are not, what you might be thinking off. In fact, in computer
terminology, “a cookie is a piece of text that a web server can store on a client’s (user) hard
disk”.
Cookies allow the web sites to store information on a client machine and later retrieve it. The
pieces of information are stored as name-value pair on the client. Later while reconnecting to
the same site (or same domain depending upon the cookie settings), client returns the same
name-value pair to the server.
Cookie’s Voyage
To reveal the mechanism of cookies, let’s take an example. We are assuming here that the
web application we are using will set some cookies
If you type URL of a Web site into your browser, your browser sends a request for
that web page
− For example, when you type www.amazon.com a request is send to the Amazon’s
server
Before sending a request, browser looks for cookie files that amazon has set
− If browser finds one or more cookie files related to amazon, it will send it along
with the request
− If not, no cookie data will be sent with the request
Amazaon web server receives the request and examines the request for cookies
− If cookies are received, amazon can use them
− If no cookie is received, amazon knows that you have not visited before or the
cookies that were previously set got expired.
− Server creates a new cookie and send to your browser in the header of HTTP
Response so that it can be saved on the client machine.
- 378 -
Handout 31
Web Design & Development CS-506
A cookies by default, lasts only for the current user session (i.e. until the user quits
the session) and will not be stored on the disk.
After making changes to cookie attributes, the most important and unforgettable step
is to add this currently created cookie into response. If you forget this step, no cookie
will be sent to the browser.
response.addCookie(c);
- 379 -
Handout 31
Web Design & Development CS-506
To read the cookies that come back from the client, following steps are generally followed.
Once you have an array of cookies, you can iterate over it. Two important methods of
Cookie class are getName() & getValue(). These are used to retrieve cookie
name and value respectively.
*/
}
} // end for
- 380 -
Handout 31
Web Design & Development CS-506
In the example below, servlet checks for a unique cookie, named “repeat”. If the cookie is
present, servlet displays “Welcome Back”. Absence of cookie indicates that the user is
visiting this site for the first time thus servlet displays a message “Welcome Aboard”.
This example contains only one servlet “RepeatVisitorServlet.java” and its code
is given below. A code snippet of web.xml is also accompanied.
Note: As a reminder, all these examples are built using netBeans4.1. This IDE will write
web.xml for you. However, here it is given for your reference purpose only, or for those
which are not using any IDE to strengthen their concepts
RepeatVisitorServlet.java
import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
// writing html
out.println("<html>");
out.println("<body>");
- 381 -
Handout 31
Web Design & Development CS-506
// reading cookies
Cookie[] cookies = request.getCookies();
out.println("</body>");
out.println("</html>");
out.close();
}
}// end RepeatVisitorServlet
- 382 -
Handout 31
Web Design & Development CS-506
web.xml
<web-app>
<servlet>
<servlet-name> RepeatVisitorServlet </servlet-name>
<servlet-class> RepeatVisitorServlet </servlet-class>
</servlet>
<servlet-mapping>
<servlet-name> RepeatVisitorServlet </servlet-name>
<url-pattern> /repeatexample </url-pattern>
</servlet-mapping>
</web-app>
Output
On first time visiting this URL, an output similar to the one given below would be displayed
On refreshing this page or revisiting it within an hour (since the age of cookie was set to 60
mins), following output should be expected.
- 383 -
Handout 31
Web Design & Development CS-506
Three books will be displayed to the user along with check boxes. User can select any check
box to add the book in the shopping cart. The heart of the application is, it remembers the
books previously selected by the user.
The following figure will help you understand the theme of this example. Books displayed
under the heading of “You have selected the following books” were added to cart one after
another. The important thing is server that remembers the previously added books by the
same user and thus maintains the session. Session management is accomplished using
cookies.
- 384 -
Handout 31
Web Design & Development CS-506
What’s the part of cookies? Cookie (named JSESSIONID, with unique value) is used to keep
the unique sessionID associated with each user. This sessionID is passed back and forth
between user and the server and is used to retrieve the HashMap (sessionInfo) of the user
from the global HashMap at the server. It should be noted here that, HashMaps of individual
users are stored in a global HashMap against a sessionID.
ShoppingCartServlet.java
import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
- 385 -
Handout 31
Web Design & Development CS-506
response.setContentType("text/html;charset=UTF-8");
} else {
out.println("<html>");
out.println("<head>");
out.println("<title>Shooping Cart Example</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Online Book Store</h1>");
String url =
"http://localhost:8084/cookiesessionex/shoppingcartex";
- 386 -
Handout 31
Web Design & Development CS-506
out.println("<br/>");
out.println("<h1>You have selected followig books</h1>");
out.println("<br/>");
out.println("</body>");
out.println("</html>");
out.close();
} // end processRequest()
- 387 -
Handout 31
Web Design & Development CS-506
Cookie c = cookies[i];
if (c.getName().equals("JSESSIONID")){
// doSomethingWith cookie
return c;
}
}
}
return null;
}
title= (String)sessionInfo.get("firstCB");
if (title != null){
out.println("<h3> "+ title +"</h3>");
}
title= (String)sessionInfo.get("secondCB");
if (title != null){
out.println("<h3> "+ title +"</h3>");
}
title= (String)sessionInfo.get("thirdCB");
if (title != null){
out.println("<h3> "+ title +"</h3>");
}
}
- 388-
Handout 31
Web Design & Development CS-506
} // end ShoppingCartServlet
- 389 -
Handout 31
Web Design & Development CS-506
web.xml
<web-app>
<servlet>
<servlet-name> ShoppingCart </servlet-name>
<servlet-class> ShoppingCartServlet </servlet-class>
</servlet>
<servlet-mapping>
<servlet-name> ShoppingCart </servlet-name>
<url-pattern> /shoppingcartex </url-pattern>
</servlet-mapping>
</web-app>
-------------
- 390 -
Handout 31
Web Design & Development CS-506
References:
- 391 -