Session Management in Servlet Using HttpSession

In this tutorial, we will learn another technique to manage the session. In the previous technique, we have learned session management using URL Rewriting Here we will learn the Fourth and last techniques HttpSession to manage the session. Before discussing this technique in details let's first understand about HttpSession.



What is HttpSession?

The HttpSession interface is implemented by the server. The servlet container uses this interface to create a session between the Http client and Http server. It enables a servlet to read and write the state information that is associated with an HttpSession.
Session Management using HttpSession


How to get a HttpSession Object?

There are 2 methods to get the HttpSession Object
1) public HttpSession getSession(): This method returns the current session associated with this request or if the request doesn't have any session it creates a new session,

2) public HttpSession getSession(boolean create): This method return the HttpSession object. It returns HttpSession object if a request has session otherwise it returns null.

How HttpSession work?


In the left side, there are two clients and in the center, there is a container (web server). The first client sends the request to web server, the web server creates the SessionId for the first client and this Session Id send back to the first client.  if the first client makes a further request for the second request the session id will be part of this request so the container can identify the particular user using the session id.
Servlet Using HttpSession


If the second client sends the request to a web server, the web server creates the sessionId for the second client and this Session Id send back to the second client. if the second client makes a further request for the second request the session id will be part of this request so the container can identify the particular user using the session id.

Methods of HttpSession interface:

1) public long getCreationTime(): 

It returns the time(in milliseconds since midnight,January 1,1970, GMT) when the session was created.

2) public String getId(): 
It returns the session Id.


3) public void invalidate():
 Invalidates this session and removes it from the context.


4) public boolean isNew(): 
Returns true if the server created the session and it has not yet been accessed by the client.


5) public void setAttribute(String str,object obj): 
Associated the value passed in obj with the attribute name passed in str.

6) public long getLastAccessedTime(): 
It returns the time(in milisecond since midnight,january 1,1970,GMT)when the client last made a request for this session.



HttpSession Example:

      index.html file:
Servlet Using HttpSession


HttpSessionServlet1.java file:
Servlet Using HttpSession



HttpSessionServlet2.java file:
Servlet Using HttpSession


web.xml file:
Servlet Using HttpSession




Servlet Using HttpSession



Servlet Using HttpSession



Servlet Using HttpSession




You may also like:

Session Management in Servlet Using HttpSession Session Management in Servlet Using HttpSession Reviewed by Prashant Srivastava on October 14, 2018 Rating: 5

No comments:

Powered by Blogger.