Implementation of Login and Logout using Http Session in JSP

Divya Prakash
5 min readNov 28, 2020

--

we will implement the login and logout using HTTP Session using JSP here.

Before diving deep, let us first give a look on what JSP is and what are its benefits?

“JSP is a server side technology that does all the processing at server. It is used for creating dynamic web applications, using java as programming language.

Benefits of JSP:-

Java Server Pages often serve the same purpose as programs implemented using the Common Gateway Interface (CGI). But JSP offers several advantages in comparison with the CGI. The advantages of JSP are as follows:

· Performance is significantly better because JSP allows embedding Dynamic Elements in HTML Pages itself instead of having separate CGI files.

· JSP are always compiled before they are processed by the server unlike CGI/Perl which requires the server to load an interpreter and the target script each time the page is requested.

· JavaServer Pages are built on top of the Java Servlets API, so like Servlets, JSP also has access to all the powerful Enterprise Java APIs, including JDBC, JNDI, EJB, JAXP, etc.

· JSP pages can be used in combination with servlets that handle the business logic, the model supported by Java servlet template engines.

Alright, lets have a look on HttpSession:-

“In web terminology, a session is simply the limited interval of time in which two systems communicate with each other. The session object is used to track a client session between client requests.”

JSP makes use of the servlet provided HttpSession Interface. This interface provides a way to identify a user across.

• a one-page request or

• visit to a website or

• store information about that user

• By default, JSPs have session tracking enabled and a new HttpSession object is instantiated for each new client automatically.

  • The JSP engine exposes the HttpSession object to the JSP author through the implicit session object. Since the session object is already provided to the JSP programmer, the programmer can immediately begin storing and retrieving data from the object without any initialization or getSession().

Setting up our project

What we need as our setup?

Nothing more, except these two :-

o Eclipse IDE (Eclipse IDE for Enterprise Java Developers)

o Apache Tomcat (for setting and starting up server)

Steps to create new project:-

1. Open Eclipse IDE.

2. Click on File -> New -> Dynamic Web Project. Following dialog box will appear.

3. We enter “jsplogin”(changeable) as our project name and select Apache Tomcat as our runtime.

4. Leaving all other settings to default and clicking on finish, we are ready with our project setup.

Folder Structure and files to edit:-

o For creating our project, our project needs some java files(for servlet), our webpages which will be served, and XML for servlet mapping and other informations.

o Our JSP files will be stored in a WebContent folder.

o After doing this, we will get our project directory something like this:-

Our files will be:-

1. index.jsp :- Homepage

2. login.jsp:- Login page

3. login-check.jsp:- Login validation and create session

4. lock.jsp:- Page which show current state of authentication

5. logout.jsp:- Logout page

6. style.css:- For Styling

Done with the setup part, we will proceed to coding:-

  1. index.jsp:- Homepage

2. login.jsp:- Login Page

3. login-check.jsp:-

Validates credentials and create Session.

4. lock.jsp:-

HTML to be sent when user checks his vault and he is authenticated.

5. logout.jsp:-

6. style.css:-

Running our Project

Done with all the coding, now its time to check our code running:-

To run the project in eclipse IDE we follow these steps:-

1. Select your project by clicked on its name directory in projects panel.

2. Click on Run -> Run as -> Run on Server.

3. Here, we will select Tomcat server, then click on next.

4. Here, our project will already be added in configured state, we can do this, if not done so by clicking on button.

5. Click on Finish.

Our project will now being served on the localhost server on defined port ( by default 8080).

Exploring our Project in web browser

  1. Open any web browser and hit to url http://localhost:8080/jsplogin.We can see our homepage (index.html) is served.

2. We can check the current status of the vault by clicking on “Check your vault” button. We can see it shows vault in locked state.

3. Click on return to home and then click on “Log In” button. Here we will enter our username (divyaprakash) and password (rangerx) to open our vault, which if we enter incorrect prompts us to enter correct one.

4. After providing right credentials, we are served page in which vault is open.

5. The vault will be open for the time until we close the vault or log out, which we can verify by returning to home and again checking the current vault state.

6. Now, if we log out or close our vault, our vault state changes to close state until we again log in.

7. As our all steps are complete, we can switch off our server by navigating to Window -> Show View -> Servers. Here right click on running server and click on stop.

In this way, we have successfully implemented login and logout with the help of JSP.

--

--

Divya Prakash
Divya Prakash

Written by Divya Prakash

Experienced Software Developer | NextJS | GatsbyJS | Flutter

No responses yet