7 JAVA MODULE6 Session Tracking
7 JAVA MODULE6 Session Tracking
MODULE-6
Session Tracking and Management in
Servlets
Session Tracking and Management
Session simply means a particular interval of time.
Session Tracking is a way to maintain state (data) of an
user. It is also known as session management in servlet.
Http protocol is a stateless so we need to maintain state
using session tracking techniques.
Each time user requests to the server, server treats the
request as the new request. So we need to maintain the state
of an user to recognize to particular user.
HTTP is stateless that means each request is considered as
the new request.
Session Tracking and Management
Session Tracking and Management
Session Tracking Techniques
There are four techniques used in Session tracking:
Cookies
Hidden Form Field
URL Rewriting
HttpSession
Session Tracking and Management
Cookies in Servlet
A cookie is a small piece of information that is persisted
between the multiple client requests.
A cookie has a name, a single value, and optional attributes such
as a comment, path and domain qualifiers, a maximum age, and
a version number.
How Cookie works
By default, each request is considered as a new request. In
cookies technique, we add cookie with response from the
servlet. So cookie is stored in the cache of the browser. After
that if request is sent by the user, cookie is added with request
by default. Thus, we recognize the user as the old user
Session Tracking and Management
Types of Cookie
• There are 2 types of cookies in servlets.
1. Non-persistent cookie
2. Persistent cookie
• Non-persistent cookie
• It is valid for single session only. It is removed each time when user
closes the browser.
• Persistent cookie
• It is valid for multiple session . It is not removed each time when user
closes the browser. It is removed only if user logout or signout.
Session Tracking and Management
//Create cookie
Cookie ck=new Cookie("user",“UUU");//creating cookie object
response.addCookie(ck);//adding cookie in the response
//Delete Cookie
Cookie ck=new Cookie("user","");//deleting value of cookie
ck.setMaxAge(0);//changing the maximum age to 0 seconds
response.addCookie(ck);//adding cookie in the response
//Get Cookie
Cookie ck[]=request.getCookies();
for(int i=0;i<ck.length;i++){
out.print("<br>"+ck[i].getName()+" "+ck[i].getValue());//printing name and value of c
ookie
}
Session Tracking and Management
Hidden Form Field
In case of hidden form field a hidden (invisible) textfield is used
for maintaining the state of an user.
In such case, we store the information in the hidden field and get it
from another servlet. This approach is better if we have to submit
form in all the pages and we don't want to depend on the browser.
<input type="hidden" name="uname" value=“UUU">
Here, uname is the hidden field name and UUU is the hidden field
value.
It is widely used in comment form of a website. In such case, we
store page id or page name in the hidden field so that each page can
be uniquely identified
request.getParameter("uname");
Session Tracking and Management
URL Rewriting
In URL rewriting, we append a token or identifier to the URL
of the next Servlet or the next resource. We can send parameter
name/value pairs using the following format:
url?name1=value1&name2=value2&??
A name and a value is separated using an equal = sign, a
parameter name/value pair is separated from another parameter
using the ampersand(&). When the user clicks the hyperlink,
the parameter name/value pairs will be passed to the server.
From a Servlet, we can use getParameter() method to obtain a
parameter value.
<a href='servlet2?uname="+n+"'>visit</a>
request.getParameter("uname");
Session Tracking and Management
HttpSession interface
Container creates a session id for each user.
The container uses this id to identify the particular user.
An object of HttpSession can be used to perform two
tasks:
Bind objects
View and manipulate information about a session, such as
the session identifier, creation time, and last accessed time.
Session Tracking and Management
The HttpServletRequest interface provides two methods to get the object of
HttpSession:
public HttpSession getSession():Returns the current session associated with this
request, or if the request does not have a session, creates one.
public HttpSession getSession(boolean create):Returns the current HttpSession
associated with this request or, if there is no current session and create is true,
returns a new session.
HttpSession session=request.getSession();
HttpSession session=request.getSession(false);
Session Tracking and Management
Example
Index.html
cookie1.java
cookie2.java
web.xml