![]() |
VOOZH | about |
Servlets are server-side Java programs used to handle client requests and generate dynamic responses. Since HTTP is a stateless protocol, the server does not remember previous client requests. Session Tracking is used to maintain and manage user-specific data across multiple requests so that web applications can remember user activities during a session.
HTTP is a stateless protocol, which means the server does not remember previous requests made by the client. Because of this limitation, session tracking is required to maintain continuity between client-server interactions.
Servlet provides four different techniques for session tracking.
Cookies are small pieces of data stored in the browser. The server sends cookies in the response, and the browser sends them back with future requests.
Syntax:
Set-Cookie: user=GFG
Hidden form fields store session information inside HTML forms using invisible input fields.
Syntax:
<input type="hidden" name="session" value="12345">
In URL rewriting, additional session data is appended to the URL as request parameters.
Syntax:
http://localhost:8080/app?sessionid=12345
HttpSession is the most commonly used session tracking technique in Servlets. It stores user-specific data on the server side and associates it with a unique session ID.
Syntax:
HttpSession session = request.getSession();
session.setAttribute("username", "GFG");
| Method | Description |
|---|---|
| getAttribute(String name) | Returns the object associated with the specified name |
| setAttribute(String name, Object value) | Stores an object in the session |
| removeAttribute(String name) | Removes a specific attribute from the session |
| invalidate() | Invalidates the entire session |
| getId() | Returns unique session ID |
| getCreationTime() | Returns session creation time |
| getLastAccessedTime() | Returns last accessed time of session |
| isNew() | Checks whether session is newly created |
| setMaxInactiveInterval(int interval) | Sets session timeout interval |
| getMaxInactiveInterval() | Returns session timeout interval |
Removes a particular attribute stored inside the current session.
session.removeAttribute("username");
Destroys the complete session and removes all associated session data.
session.invalidate();
Sets the maximum inactive time interval for the session in seconds.
session.setMaxInactiveInterval(1200);
Defines the default session timeout duration for the entire web application.
<session-config>
<session-timeout>20</session-timeout>
</session-config>
Follow the below steps to implement Session Tracking using HttpSession in Servlet. we will create a session using the HttpSession object and track user visit information such as session creation time, last accessed time, and number of visits.
Create a Dynamic Web Project in Eclipse.
Create GfgSession.java to create and manage the user session using the HttpSession object. This servlet stores session-related information such as session ID, creation time, last accessed time, and visit count, then displays the details in the browser response.
Configure the servlet mapping inside the web.xml file so that the server can identify the servlet class and map it with the specified URL pattern.
Run the project on the server using:
Run As -> Run on Server
if your IDE directly open the browser then the output is visible to us if not then Execute the following URL:
http://localhost:8080/SessionTrackingGfg/GfgSession
Output:
👁 ImageIf we try to run the same servlet again, we will get the following result.
👁 ImageExplanation: The output displays the session details such as session ID, creation time, last accessed time, user ID, and visit count using the HttpSession object.