![]() |
VOOZH | about |
Managing user sessions is an essential part of web applications to track user state across multiple requests. In Java Servlet-based applications, the HttpSession interface provides an easy and effective way to store session data such as user login status. This example demonstrates a simple login and logout implementation using HttpSession.
The HttpSession interface is used in Java Servlets to maintain user-specific data across multiple requests during a session. It allows storing and accessing user information on the server side.
A session is created after user login to maintain user-specific data across multiple requests. In Servlets. The HttpServletRequest provides various methods to create or access a session.
Creates a new session if it does not exist, otherwise returns the existing session.
Syntax:
HttpSession session = request.getSession();
Creates a new session if no session exists, otherwise returns the current session.
Syntax:
HttpSession session = request.getSession(true);
Returns the existing session if available, otherwise returns null without creating a new session.
Syntax:
HttpSession session = request.getSession(false);
Once the user requests to logout, the session must be destroyed to remove all stored user data. This is done using the invalidate() method of the HttpSession interface.
This method destroys the current session and removes all objects bound to it, effectively logging out the user.
Syntax:
HttpSession session = request.getSession();
session.invalidate();
When this invalidate method is called on the session, it removes all the objects that are bound to that session.
Removes a specific attribute from the session (not the whole session), so other session data may still remain.
Syntax:
session.removeAttribute("user");
Sets session timeout in seconds. If set to 0 or a small value, the session will expire automatically after inactivity.
Syntax:
session.setMaxInactiveInterval(0);
We will create a basic Servlet program to display a welcome message for the validated users.
Your project directory is looks like after creation of Dynamic Project in your Eclipse IDE.
Create login.jsp under the WebContent folder. This page accepts username and password from the user.
Create LoginServlet under src folder Handles login request and creates session
Explanation: This servlet handles user login by validating the password and creating an HttpSession for a successful login. It then redirects the user to the welcome page or shows an error message if the login fails.
Create welcome.jsp under the WebContent folder to display the welcome message and logout button.
Handles logout request and Destroys session using invalidate()
Explanation: This servlet destroys the current session using invalidate() and logs the user out of the application.
http://localhost:8080/Servlet_LoginLogout/login.jsp
Output:
Your Login page will be disappear on your screen.
Enter the user name and password and click on Login.
Give the Password as "geek" as we are validating against it, if not it throws an error like below.
Enter the correct credentials and log in.
The User name which we set in the session object is displayed with a welcome message. Click on Logout.
Now, if you check the console, it prints the session object values.
Explanation: