The following article describes the Difference Between Forward and Redirect in JSP.
In JSP (JavaServer Pages), both forward and redirect are mechanisms to transfer control from one page to another. However, they differ in how they accomplish this task and what happens to the client’s request and response.
Basically, a forward is an internal redirection that forwards the client’s request from one page to another within the server. So, in a forward, the client is unaware of the redirection and sends only one request to the server. The server processes the request and sends the response directly to the client. Also, the client’s URL remains the same, and the browser does not see the second page in the server response.
In contrast, a redirect is an external redirection that instructs the client’s browser to send a new request to the server for a different page. So, in a redirect, the server sends a response to the client with an HTTP status code of 3xx (usually 302 Found) and a new URL for the client to follow. Further, the client’s browser then sends a new request to the server for the new URL. Also, the client’s URL changes to the new URL. Hence, the browser sees the second page in the server response.
The following table shows a summary of the differences between forward and redirect.
Forward | Redirect | |
Type | Internal | External |
Request | Single | Multiple |
Client URL | Unchanged | Changed to new URL |
Browser | Sees one page | Sees both pages |
Response | One | Two (original and new) |
In general, you would use a forward when you want to keep the same URL and simply display a different page. Whereas you would use a redirect when you want to send the client to a completely different URL.