0% found this document useful (0 votes)
5 views2 pages

Java EE P2a

The document contains an HTML form for user login and a Java servlet (LoginServlet) that processes the login request. The servlet checks if the entered username and password match predefined values and redirects to a welcome page or displays a login failure message accordingly. The HTML and servlet code demonstrate a simple web application structure for handling user authentication.

Uploaded by

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

Java EE P2a

The document contains an HTML form for user login and a Java servlet (LoginServlet) that processes the login request. The servlet checks if the entered username and password match predefined values and redirects to a welcome page or displays a login failure message accordingly. The HTML and servlet code demonstrate a simple web application structure for handling user authentication.

Uploaded by

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

Practical 2(A)

index.html

<!DOCTYPE html>

<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<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>
</body>
</html>

LoginServlet.java

package mypack;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginServlet extends HttpServlet {

/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet LoginServlet</title>");
out.println("</head>");
out.println("<body>");
String uname = request.getParameter("txtId");
String upass = request.getParameter("txtPass");
if(uname.equals("admin") && upass.equals("servlet"))
{
RequestDispatcher rd =
request.getRequestDispatcher("WelcomeServlet");
rd.forward(request, response);
}
else
{
out.println("<h1> Login Fail !!! </h>");
RequestDispatcher rd = request.getRequestDispatcher("index.html");
rd.forward(request, response);
}
out.println("</body>");
out.println("</html>");
}
}
}

OutPut:

You might also like