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

Login Servlet Example with Output

The document describes creating a login servlet in Java. An index.html page contains a login form that submits to the LoginServlet. The LoginServlet gets the username and password parameters, checks if they match expected values, and returns a response saying "Hello <username>" if correct or "login failed" if incorrect.

Uploaded by

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

Login Servlet Example with Output

The document describes creating a login servlet in Java. An index.html page contains a login form that submits to the LoginServlet. The LoginServlet gets the username and password parameters, checks if they match expected values, and returns a response saying "Hello <username>" if correct or "login failed" if incorrect.

Uploaded by

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

1b. Create a servlet for a login page.

If the username and password are


correct then it says message
“Hello <username>” else a message “login failed”
1) index.html
<html>
<head>
<title>Login Form</title>
</head>
<form action="LoginServlet" >
Enter User ID<input type="text" name="txtId"><br>
Enter Password<input type="password" name="txtPass"><br>
<input type="reset">
<input type="submit" value=" Click to Login " >
</form>
</html>
2) LoginServlet.java

LoginServlet.java
package mypack;
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;

public class LoginServlet extends HttpServlet {


public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<html><head><title>Servlet LoginServlet</title></head>");
String uname = request.getParameter("txtId");
String upass = request.getParameter("txtPass");
if(uname.equals("client") && upass.equals("12345")){
out.println("<body bgcolor=blue >");
out.println("<h1> Welcome !!! "+uname+"</h1>");
}
else{
out.println("<body bgcolor=red >");
out.println("<h1> Login Fail !!! </h1>");
}
out.println("</body></html>");
}
}
Output:

If ID & Password incorrect


If ID & Password is correct

You might also like