Login Example with JSP/Servlet
In this tutorial, we will build a web application using jsp and servlet which allows users to login.
Create a maven project with following dependencies.


provide group id and artifact id information

Once, the project is created, right click onto it and click on the properties option as shown below

In the properties of the project, check the below ‘Dynamic Web Module’ to convert this maven project into a web project. Then click ‘Apply’ and ‘Apply and Close’.

After that your project should look like this. It should contain webapp folder as shown below

After that, click on the project properties and click the following option to generate web.xml file

After this step, you must be able to see web.xml file under WEB-INF folder

pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sks</groupId>
<artifactId>login-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>5.0.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.1</version>
</plugin>
</plugins>
</build>
</project>
Do a Run as -> maven install as shown below

Build should be successful as below

Make sure you’re using java version 1.8 for this example

Create login.jsp under webapp folder
<!DOCTYPE html>
<html>
<head>
<title>Login Page</title>
</head>
<body>
<h2>Login Form</h2>
<form action="login" method="post">
Username: <input type="text" name="username" /><br />
Password: <input type="password" name="password" /><br />
<input type="submit" value="Login" />
</form>
</body>
</html>
Create LoginServlet.java file
package com.sks;
import java.io.IOException;
import java.io.PrintWriter;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
if ("admin".equals(username) && "admin".equals(password)) {
out.println("<h2>Login Successful!</h2>");
} else {
out.println("<h2>Login Failed. Invalid username or password.</h2>");
}
}
}
Deploy application on Tomcat version 10.1
Follow the link if you don’t know how to configure tomcat for your eclipse/STS
Test the application
Hit the following url –
http://localhost:8080/login-example/login.jsp

