Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between JSP forward and redirect [duplicate]

Please explain the difference between jsp:forward and redirect.
What is happening in each case?

like image 422
Ammu Avatar asked May 20 '11 07:05

Ammu


People also ask

What is the difference between forward and redirect?

A forward( ) operates within the server and executes faster than a SendRedirect( ). A redirect has to go through the browser and then wait for the browser to make a new HTTP request.

What is the difference between forward () and include () method in jsp?

The forward() method is used to transfer the client request to another resource (HTML file, servlet, jsp etc). When this method is called, the control is transferred to the next resource called. On the other hand, the include() method is used to include the content of the calling file into the called file.

What is the difference between jsp forward page and response sendRedirect URL?

When we use the forward method request is transferred to other resources within the same server for further processing. In case of sendRedirect request is transferred to another resource to a different domain or different server for further processing.

What is the difference between RequestDispatcher forward () and include () method?

RequestDispatcher methods The difference between the two methods is that the forward method will close the output stream after it has been invoked, whereas the include method leaves the output stream open. The include method takes the content from another resource and includes it in the servlet.


Video Answer


2 Answers

  • redirect sets the response status to 302 [1], and the new url in a Location header, and sends the response to the browser. Then the browser, according to the http specification, makes another request to the new url

  • forward happens entirely on the server. The servlet container just forwards the same request to the target url, without the browser knowing about that. Hence you can use the same request attributes and the same request parameters when handling the new url. And the browser won't know the url has changed (because it has happened entirely on the server)


[1]: This is an example of industry practice contradicting the standard. The HTTP/1.0 specification (RFC 1945) required the client to perform a temporary redirect (the original describing phrase was "Moved Temporarily"), but popular browsers implemented 302 with the functionality of a 303 See Other. Therefore, HTTP/1.1 added status codes 303 and 307 to distinguish between the two behaviours. However, some Web applications and frameworks use the 302 status code as if it were the 303. Source

like image 115
Bozho Avatar answered Oct 10 '22 19:10

Bozho


I've heard interesting explanations of redirect and forward. Imagine that you need some service from your friend. It doesn't matter what service. Suppose that your friend can't help you but knows who can.

He would REDIRECT your request if he would tell you: "I can't handle this, but I know who can. Here his phone number. Call him."

He would FORWARD your request if he would tell you: "No problem" and calls that man by himself without notifying you about involving another person in handling your desire. Then, your friend will get the result of sorting out your wish and transmit it to you.

like image 46
Serhii Soboliev Avatar answered Oct 10 '22 19:10

Serhii Soboliev