Session Tracking in servlet
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.
Why use Session Tracking?
To recognize the user It is used to recognize the particular user. When
user making multiple request, server should know these all request have
come from the same user.
Session Tracking Techniques
There are four techniques used in Session tracking:
1. Cookies
2. Hidden Form Field
3. URL Rewriting
4. HttpSession
Follow the link for managing session using cookie
[Link]
cookies
Follow the link for managing session using hidden field
[Link]
Follow the link for managing session using URL rewriting
[Link]
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:
1. bind objects
2. view and manipulate information about a session, such as the session
identifier, creation time, and last accessed time.
How to get the HttpSession object ?
The HttpServletRequest interface provides two methods to get the object
of HttpSession:
1. public HttpSession getSession():Returns the current session
associated with this request, or if the request does not have a
session, creates one.
2. 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.
Commonly used methods of HttpSession interface
1. public String getId():Returns a string containing the unique
identifier value.
2. public long getCreationTime():Returns the time when this
session was created, measured in milliseconds since midnight
January 1, 1970 GMT.
3. public long getLastAccessedTime():Returns the last time the
client sent a request associated with this session, as the number of
milliseconds since midnight January 1, 1970 GMT.
4. public void invalidate():Invalidates this session then unbinds any
objects bound to it.
Examples:
[Link]
<form action="servlet1">
Name:<input type="text" name="userName"/><br/>
<input type="submit" value="go"/>
</form>
[Link]
import [Link].*;
import [Link].*;
import [Link].*;
public class FirstServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse respon
se){
try{
[Link]("text/html");
PrintWriter out = [Link]();
String n=[Link]("userName");
[Link]("Welcome "+n);
HttpSession session=[Link]();
[Link]("uname",n);
[Link]("<a href='servlet2'>visit</a>");
[Link]();
}catch(Exception e){[Link](e);}
}
}
[Link]
import [Link].*;
import [Link].*;
import [Link].*;
public class SecondServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse respon
se)
try{
[Link]("text/html");
PrintWriter out = [Link]();
HttpSession session=[Link](false);
String n=(String)[Link]("uname");
[Link]("Hello "+n);
[Link]();
}catch(Exception e){[Link](e);}
}
}
[Link]
<web-app>
<servlet>
<servlet-name>s1</servlet-name>
<servlet-class>FirstServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>s1</servlet-name>
<url-pattern>/servlet1</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>s2</servlet-name>
<servlet-class>SecondServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>s2</servlet-name>
<url-pattern>/servlet2</url-pattern>
</servlet-mapping>
</web-app>