RequestDispatcher in Servlet

As of now, we have discussed ServletRequest and ServletResponse Interface in Servlet. We have also discussed how the request is done and how the response is done, their methods, etc. In this article, we will discussĀ  RequestDispatcher Interface in a servlet, their methods, etc.

What is RequestDispatcher?

In Servlet RequestDispatcher is an interface that defines an object in the servlet that receives a request from the client and sends it to any resources such as JSP, HTML, or any servlet on the server.

  • The serverlet container is responsible for making the object of RequestDispatcher.
  • It is used to include the response of one servlet into another.
  • It is also used to forward the client request to another servlet
  • This interface is present in javax.servlet package.
public interface RequestDispatcher

Methods in RequestDispatcher

  • public void forward(ServletRequest req,ServletResponse res):This method forward the request from one servlet to any another servlet,JSP,HTML,etc.
    req: It is a request from a client.
    res: It is a response to the client.
  • public void include(ServletRequest req, ServletResponse res): This method includes the content of the resource in the response.

How to get Object of RequestDispatcher?

The getRequestDispatcher() method is used to get the object of RequestDispatcher.

RequestDispatcher rd=request.getRequestDispatcher("welcome.html")

After this, we will call forward() or include() method as per our requirement.

rd.forward(request,response)
rd.include(request,response)

Let’s see the example of RequestDispatcher Interface in servlet

In this example, we will verify the login of the user. If the login information is correct it will forward it to the welcome page. otherwise, it will show an error message. The following diagram shows the actual representation of what we are gonna do.

index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
  <form action="Login" method="post">
    Username <input type="text" name="uname"> <br> Password <input
      type="password" name="pass"> <br> <input type="submit"
      value="Login">
  </form>
</body>
</html>

LoginServlet.java

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
  private static final long serialVersionUID = 1L;

  public LoginServlet() {
    super();
    // TODO Auto-generated constructor stub
  }

  protected void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    // TODO Auto-generated method stub
    System.out.println("in post");
    PrintWriter out = response.getWriter();
    String uname = request.getParameter("uname");
    String pass = request.getParameter("pass");
    if (uname.equals("abc") && pass.equals("abc10")) {
      RequestDispatcher rd = request.getRequestDispatcher("Welcome");
      rd.forward(request, response);
    } else {
      out.println("Incorrect Username or Password");
      RequestDispatcher rd = request.getRequestDispatcher("login.html");
      rd.include(request, response);
    }
  }
}

Welcome.java

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Welcome
 */
@WebServlet("/Welcome")
public class Welcome extends HttpServlet {
  private static final long serialVersionUID = 1L;

  public Welcome() {
    super();
    // TODO Auto-generated constructor stub
  }

  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    // TODO Auto-generated method stub
    PrintWriter out = response.getWriter();
    String name = request.getParameter("uname");
    out.println("Welcome " + name);
  }
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns="http://java.sun.com/xml/ns/javaee"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  id="WebApp_ID" version="3.0">
  <display-name>RequestDispatcherDemo</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>Login</servlet-name>
    <servlet-class>LoginServlet</servlet-class>
  </servlet>
  <servlet>
    <servlet-name>abc</servlet-name>
    <servlet-class>Welcome</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Login</servlet-name>
    <url-pattern>/Login</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>abc</servlet-name>
    <url-pattern>/abc</url-pattern>
  </servlet-mapping>
</web-app>

Output