Building a Login and Logout System with Java Servlets

Codes With Pankaj
2 min readOct 16, 2023

--

Creating a basic login and logout system using Java Servlets. This system will allow users to log in, access protected resources, and log out when they are done.

Prerequisites

Before we begin, make sure you have the following prerequisites in place:

  1. Java Development Kit (JDK): Ensure you have Java installed on your system.
  2. Servlet Container: We will use Apache Tomcat in this example. Download and set up Tomcat on your system if you haven’t already.
  3. IDE: You can use an Integrated Development Environment (IDE) like Eclipse, IntelliJ IDEA, or any other of your choice.

Project Structure

Let’s start by creating the project structure:

|-- src
| |-- com
| |-- codeswithpankaj
| |-- servlets
| |-- LoginServlet.java
| |-- LogoutServlet.java
|-- WebContent
| |-- WEB-INF
| |-- web.xml
| |-- login.html
| |-- home.html

Step 1: Create the HTML Pages

login.html

<!DOCTYPE html>
<html>
<head>
<title>Login Page</title>
</head>
<body>
<form action="LoginServlet" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required><br><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required><br><br>
<input type="submit" value="Login">
</form>
</body>
</html>

home.html

<!DOCTYPE html>
<html>
<head>
<title>Home Page</title>
</head>
<body>
<h1>Welcome to the Home Page</h1>
<form action="LogoutServlet" method="post">
<input type="submit" value="Logout">
</form>
</body>
</html>

Step 2: Create Servlets

LoginServlet.java

package com.yourdomain.servlets;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
// You should validate the username and password here
// For simplicity, we'll assume a hardcoded valid username and password
if (username.equals("yourusername") && password.equals("yourpassword")) {
HttpSession session = request.getSession();
session.setAttribute("username", username);
response.sendRedirect("home.html");
} else {
response.sendRedirect("login.html");
}
}
}

LogoutServlet.java

package com.yourdomain.servlets;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
public class LogoutServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession(false);
if (session != null) {
session.invalidate();
}
response.sendRedirect("login.html");
}
}

Step 3: Configure web.xml

In the web.xml file located in the WEB-INF directory, configure your servlets as follows:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.yourdomain.servlets.LoginServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>LogoutServlet</servlet-name>
<servlet-class>com.yourdomain.servlets.LogoutServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/LoginServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>LogoutServlet</servlet-name>
<url-pattern>/LogoutServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>login.html</welcome-file>
</welcome-file-list>
</web-app>

Step 4: Deploy and Run

Deploy your web application to your Servlet container (e.g., Tomcat) and access it through the browser. Start with the login.html page, and you should be able to log in with the provided username and password. After logging in, you'll be directed to the home.html page, where you can log out.

Congratulations! You’ve created a simple login and logout system using Java Servlets. You can enhance this system by adding a database for user authentication and more advanced features.

--

--

Codes With Pankaj
Codes With Pankaj

No responses yet