What are the different ways for browser redirection in JSP?
There are two ways for browser redirection in JSP:
- Using response.sendRedirect()
- Using <jsp:forward /> element
A redirect is a totally different from a forward. The response.sendRedirect() creates a new request object which doesn’t carry any of old requests. The request handling JSP page tells the browser to make a new request to the target servlet or JSP page. The URL shown in the browser therefore changes to the URL of the new page when you redirect.
The <jsp:forward /> standard action is actually a call to HttpServletRequest.forward(String url) which passes the request object within the server to either a servlet or a JSP page. The new servlet or JSP page continues to process the same request and the browser is not aware of the fact that more than one servlet or page is involved. In other words, the client is not aware that the request is being forwarded anywhere. The URL shown in the browser stays unchanged when you do forward.