FAQ

Java

JSP

Servlet


Advertisement



What is the difference between RequestDispatcher's forward method and HttpServletResponse's sendRedirect method?

RequestDispatcher's forward(ServletRequest request, ServletResponse response)

The forward() method of RequestDispatcher will forward the ServletRequest and ServletResponse that it is passed to the path that was specified in getRequestDispatcher(String path). The response will not be sent back to the client and the web container (For example, Tomcat) internally redirects the request to the other JSP/Servlet. Remember , you can redirect only to a page within current servlet context. The client will not know about this change of resource on the server.

The forward works better when one resource(JSP/Servlet) must perform business logic and share the results with another resource(JSP/Servlet). Because the request and response are forwarded to another resource all request parameters are maintained and available for use. Forward request, happened on server side, is transparent to the client (browser). Therefor, no history of it will be stored on the client, so using the back and forward buttons will not work.

An example using forward:

protected void doGet(HttpServletRequest request, 
HttpServletResponse response)
throws ServletException, IOException {
RequestDispatcher rd = request.getRequestDispatcher("pathToResource");
rd.forward(request, response);
}

Also there are two flavours of RequestDispatcher:

  • You can get it from ServletContext .The pathname must begin with a "/" and is interpreted as relative to the current context root.
  • You can get it from ServletRequest. The pathname specified may be relative, although it cannot extend outside the current servlet context. If the path begins with a "/" it is interpreted as relative to the current context root.

HttpServletResponse's sendRedirect(String location) Method

The sendRedirect(String path) method of HttpServletResponse actually modifies response header and tells the browser (the client) that the requested page is relocated and header also contains the new location of the page. Then browser (the client) will send the request again to this new location. Thus your request gets redirected. It requires a round-trip communication with the client. You can redirect the request any where in the web using sendRedirect. Because a new request is being submitted, all previous parameters stored in the request will be unavailable. The client's history will be updated so the forward and back buttons will work. This method is useful for redirecting to pages on other servers and domains.

An example using sendRedirect:

protected void doGet(HttpServletRequest request, 
HttpServletResponse response)
throws ServletException, IOException {
response.sendRedirect("pathToResource");
}

Summary

You can use only relative URLs with forward(), while sendRedirect() works with absolute URLs. This poses its own problems, as if your page has relative links(to images etc), if you use forward, there is a good chance they they will end up as broken links. Hence, it will be better to use sendRedirect() whenever possible and use forward() only when required.

When forward is used we can pass data to the forwarded jsp/servlet using request.setAttribute() as the same request and response objects available the forwarded servlet/jsp. But using sendRedirect() we have to set the data in session or by appending the data to the URL that will be passed as argument to this method, because by calling this method a new request is created.

The forward() request is transparent to the user. It is done at server side and it is not visible to user.

The sendRedirect is not transparent to the user, if request is sendRedirect then its visible in your browser (redirect at client side).

The forward() method is faster than using sendRedirect as no network round trip to the server and back is required.


Printer-friendly version Printer-friendly version | Send this 
article to a friend Mail this to a friend

Previous Next vertical dots separating previous/next from contents/index/pdf Contents

  |   |