0% found this document useful (0 votes)
13 views4 pages

SA 13

Software Architecture

Uploaded by

zufishaali2003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views4 pages

SA 13

Software Architecture

Uploaded by

zufishaali2003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Question 13: Write a simple program to demonstrate the use of Servlets and JSP.

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

The basic structure of the project looks like this:

- WebContent/
- WEB-INF/
- web.xml
- index.jsp
- login-success.jsp
- login-failure.jsp
- src/
- com.example.LoginServlet.java

Step 1: index.jsp - Login Page

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);
}
}
}

Step 3: login-success.jsp - Success Page

This page is displayed if the login credentials are correct.

html
<!DOCTYPE html>
<html>
<head>
<title>Login Successful</title>
</head>
<body>
<h2>Login Successful</h2>
<p>Welcome, user!</p>
</body>
</html>

Step 4: login-failure.jsp - Failure Page

This page is displayed if the login credentials are incorrect.

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>

Step 5: web.xml - Web Application Deployment Descriptor

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

<!DOCTYPE web-app PUBLIC


"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >

<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.

Running the Project

1. Deploy the project on a Java-based web server like Apache Tomcat.


2. Access the application by navigating to http://localhost:8080/YourAppName/index.jsp.
3. Enter user as the username and password as the password to be redirected to the success
page. Any other credentials will redirect you to the failure page.

This program demonstrates a simple login flow using Servlets and JSP, covering form handling,
request forwarding, and basic user validation.

You might also like