Step 1: Setting Up the Development Environment
Ensure you have the following tools installed:
- JDK (Java Development Kit) - preferably the latest version.
- Apache Tomcat - latest version.
- MySQL - latest version.
- IDE (Integrated Development Environment) - Eclipse, IntelliJ IDEA, or any other Java IDE.
- Maven - for managing project dependencies.
Step 2: Create a Maven Project
- Open your IDE and create a new Maven project.
- Update the
pom.xml
file to include the necessary dependencies.
<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 http://www.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>login-webapp</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>6.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jakarta.servlet.jsp</groupId>
<artifactId>jakarta.servlet.jsp-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jakarta.servlet.jsp.jstl</groupId>
<artifactId>jakarta.servlet.jsp.jstl-api</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>jakarta.servlet.jsp.jstl</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
</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>
Step 3: Directory Structure
Ensure your project structure looks like this:
login-webapp/
├── src/
│ ├── main/
│ │ ├── java/
│ │ │ └── com/
│ │ │ └── example/
│ │ │ ├── controller/
│ │ │ │ ├── LoginServlet.java
│ │ │ ├── dao/
│ │ │ │ ├── UserDAO.java
│ │ │ ├── model/
│ │ │ │ └── User.java
│ │ │ └── util/
│ │ │ └── DBUtil.java
│ │ ├── resources/
│ │ └── webapp/
│ │ ├── WEB-INF/
│ │ │ └── lib/
│ │ ├── index.jsp
│ │ ├── login.jsp
│ │ └── welcome.jsp
└── pom.xml
Step 4: Create the Database
Create a MySQL database and a users
table.
CREATE DATABASE userdb;
USE userdb;
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL,
password VARCHAR(50) NOT NULL,
email VARCHAR(50) NOT NULL
);
Step 5: Create Model Class
Create a model class User.java
in the com.example.model
package.
package com.example.model;
public class User {
private int id;
private String username;
private String password;
private String email;
// Getters and Setters
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
Step 6: Create a DAO Class
Create a DAO class UserDAO.java
in the com.example.dao
package.
package com.example.dao;
import com.example.model.User;
import com.example.util.DBUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class UserDAO {
public boolean validate(User user) throws ClassNotFoundException {
boolean status = false;
Class.forName("com.mysql.cj.jdbc.Driver");
try (Connection connection = DBUtil.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM users WHERE username = ? AND password = ?")) {
preparedStatement.setString(1, user.getUsername());
preparedStatement.setString(2, user.getPassword());
ResultSet rs = preparedStatement.executeQuery();
status = rs.next();
} catch (SQLException e) {
e.printStackTrace();
}
return status;
}
}
Step 7: Create Utility Class
Create a utility class DBUtil.java
in the com.example.util
package.
package com.example.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBUtil {
private static final String JDBC_URL = "jdbc:mysql://localhost:3306/userdb";
private static final String JDBC_USERNAME = "root";
private static final String JDBC_PASSWORD = "root";
public static Connection getConnection() throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.cj.jdbc.Driver");
return DriverManager.getConnection(JDBC_URL, JDBC_USERNAME, JDBC_PASSWORD);
}
}
Step 8: Create Controller Class
Create a controller class LoginServlet.java
in the com.example.controller
package.
package com.example.controller;
import com.example.dao.UserDAO;
import com.example.model.User;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private UserDAO userDAO;
public void init() {
userDAO = new UserDAO();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
User user = new User();
user.setUsername(username);
user.setPassword(password);
try {
if (userDAO.validate(user)) {
RequestDispatcher dispatcher = request.getRequestDispatcher("welcome.jsp");
dispatcher.forward(request, response);
} else {
response.sendRedirect("login.jsp");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Step 9: Create JSP Pages
Create a login form login.jsp
in the src/main/webapp
directory.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login Form</title>
</head>
<body>
<h2>Login Form</h2>
<form action="login" method="post">
<label>Username:</label>
<input type="text" name="username" required><br><br>
<label>Password:</label>
<input type="password" name="password" required><br><br>
<input type="submit" value="Login">
</form>
</body>
</html>
Create a welcome page welcome.jsp
in the src/main/webapp
directory.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Welcome</title>
</head>
<body>
<h2>Welcome, ${username}!</h2>
<a href="login.jsp">Logout</a>
</body>
</html>
Step 10: Run the Application
- Build the Maven project using
mvn clean install
. - Deploy the
login-webapp.war
file to Apache Tomcat. - Start the Tomcat server
and navigate to http://localhost:8080/login-webapp/login.jsp
.
You should see the login form. If the credentials are correct, you will be redirected to the welcome page upon submitting the form.
Conclusion
In this tutorial, we created a JSP Login Form that connects to a MySQL database using JDBC. We used the latest versions of JSP and Servlet and followed a structured approach to create a simple yet functional login application.
Comments
Post a Comment
Leave Comment