Using Request Dispatcher Interface create a Servlet which will validate the password entered by the user, if the user has entered "Servlet" as password, then he will be forwarded to Welcome Servlet else the user will stay on the index.html page and an error message will be displayed.

 index.html

<html> 

    <head><title>Login Form</title> 

    </head>  

    <form action="LoginServlet" >      

        Enter User ID<input type="text" name="txtId"><br><br>      

        Enter Password<input type="password" name="txtPass"><br><br>   

        <input type="reset" value="Reset">  

        <input type="submit" value=" Click to Login " >  

    </form> 

</html> 

LoginServlet.java
package mypack; 
import java.io.IOException; 
import java.io.PrintWriter; 
import javax.servlet.ServletException;  
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import javax.servlet.RequestDispatcher; 
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>");                  
           out.println("<title>Servlet LoginServlet</title></head>");      
           String uname = request.getParameter("txtId");                
           String upass = request.getParameter("txtPass");                   
          if(uname.equals("admin") && upass.equals("servlet")) 
        {       
            RequestDispatcher rd = request.getRequestDispatcher("Welcomel.html");  
            rd.forward(request, response);       
        }         
        else 
        {     
            out.println("<body bgcolor=red >");                  
            out.println("<h1> Login Fail !!! </h1>");      
            RequestDispatcher rd = request.getRequestDispatcher("index.html");       
            rd.include(request, response);        
        }            
        out.println("</body>");             
        out.println("</html>");       
        }      
 
Welcome1.html
<html> 
    <head> 
        <title>welcome</title> 
        <meta charset="UTF-8"> 
        <meta name="viewport" content="width=device-width, initial-scale=1.0">     </head> 
    <body bgcolor="blue"> 
        <h1>Welcome Servlet </h1> 
    </body> 
</html> 




Post a Comment

0 Comments