Coders Packet

Login and logout using HTTP sessions in Java

By Kethireddy Chandra Karan Reddy

A simple application having login, logout, and profile pages are implemented using HTTP sessions and servlets in Java.

This project code aims to build a small web application to implement login and logout using sessions. Three servlets 'Validator', 'Logout', and 'Profile' are used in this project. A web server is required to run the code. Tomcat server is used in this project according to the system specifications. The home page of the application is provided by the 'link.html' file showing three hyperlinks 'login', 'logout', and 'profile'.

home page 

Upon clicking the 'Login' link, it will invoke 'welcome.html' to take login details from the user and send it to the 'Validator' servlet for validating the user details. In this project, any username with a password as 'password' is considered a valid login entry and thus permitted to the application. 'Validator' servlet creates an HTTP session object to track the user.


login success

A user can view the profile only if logged in. 'Profile' servlet checks whether any session exists to know if the user has logged in. If any session is found, the profile of the user is displayed or else it will direct to a login page asking the user to login first.


Accessing profile without logging in results as below.

invalid profile access

Another hyperlink provided in the application is 'Logout' to log out from the application. Clicking on 'Logout' invokes the 'Logout' servlet which invalidates any sessions created before by the user and deletes user track data. 'Session. invalidate()' is the method used to invalidate the session.


Steps to run the code:

Install the webserver(Tomcat here) and create a folder project folder inside the web apps folder on your server.

Under your project folder, insert the HTML files like 'link.html' and 'welcome.html'. Create a folder inside the project folder and name it WEB-INF.

Under WEB-INF, add two folders 'classes' and 'bin'. 

Create three Java files '', '', and '' and place them in the 'classes' folder.

Insert 'service-api.jar' file inside the bin folder of the project. Compile the three Java files.

Create a web.xml file inside WEB-INF in which servlets are linked with the classes and URL patterns.

Run the server and give the URL to the 'link.html' to run the web application. (Ex:https://localhost:8080/project_name/link.html ) and the application will be opened.


Download Complete Code


No comments yet