Servlet HttpSession Login Logout

In this tutorial you will learn about the Servlet HttpSession Login Logout and its application with practical example.

Servlet HttpSession Login Logout

In previous article we have gone through with methods of HttpSession interface. Also we have tried an example for that, here we are going to do login and logout with session. Let’s get understand more about HttpSession Interface.
We need to create some html, servlet classes for this, get understand about them one by one.

index.html

This will be our Welcome page, here we keep three links first one will be for Login, second Logout and last one will be Profile, like below code.

link_session.html

This is our link page, suppose we clicked directly on profile without login so this page will show us or even we did logout. Check below code.

login_session.html

This will call from index.html file or link_session.html file, when user will click on Login button.

LoginSession.java

This is our Login file, here I have taken “w3adda1234” as a password, if password matched so it stores the attribute into the session. Check below code.

 

ProfileSession.java

With the help of session.getAttribute(“name”) we will get the value of attribute which is stored in the session. Check below code.

LogoutSession.java

From here we will destroy session. Check below code.

Output:-

As we are done with code, so let’s run this and get output like below images.

Screen 1:-

image001

Screen 2:-

image003

Screen 3:-

image005

Screen 4:-

image007

Screen 5:-

image009

sdfsdf

In this tutorial we have learn about the Servlet HttpSession Login Logout and its application with practical example. I hope you will like this tutorial.