To create a simple JSP login page that validates the username and password, and redirects to a welcome page or shows an error message on the same login page if the credentials are wrong, you need to follow these steps:
- Create the login form (login.jsp)
- Create the login validation logic (validateLogin.jsp)
- Create the welcome page (welcome.jsp)
Step 1: Create the Login Form (login.jsp)
This file contains the login form where users enter their username and password.
<!DOCTYPE html>
<html>
<head>
<title>Login Page</title>
</head>
<body>
<h2>Login Page</h2>
<form action="validateLogin.jsp" 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>
<%
String errorMessage = (String) request.getAttribute("errorMessage");
if (errorMessage != null) {
%>
<p style="color:red;"><%= errorMessage %></p>
<%
}
%>
</body>
</html>
Step 2: Create the Login Validation Logic (validateLogin.jsp)
This file handles the login logic by checking if the username and password are correct.
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
String username = request.getParameter("username");
String password = request.getParameter("password");
if ("admin".equals(username) && "admin".equals(password)) {
// Correct username and password
response.sendRedirect("welcome.jsp");
} else {
// Incorrect username or password
request.setAttribute("errorMessage", "Invalid username or password");
request.getRequestDispatcher("login.jsp").forward(request, response);
}
%>
Step 3: Create the Welcome Page (welcome.jsp)
This file is the welcome page that users see after logging in successfully.
<!DOCTYPE html>
<html>
<head>
<title>Welcome Page</title>
</head>
<body>
<h2>Welcome, Admin!</h2>
<p>You have successfully logged in.</p>
</body>
</html>
Explanation:
- login.jsp: This page displays a login form where users can enter their username and password. If the login fails, an error message is shown.
- validateLogin.jsp: This script retrieves the username and password from the form, checks if they match the correct credentials (
admin
for both username and password), and redirects the user towelcome.jsp
if they match. If they do not match, it sets an error message and forwards the request back tologin.jsp
. - welcome.jsp: This is the page users see after a successful login.
Deployment:
Ensure that your JSP files are placed in the appropriate directory (typically the webapp
directory) and your web server (such as Apache Tomcat) is properly configured to serve JSP pages. You can deploy these JSP files to your server and access the login page via your web browser.
Testing:
- Access
login.jsp
via your web browser. - Enter
admin
as the username andadmin
as the password. - If the credentials are correct, you should be redirected to
welcome.jsp
. - If the credentials are incorrect, you should see an error message on the
login.jsp
page.