RequestDispatcher.forward() vs HttpServletResponse.sendRedirect()
forward()
和sendRedirect()
之间的概念区别是什么?
requestDispatcher - forward() method
When we use forward method, request is transfer to other resource within the same server for further processing.
In case of forward, web container handle all process internally and client or browser is not involved.
When forward is called on requestdispatcher object we pass request and response objects so our old request object is present on new resource which is going to process our request.
Visually we are not able to see the forwarded address, it is transparent.
Using forward () method is faster than send redirect.
When we redirect using forward and we want to use same data in new resource we can use request.setAttribute () as we have request object available.
SendRedirect
In case of sendRedirect, request is transfer to another resource to different domain or different server for further processing.
When you use sendRedirect, container transfers the request to client or browser so URL given inside the sendRedirect method is visible as a new request to the client.
In case of sendRedirect call, old request and response objects are lost because it's treated as new request by the browser.
In address bar, we are able to see the new redirected address. It's not transparent.
sendRedirect is slower because one extra round trip is required, because completely new request is created and old request object is lost. Two browser request required.
But in sendRedirect, if we want to use we have to store the data in session or pass along with the URL.
Which one is good?
Its depends upon the scenario that which method is more useful.
If you want control is transfer to new server or context and it is treated as completely new task then we go for Send Redirect. Generally, forward should be used if the operation can be safely repeated upon a browser reload of the web page will not affect the result.
Source
First of all, the term "redirect" is in web development world the action of sending the client an empty HTTP response with just a Location
header with therein the new URL on which the client has to send a brand new GET request. So basically:
some.jsp
. Location: other.jsp
header other.jsp
(this get reflected in browser address bar!) other.jsp
. You can track it with the webbrowser's builtin/addon developer toolset. Press F12 in Chrome/IE9/Firebug and check the "Network" section to see it.
Exactly the above is achieved by sendRedirect("other.jsp")
. The RequestDispatcher#forward()
doesn't send a redirect. Instead, it uses the content of the target page as HTTP response.
some.jsp
. other.jsp
. However as the original HTTP request was to some.jsp
, the URL in browser address bar remains unchanged.
The RequestDispatcher
is extremely useful in the MVC paradigm and/or when you want to hide JSP's from direct access. You can put JSP's in /WEB-INF
folder and use a Servlet
which controls, preprocesses and postprocesses the requests. The JSPs in /WEB-INF
folder are not directly accessible by URL, but the Servlet
can access them using RequestDispatcher#forward()
.
You can for example have a JSP file in /WEB-INF/login.jsp
and a LoginServlet
which is mapped on an url-pattern
of /login
. When you invoke http://example.com/context/login
, then the servlet's doGet()
will be invoked. You can do any preprocessing stuff in there and finally forward the request like:
request.getRequestDispatcher("/WEB-INF/login.jsp").forward(request, response);
When you submit a form, you normally want to use POST
:
<form action="login" method="post">
This way the servlet's doPost()
will be invoked and you can do any postprocessing stuff in there (eg validation, business logic, login the user, etc).
If there are any errors, then you normally want to forward the request back to the same page and display the errors there next to the input fields and so on. You can use the RequestDispatcher
for this.
If a POST
is been successful, you normally want to redirect the request, so that the request won't be resubmitted when the user refreshes the request (eg pressing F5 or navigating back in history).
User user = userDAO.find(username, password);
if (user != null) {
request.getSession().setAttribute("user", user); // Login user.
response.sendRedirect("home"); // Redirects to http://example.com/context/home after succesful login.
} else {
request.setAttribute("error", "Unknown login, please try again."); // Set error.
request.getRequestDispatcher("/WEB-INF/login.jsp").forward(request, response); // Forward to same page so that you can display error.
}
A redirect thus instructs the client to fire a new GET
request on the given URL. Refreshing the request would then only refresh the redirected request and not the initial request. This will avoid "double submits" and confusion and bad user experience. This is also called the POST-Redirect-GET
pattern.
The RequestDispatcher
interface allows you to do a server side forward/include whereas sendRedirect()
does a client side redirect. In a client side redirect, the server will send back an HTTP status code of 302
(temporary redirect) which causes the web browser to issue a brand new HTTP GET
request for the content at the redirected location. In contrast, when using the RequestDispatcher
interface, the include/forward to the new resource is handled entirely on the server side.
上一篇: 如何将jsp中的ResultSet对象发送回html(javascript)?
下一篇: RequestDispatcher.forward()vs HttpServletResponse.sendRedirect()