The main purpose of a requestDispatcher is to forward the received request and the response of a servlet to another resource -mainly another servlet,html file,JSP etc- or to include another resource before responding to the client's request.In this tutorial we will see how to use both RequestDispatcher methods forward() and include().

As a fist step in this tutorial we need to obtain the RequestDispatcher.This is quite easy since you can get access to the requestDispatcher in several ways;from the ServletContext using

servletContext.getRequestDispatcher()
or from the request using
request.getRequestDispatcher();
.In both cases you need to provide the path to the resource.

Forwarding request and response using forward() Method


package com.javaonly.servlets;

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 DispatchingServlet extends HttpServlet {

 
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //get the RequestDispatcher from the servletContext
        RequestDispatcher requestDispatcher = getServletContext().getRequestDispatcher("/index.jsp");
        //forward the request to index.jsp
        requestDispatcher.forward(request, response);
    }

    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

  
    @Override
    public String getServletInfo() {
        return "Short description";
    }
}

The above example may be quite trivial but note that all the parameters or the attributes that the request may have will be forwarded and displayed in the jsp.

Include resources with include() method


package com.javaonly.servlets;

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 DispatchingServlet extends HttpServlet {

 
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //get the RequestDispatcher from the servletContext
        RequestDispatcher requestDispatcher = getServletContext().getRequestDispatcher("/TestServlet");
        //include TestServlet into the resposnse
        requestDispatcher.include(request, response);
        //forward the request to index.jsp
        RequestDispatcher dispatcher=getServletContext().getRequestDispatcher("/index.jsp");
        
        dispatcher.forward(request, response);;
    }

    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

  
    @Override
    public String getServletInfo() {
        return "Short description";
    }
}

Finally,as you can see in the snippet below,the attribute added by TestServlet is displayed in the jsp



<%@page contentType="text/html" pageEncoding="UTF-8"%>


    
        
        JSP Page
    
    
        

<%=request.getAttribute("test")%>