BSc IT Semester 5 – Enterprise Java Practical 2A
In this practical, we are going to learn how to use the RequestDispatcher Interface in Java Servlets. The objective of this task is to create a login form and validate the user credentials. If the password entered by the user is correct, he will be forwarded to a welcome page; otherwise, he will remain on the login page with an error message.
Practical Aim
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 the Welcome Servlet. Else, the user will stay on the index.html page and an error message will be displayed.
Steps Performed in the Practical
- Create index.html page: First, we designed a simple login form where the user has to enter User ID and Password. The form is submitted to LoginServlet for validation.
- 
    LoginServlet:  
    In the servlet, we captured the values of User ID and Password entered in the form.  
    If the User ID is admin and the Password is servlet, then the request is forwarded to the WelcomeServlet using RequestDispatcher.forward(). Otherwise, the login fails, and the user is shown an error message with a red background. The original login page (index.html) is then included again usingRequestDispatcher.include().
- WelcomeServlet: If the login details are correct, this servlet displays a new page with a green background and a message saying: "Welcome, <User Name>. You have successfully logged in!"
🌐 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>
🌐 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("WelcomeServlet");
            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>");
    }
}
🌐 WelcomeServlet.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;
public class WelcomeServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String uname = request.getParameter("txtId");
        out.println("<html><head>");
        out.println("<title>Welcome Page</title></head>");
        out.println("<body bgcolor='lightgreen'>");
        out.println("<h1> Welcome, " + uname + " </h1>");
        out.println("<h2> You have successfully logged in! </h2>");
        out.println("</body></html>");
    }
}
🔍 Output Preview
Output Explanation
- If the user enters admin as User ID and servlet as Password, he is successfully forwarded to the Welcome Servlet.
- If the user enters any wrong password, he remains on the login page with the error message Login Fail!!!.
Learning Outcome
From this practical, we learned how to use RequestDispatcher in two ways:
- forward() – to transfer the request to another servlet when the condition is correct.
- include() – to include another page (like index.html) when login fails.
Conclusion
This practical demonstrated how to validate login details in Java Servlets using RequestDispatcher. By forwarding or including pages, we can control the flow of the application easily. It is an important concept for building secure and user-friendly web applications in Java.
🔗
 
 
 
