Javatpoint Logo

91-9990449935

 0120-4256464

Servlet HttpSession Login and Logout Example

We can bind the objects on HttpSession instance and get the objects by using setAttribute and getAttribute methods.

In the previous page, we have learnt about what is HttpSession, How to store and get data from session object etc.

Here, we are going to create a real world login and logout application without using database code. We are assuming that password is admin123.


Visit here for login and logout application using cookies only servlet login and logout example using cookies


In this example, we are creating 3 links: login, logout and profile. User can't go to profile page until he/she is logged in. If user is logged out, he need to login again to visit profile.

In this application, we have created following files.

  1. index.html
  2. link.html
  3. login.html
  4. LoginServlet.java
  5. LogoutServlet.java
  6. ProfileServlet.java
  7. web.xml
File: index.html

File: link.html

File: login.html

File: LoginServlet.java

File: LogoutServlet.java

File: ProfileServlet.java

File: web.xml

Output

servlet http session login example 1 servlet http session example 2 servlet http session example 3 servlet http session example 4 servlet http session example 5 servlet http session example 6

If again you click on the profile link, you need to login first.