What is Request Dispatcher?

What is Request Dispatcher?

RequestDispatcher is an interface provided by the Servlet API that defines an object which can be used to forward a request to another resource (such as a servlet, HTML file, or JSP) or to include the content of another resource in the response. It is used to delegate the request from one servlet to another or to include additional content in the response. There are two primary methods in the RequestDispatcher interface.

Request Dispatcher

1.  forward(request, response)

  •  Forwards the request from one servlet to another resource within the same application.
  •  The request and response objects are passed to the target resource.
  •  The client does not know about the internal forwarding as the URL in the browser remains unchanged.

2.  include(request, response)

  • Includes the content of another resource in the response.
  • The response from the target resource is included in the original response.
  • The URL in the browser remains unchanged.

Here’s an example servlet demonstrating the use of Request Dispatcher:

Example
1.  Deployment Descriptor (web.xml) :
    xml
    <web-app>
        <servlet>
            <servlet-name>DispatcherServlet</servlet-name>
            <servlet-class>com.example.DispatcherServlet</servlet-class>
        </servlet>
        <servlet>
            <servlet-name>TargetServlet</servlet-name>
            <servlet-class>com.example.TargetServlet</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>DispatcherServlet</servlet-name>
            <url-pattern>/dispatcher</url-pattern>
        </servlet-mapping>
        <servlet-mapping>
            <servlet-name>TargetServlet</servlet-name>
            <url-pattern>/target</url-pattern>
        </servlet-mapping>
    </web-app>
    


Example
2.  Dispatcher Servlet Implementation :
    java
    package com.example;

    import java.io.IOException;
    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("/dispatcher")
    public class DispatcherServlet extends HttpServlet {

        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            String action = request.getParameter("action");

            if ("forward".equals(action)) {
                RequestDispatcher dispatcher = request.getRequestDispatcher("/target");
                dispatcher.forward(request, response);
            } else if ("include".equals(action)) {
                RequestDispatcher dispatcher = request.getRequestDispatcher("/target");
                dispatcher.include(request, response);
            } else {
                response.setContentType("text/html");
                response.getWriter().println("<h1>Invalid Action</h1>");
            }
        }
    }

Example
3.  Target Servlet Implementation :
    java
    package com.example;

    import java.io.IOException;
    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("/target")
    public class TargetServlet extends HttpServlet {

        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            response.setContentType("text/html");
            response.getWriter().println("<h1>This is the target servlet content</h1>");
        }
    }
   

Detailed Request Dispatcher

  • Forward :
    • The DispatcherServlet checks if the action parameter is forward.
    • If so, it creates a RequestDispatcher for the /target URL and calls the forward method.
    • The request is forwarded to TargetServlet, and its content is generated as the response.
    • The URL in the browser remains /dispatcher.

  • Include :
    • The DispatcherServlet checks if the action parameter is include.
    • If so, it creates a RequestDispatcher for the /target URL and calls the include method.
    • The content from TargetServlet is included in the response generated by DispatcherServlet.
    • The URL in the browser remains /dispatcher.

Homepage

Readmore