JSP forward vs JSP redirect
The differenes are same as in the case of Servlets disucssed in 20 differences between forward and sendRedirect Methods.
Anyhow, the same is reproduced replacing some minor terms.
This posting discusses the difference between <jsp:forward> action and response.sendRedirect() method.
Let us tabulate the differences of JSP forward vs JSP redirect.
|Defined interface||It is one of the JSP actions||Used with implicit response object; defined in HttpServletResponse|
|Signature||The action takes attributes like page etc.||void sendRedirect(String url)|
|Client awareness||Client is not aware of that he is getting response from a different Servlet as the URL will not change in client’s browser. It is just equivalent to a new response getting by client.||Client can know easily as the URL (from where he is getting response) changes in the client browser’s prompt.|
|Execution control||Execution control changes to another JSP file on the same server without client being informed that altogether a different JSP file is going to process his request.||Control changes to client|
|Where is what?||jsp:forward is done on server side without client’s knowledge.||Browser issues a new request on the URL that is redirected (sent as parameter) by the server and client can easily aware of.|
|Where happens||Everything happens on server side within the Web container and client is not involved.||sendRedirect() causes the Web container to return to the client’s browser. Client inturn can be redirected to different servers or domains.|
|Speed||Faster as forward runs on server-side entirely and no extra network trip (to client) is required.||Due to extra round trip between browser-server-browser (running on client as well as on server side), it is slower.|
|Content||jsp:forward action sends the client request to another resource of the same Web application.||Calls another page with a different request URL but not on the same request.|
|Usage||Used when processing is done by another Servlet||Used when wanted to redirect the client request to another Web site (completely out of current context) or to redirect the errors to another resource|
|Reusability||jsp:forward action reuses the current request object||Redirects create a new request object; consequently looses the original request with all its parameters and attributes.|
|Transfer of parameters||Original request and response objects transfer data coming from client along with additional information set with <param> tags (if any) to another resource request and response objects.||Redirect action sends header back to the client. Browser uses the URL contained in the header to call a new resource. As client initiates a new request, the original request and response objects are lost and fresh ones are to be created.|
|Transfer control||Internally, the JSP container transfers control of client request to another JSP.||This method sends the HTTP response to client browser to allow the client to send another request with a different URL. Usage of this method is equivalent to opening a new browser window and typing the URL.|
|What is sent?||Server sends the response (information required) to the client.||With this method, server sends a URL to the client.|
|Visual difference||Client cannot see the address of new resource that honours the client request in the address bar of the browser.||Client can see the new redirected address in address bar.|
|Examples||Calling another resource to process the data like validation of Login data.||Calling advertisements on the Web page or payment gateways.|
|Task separation||With this method, the responsibility of handling the client request can be distributed between many Servlets (or JSPs).||Used to transfer control altogether to a different domain. Also used to write separation of tasks.|
|Back and Forward buttons||As everything happens on server with jsp:forward, nothing is stored on browser history. So, Back and Forward buttons will not work.||As client makes new request and updated in browser history, back and forward buttons work.|
|URL||Use only relative URLs with jsp:forward.||Use absolute URLs.|
|MVC to hide||Useful in MVC design pattern to hide JSP/Servlet from direct access.||Once redirected to client, server looses control.|
|Which one to prefer?||If you would like to forward the client request to a new resource on the same server for further process, prefer jsp:forward where data of the original resource can be passed to the new resource.||If you would like to transfer the control to a new server or domain where client treats as a new task, prefer sendRedirect(). If the data of the original resource (which client requested) is needed in the new resource, store them in Session object and reuse.|
Some more points to notice on JSP forward vs JSP redirect.
- Session is not lost in both cases.
- The above differences are applicable to Servlets and JSPs. In Servlets, these methods are used in service() and in JSP used in scriptlets.
- In frameworks like Struts, the Controller can decide, at the end of request processing, which one to use of either forward or redirect operation.
- The Controller also can decide with forward() method, to what resource the forward should be made, depending on different conditions of client request requirements.
Which one is preferred, JSP forward vs JSP redirect?
Just depends on the scenario.
If you would like to forward the client request to a new resource on the same server for further process, prefer jsp:forward where data of the original resource can be passed to the new resource.
If you would like to transfer the control to a new server or domain where client treats as a new task, prefer response.sendRedirect(). If the data of the original resource (which client requested) is needed in the new resource, store them in Session object and reuse.
Do you know you can use <jsp:forward> with parameters and is discussed in JSP forward action with Parameters.