School of Computing
Department of Computer Science and Engineering
ACADEMIC YEAR 2024 – 2025 (Summer Semester)
10211CS204 – OBJECT ORIENTED PROGRAMMING USING JAVA
USE CASE
Validated Login for Employee management JSP
TEAM MEMBERS :
VTU19251 DHANUSH SRIRAM S
VTU19838 NITHISH KUMAR K
VTU21406 MANISH KUMAR S
VTU21423 ABRAHAM WILKINSON H
VTU21434 HARI KRISHNAN M
VTU21441 BHUMMIREDDY KAMBAIAHGARI JAYANTH
VTU21524 SAAQIB A
USECASE JAVA
Validated Login for Employee management JSP
AIM:-
The aim of this use case is to create a validated login system for an Employee Management
System using JSP (Java Server Pages). The system will allow employees to securely log in to
access their personalized dashboards. The login process will include validation of user
credentials (username and password) stored in a database. Only authorized employees with
valid credentials can access the system.
ALGORITHM:-
1. Start
2. Collect input:
Employee enters username and password in the login form on the JSP page.
3. Establish a database connection:
Using JDBC (Java Database Connectivity), connect to the database where
employee login credentials are stored.
4. Query the database:
Create an SQL query to check if the entered username exists in the employee
database.
5. Validation:
If the username exists, retrieve the corresponding password from the database
and compare it with the password entered by the employee.
If the password matches, proceed to the next step. Otherwise, return an error
message (e.g., "Invalid username or password").
6. Login Success/Failure:
If validation is successful, the employee is redirected to their dashboard.
If validation fails, the employee is redirected back to the login page with an
error message.
7. End
PROGRAM
<%@ page import="java.sql.*" %>
<html>
<head>
<title>Employee Login</title>
</head>
<body>
<h2>Login</h2>
<form action="LoginServlet" method="POST">
<label>Username:</label>
<input type="text" name="username" required /><br/>
<label>Password:</label>
<input type="password" name="password" required /><br/>
<input type="submit" value="Login" />
</form>
</body>
</html>
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String username = request.getParameter("username");
String password = request.getParameter("password");
try {
// Step 3: Establish database connection
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/employeeDB", "root",
"password");
// Step 4: Query the database
PreparedStatement ps = con.prepareStatement("SELECT * FROM employees
WHERE username=?");
ps.setString(1, username);
ResultSet rs = ps.executeQuery();
// Step 5: Validate credentials
if (rs.next()) {
String dbPassword = rs.getString("password");
if (password.equals(dbPassword)) {
out.println("<h2>Login Successful! Welcome, " + username + "</h2>");
// Redirect to employee dashboard (e.g., employeeDashboard.jsp)
} else {
out.println("<h3>Invalid Username or Password</h3>");
// Redirect back to login page
} else {
out.println("<h3>User not found!</h3>");
ps.close();
con.close();
} catch (Exception e) {
out.println("Error: " + e.getMessage());
CREATE TABLE employees (
id INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(50) UNIQUE NOT NULL,
password VARCHAR(50) NOT NULL
);
OUTPUT
Successful Login:
If the user enters valid credentials, they will see a "Login Successful" message and be
redirected to their dashboard.
Input:
Username: john_doe
Password: password123
Failed Login:
If the credentials are incorrect, the user will see an "Invalid Username or Password" message
and will be prompted to try again.
Input:
Username: john_doe
Password: password123