Servlet Login and Logout Using HttpSession Example

Here you will get servlet login and logout example using HttpSession.

There are various ways to maintain session but here I will use HttpSession class.

In this example we have a index.html page where a login form is displayed. When user enters login details and submits the form the request is sent to LoginServlet. If the details are correct then user is redirected to HomeServlet otherwise redirected to index.html.

LogoutServlet invalidates the sessions to logout the user and redirect to index.html.

If anyone tries to access Home page directly without doing login then he/she will be redirected to index.html.

In this example I have hard coded email and password but you can use database to compare login details. For doing successful login the email should be abc@gmail.com and password should be abc.

Also Read: Servlet Registration Form with MySQL Database Example

Servlet Login and Logout Using HttpSession Example

The project contains following files.

WebContent/index.html

<html>
    <head>
        <title>Login and Logout With Session Example</title>
    </head>
    
    <body>
        <form action="login" method="post">
            Email: <input type="email" name="email" required/><br/><br/>
            Password: <input type="password" name="pass" required/><br/><br/>
            <input type="submit" value="Login"/>
        </form>
    </body>
</html>

src/com/LoginServlet.java

package com;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  		
		String email = request.getParameter("email");
		String pass = request.getParameter("pass");
		
		if(email.equals("abc@gmail.com") && pass.equals("abc")) {
			HttpSession session = request.getSession();
			session.setAttribute("email", email);
			response.sendRedirect("home");
		}
		else {
			response.sendRedirect("index.html");
		}
	}
}

src/com/HomeServlet.java

package com;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet("/home")
public class HomeServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
		response.setContentType("text/html");
		PrintWriter pr = response.getWriter();

		HttpSession session = request.getSession(false);

		if(session != null) {
			String email = (String) session.getAttribute("email");
			pr.print("Welcome " + email);
			pr.print("<br/><a href=\"logout\">Logout</a>");
		}
		else {
			response.sendRedirect("index.html");
		}
		
		pr.close();
	}
}

src/com/LogoutServlet.java

package com;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet("/logout")
public class LogoutServlet extends HttpServlet{
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
		HttpSession session = request.getSession();
		session.invalidate();
		
		response.sendRedirect("index.html");		
	}
}

Screenshots

Servlet Login and Logout Using HttpSession Example

Servlet Login and Logout Using HttpSession Example

Comment below if you have doubts regarding above servlet login and logout example using HttpSession.

Leave a Comment

Your email address will not be published. Required fields are marked *