In Java, the RequestDispatcher
is used to forward a client’s request to another resource, such as a servlet, JSP, or HTML file. This is common when you want to break down the processing of a request into multiple components.
Syntax to Use RequestDispatcher
The RequestDispatcher
interface provides two main methods to forward or include content:
forward(ServletRequest request, ServletResponse response)
: Forwards the request to another resource.include(ServletRequest request, ServletResponse response)
: Includes the content of another resource in the response.
Steps to Forward Requests
- Get the
RequestDispatcher
object:
UseServletRequest.getRequestDispatcher(String path)
to obtain aRequestDispatcher
instance. Thepath
can be relative or absolute. -
Forward the request:
Call theforward()
method on theRequestDispatcher
object to forward the request and response to another resource.
Example of Using RequestDispatcher
Here’s an example of using the RequestDispatcher
to forward a request to another servlet or JSP:
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/forwardExample")
public class ForwardExampleServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Add some attributes to the request
request.setAttribute("message", "This is a forwarded request");
// Get the RequestDispatcher for the target resource
RequestDispatcher dispatcher = request.getRequestDispatcher("/target.jsp");
// Forward the request and response
dispatcher.forward(request, response);
}
}
What Happens When You Forward?
- The
forward()
method hands over control of the request to the specified resource. - The original request and response objects are passed along to the next resource.
- The client’s browser does not see a new request or URL change. The forward happens entirely on the server.
Example of the Target Resource (target.jsp
)
Here’s an example target.jsp
that receives the forwarded request:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Forwarded Page</title>
</head>
<body>
<h1>Forwarded Page</h1>
<p>Message: ${message}</p>
</body>
</html>
Key Points to Remember
- Forward Happens Internally:
The URL in the browser doesn’t change, and the operations happen on the server side. -
Avoid Committing the Response:
You cannotforward()
the request if the response has already been committed (e.g., if you’ve written something to the response output already). -
Relative and Absolute Paths:
- A path starting with
/
is absolute (relative to the web application root). - A path without
/
is relative to the current request path.
- A path starting with
- Forward vs Redirect:
- Forward happens on the server side; the browser is unaware.
- Redirect happens by sending a response back to the client, requiring the client to make a new request.
Maven dependencies
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>6.1.0</version>
<scope>provided</scope>
</dependency>