Pega Interview Question
Applications DevelopersCountry: India
Interview Type: In-Person
SendRedirect () method is declared in HttpServletResponse Interface as
void sendRedirect(String url)
Redirect sends a header back to the client browser on transfer of control to a different server or domain. Header contains the resource url , the browser needs for redirection. Using this url information, the browser now initiates a new connection and in this process, the old connection and response object are lost.
Forward method is declared in RequestDispatcher Interface as forward(ServletRequest request, ServletResponse response)
Transfer of control is done internally, hence browser/ client in not involved. Request is forwarded to resources available within the same server where the request is made. On forward, the original request and response objects are transferred to the resource along with any parameters available.
Also, in forward the browser does not display the forwarded address in the address bar as it is done internally in the same server while in redirect, the redirected address is visible in the address bar.
Hence, redirect can be used when resource is in different domain/ server, otherwise forward is faster than redirect.
SendRedirect actually sends a response and status code back to the client, in which case the browser makes another request, hence, the original request object is lost. Forwarding actually passes on the same request.
- Pankaj Singh June 20, 2012