0% found this document useful (0 votes)
3 views3 pages

19PROG

The document provides a Java servlet implementation for handling a login form using the doPost method. It includes a hardcoded username and password for validation and returns a success or failure message based on the input. An accompanying HTML form is also provided for user input.

Uploaded by

shadaf8010
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)
3 views3 pages

19PROG

The document provides a Java servlet implementation for handling a login form using the doPost method. It includes a hardcoded username and password for validation and returns a success or failure message based on the input. An accompanying HTML form is also provided for user input.

Uploaded by

shadaf8010
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/ 3

PRACTICAL NO.

:19
Aim: Implementation of Servlet for doPost method.

JAVA PROGRAM:

import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

import java.io.*;

@WebServlet("/login") // Maps to /login URL


public class Login extends HttpServlet {

private static nal long serialVersionUID = 1L;

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
// Retrieve the username and password from the form
String username = request.getParameter("username");
String password = request.getParameter("password");

// De ne some hardcoded username and password for demonstration purposes


String correctUsername = "Shadaf Khan";
String correctPassword = "Shadaf123";

// Check if the entered username and password are correct


response.setContentType("text/html");
PrintWriter out = response.getWriter();

if (username.equals(correctUsername) && password.equals(correctPassword)) {


out.println("<html><body>");
out.println("<h2>Login Successful!</h2>");
out.println("<p>Welcome, " + username + "!</p>");
out.println("</body></html>");
} else {
out.println("<html><body>");
out.println("<h2>Login Failed</h2>");
out.println("<p>Invalid username or password.</p>");
out.println("</body></html>");
}
}
}

HTML PROGRAM:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login Form</title>
</head>
<body>
<h2>Login</h2>
fi
fi
<form action="login" 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>
</body>
</html>

OUTPUT:

You might also like