What is the conceptual difference between forward()
and sendRedirect()
?
相关问题
- Stop .htaccess redirect with query string
- UrlEncodeUnicode and browser navigation errors
- #{facesContext} EL expression not resolved at runt
- jsp caching tag library
- how to create files under /WEB-INF/
相关文章
- jsp里面的画布功能,为什么会出错?求大佬找下问题
- How to get jQuery.ajax response status?
- JSP String formatting Truncate
- send redirect and setting cookie, using laravel 5
- java.lang.NoClassDefFoundError: javax/servlet/http
- Forward request from servlet to jsp
- Intercept @RequestHeader exception for missing hea
- Comparing string and boolean in Expression languag
Source
The
RequestDispatcher
interface allows you to do a server side forward/include whereassendRedirect()
does a client side redirect. In a client side redirect, the server will send back an HTTP status code of302
(temporary redirect) which causes the web browser to issue a brand new HTTPGET
request for the content at the redirected location. In contrast, when using theRequestDispatcher
interface, the include/forward to the new resource is handled entirely on the server side.Technically redirect should be used either if we need to transfer control to different domain or to achieve separation of task.
For example in the payment application we do the PaymentProcess first and then redirect to displayPaymentInfo. If the client refreshes the browser only the displayPaymentInfo will be done again and PaymentProcess will not be repeated. But if we use forward in this scenario, both PaymentProcess and displayPaymentInfo will be re-executed sequentially, which may result in incosistent data.
For other scenarios, forward is efficient to use since as it is faster than sendRedirect
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
headerother.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")
. TheRequestDispatcher#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 aServlet
which controls, preprocesses and postprocesses the requests. The JSPs in/WEB-INF
folder are not directly accessible by URL, but theServlet
can access them usingRequestDispatcher#forward()
.You can for example have a JSP file in
/WEB-INF/login.jsp
and aLoginServlet
which is mapped on anurl-pattern
of/login
. When you invokehttp://example.com/context/login
, then the servlet'sdoGet()
will be invoked. You can do any preprocessing stuff in there and finally forward the request like:When you submit a form, you normally want to use
POST
:This way the servlet's
doPost()
will be invoked and you can do any postprocessing stuff in there (e.g. 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 (e.g. pressing F5 or navigating back in history).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 thePOST-Redirect-GET
pattern.Either of these methods may be "better", i.e. more suitable, depending on what you want to do.
A server-side redirect is faster insofar as you get the data from a different page without making a round trip to the browser. But the URL seen in the browser is still the original address, so you're creating a little inconsistency there.
A client-side redirect is more versatile insofar as it can send you to a completely different server, or change the protocol (e.g. from HTTP to HTTPS), or both. And the browser is aware of the new URL. But it takes an extra back-and-forth between server and client.
SendRedirect()
will search the content between the servers. it is slow because it has to intimate the browser by sending the URL of the content. then browser will create a new request for the content within the same server or in another one.RquestDispatcher
is for searching the content within the server i think. its the server side process and it is faster compare to theSendRedirect()
method. but the thing is that it will not intimate the browser in which server it is searching the required date or content, neither it will not ask the browser to change the URL in URL tab. so it causes little inconvenience to the user.