ZetCode

Java RequestDispatcher

last modified September 18, 2026

This Java RequestDispatcher tutorial shows how to use RequestDispatcher to dispatch requests to other resources.

RequestDispatcher

RequestDispatcher dispatches a request to a resource on the server, such as a servlet, an HTML file, a JSP file, or a FreeMarker or Thymeleaf template.

RequestDispatcher methods

RequestDispatcher has two methods:

The forward method transfers control to another resource. The target resource generates the response, and the calling servlet must not write to the response after forwarding. The include method adds the output from another resource to the current response, so the calling servlet can continue writing to the response afterward.

Getting RequestDispatcher

RequestDispatcher can be obtained from a request object or from a servlet context.

var dispatcher = request.getRequestDispatcher("greet.jsp");
dispatcher.forward(request, response);

We can get the RequestDispatcher from the request object with the getRequestDispatcher method.

var dispatcher = getServletContext().getRequestDispatcher("/greet.jsp");
dispatcher.forward(request, response);

Here we get the RequestDispatcher from the servlet context. In this case, the path must begin with a slash character.

Java RequestDispatcher Forward to JSP

The following example sends a request from the client to a JSP page.

index.html
<!DOCTYPE html>
<html>
    <head>
        <title>Start Page</title>
        <meta charset="UTF-8">
    </head>
    <body>
        <form action="MyServlet">

            <label>Enter your name:
                <input type="text" name="name">
            </label>
            
            <button type="submit">Submit</button>

        </form>
    </body>
</html>

In the home page we have a simple form: it takes a value from the user and sends it as a request parameter to MyServlet.

MyServlet.java
package com.zetcode.web;

import java.io.IOException;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

@WebServlet("/MyServlet")
public class MyServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        response.setContentType("text/html;charset=UTF-8");

        request.getRequestDispatcher("greet.jsp").forward(request, response);
    }
}

In the MyServlet, we forward to the greet.jsp page using the RequestDispatcher.

greet.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <p>Hello ${param.name}!</p>
    </body>
</html>

In the greet.jsp page, we display the name parameter, which was set by the user in the form.

Java RequestDispatcher Forward to Servlet

The following example sends a request from the client to a servlet, which forwards it to another servlet.

index.html
<!DOCTYPE html>
<html>
    <head>
        <title>Start Page</title>
        <meta charset="UTF-8">
    </head>
    <body>
        <p>
            <a href="MyServlet">Call servlet</a>
        </p>
    </body>
</html>

The home page contains a link that calls MyServlet.

MyServlet.java
package com.zetcode.web;

import java.io.IOException;
import java.time.Instant;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

@WebServlet("/MyServlet")
public class MyServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        response.setContentType("text/html;charset=UTF-8");

        request.setAttribute("now", Instant.now());
        
        request.getRequestDispatcher("AnotherServlet").forward(request, response);
    }
}

The request goes first to MyServlet.

request.setAttribute("now", Instant.now());

We set an attribute to the request; it is the current time instant.

request.getRequestDispatcher("AnotherServlet").forward(request, response);

The request, including the new attribute, is sent to AnotherServlet.

AnotherServlet.java
package com.zetcode.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.Locale;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

@WebServlet("/AnotherServlet")
public class AnotherServlet extends HttpServlet {

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

        response.setContentType("text/plain;charset=UTF-8");

        var out = response.getWriter();

        var formatter
                = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT)
                        .withLocale(Locale.ENGLISH)
                        .withZone(ZoneId.of("UTC"));

        var now = (Instant) request.getAttribute("now");

        var output = formatter.format(now);
        out.println(output);
    }
}

AnotherServlet formats the instant as a short English date-time value and prints it to the output stream.

DateTimeFormatter formatter
        = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT)
                .withLocale(Locale.ENGLISH)
                .withZone(ZoneId.of("UTC"));

We format the date and time with the DateTimeFormatter class.

var now = (Instant) request.getAttribute("now");

We retrieve the attribute from the request with the getAttribute method.

var output = formatter.format(now);
out.println(output);

The instant is formatted and printed to the output.

Java RequestDispatcher include

The next example includes output from another servlet in the calling servlet's response.

index.html
<!DOCTYPE html>
<html>
    <head>
        <title>Start Page</title>
        <meta charset="UTF-8">
    </head>
    <body>
        <p>
            <a href="MyServlet">Call servlet</a>
        </p>
    </body>
</html>

The home page contains a link that calls MyServlet.

MyServlet.java
package com.zetcode.web;

import java.io.IOException;
import java.io.PrintWriter;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

@WebServlet("/MyServlet")
public class MyServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        response.setContentType("text/plain;charset=UTF-8");
        
        var out = response.getWriter();
        out.println("Hello from MyServlet");
        
        request.getRequestDispatcher("AnotherServlet").include(request, response);
    }
}

MyServlet prints data to the output stream and forwards to AnotherServlet.

AnotherServlet.java
package com.zetcode.web;

import java.io.IOException;
import java.io.PrintWriter;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

@WebServlet("/AnotherServlet")
public class AnotherServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        response.setContentType("text/plain;charset=UTF-8");
        
        var out = response.getWriter();
        
        out.println("Hello from AnotherServlet");
    }
}

AnotherServlet also prints data to the output stream. In the end, we have both messages written to the output stream and sent to the client.

Source

Java RequestDispatcher

In this article, we covered the Java RequestDispatcher and its forward and include methods.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Java tutorials.