SA 13
SA 13
Here's a simple program that demonstrates the use of Servlets and JSP by creating a basic user
login page. The program includes:
1. A login form on a JSP page where the user enters their username and password.
2. A Servlet that handles the form submission, checks the credentials, and redirects the user
to a success or failure page based on the authentication result.
Project Structure
- WebContent/
- WEB-INF/
- web.xml
- index.jsp
- login-success.jsp
- login-failure.jsp
- src/
- com.example.LoginServlet.java
This page provides a login form where the user enters their username and password.
html
<!DOCTYPE html>
<html>
<head>
<title>Login Page</title>
</head>
<body>
<h2>Login</h2>
<form action="login" method="post">
<label for="username">Username:</label>
<input type="text" name="username" id="username" required><br><br>
<label for="password">Password:</label>
<input type="password" name="password" id="password" required><br><br>
<input type="submit" value="Login">
</form>
</body>
</html>
Step 2: LoginServlet.java - Servlet for Processing Login
This servlet processes the login form, validates the user credentials, and redirects to a success or
failure page based on the result.
java
package com.example;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
private static final String USERNAME = "user";
private static final String PASSWORD = "password";
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
// Check credentials
if (USERNAME.equals(username) && PASSWORD.equals(password)) {
// Redirect to success page if credentials match
request.getRequestDispatcher("login-success.jsp").forward(request, response);
} else {
// Redirect to failure page if credentials do not match
request.getRequestDispatcher("login-failure.jsp").forward(request, response);
}
}
}
html
<!DOCTYPE html>
<html>
<head>
<title>Login Successful</title>
</head>
<body>
<h2>Login Successful</h2>
<p>Welcome, user!</p>
</body>
</html>
html
<!DOCTYPE html>
<html>
<head>
<title>Login Failed</title>
</head>
<body>
<h2>Login Failed</h2>
<p>Invalid username or password. Please try again.</p>
</body>
</html>
Define the servlet mapping in web.xml if you're not using annotations (though this step is
optional if you use @WebServlet annotations, as shown in the LoginServlet above).
xml
<web-app>
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.example.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
</web-app>
Explanation
1. index.jsp: Displays a login form. When the form is submitted, it sends a POST request to
/login, handled by LoginServlet.
2. LoginServlet.java: Receives the POST request, checks the username and password
parameters, and redirects the user to either login-success.jsp or login-failure.jsp based on
the validation result.
3. login-success.jsp: A simple page that shows a welcome message if login is successful.
4. login-failure.jsp: Displays an error message if login fails.
This program demonstrates a simple login flow using Servlets and JSP, covering form handling,
request forwarding, and basic user validation.