Wednesday, August 02, 2006

Forward vs Redirect

 

Forward

  • forward is performed internally by the servlet
  • the browser is completely unaware that it has taken place, so its original URL remains intact
  • any browser reload will simple repeat the original request, with the original URL

Redirect

  • redirect is a two step process, where the web application instructs the browser to fetch a second URL, which differs from the original
  • a browser reload of the second URL will not repeat the original request, but will rather fetch the second URL
  • redirect is always slower than a forward, since it requires a second browser request
  • beans placed in the original request scope are not available to the second request

 

 private void redirect(
    ResponsePage aDestinationPage, 
    HttpServletResponse aResponse
  ) throws IOException {
    
    String urlWithSessionID = aResponse.encodeRedirectURL(aDestinationPage.toString());
    fLogger.fine("REDIRECT: " + Util.quote(urlWithSessionID));
    aResponse.sendRedirect( urlWithSessionID );
  }
 
  private void forward(
    ResponsePage aDestination, 
    HttpServletRequest aRequest, 
    HttpServletResponse aResponse
  ) throws ServletException, IOException {
    
    RequestDispatcher dispatcher = aRequest.getRequestDispatcher(aDestination.toString());
    dispatcher.forward(aRequest, aResponse);
  }

http://www.javapractices.com/Topic181.cjp

 

Tony

 

No comments:

Thumbs Up to GitHub Copilot and JetBrains Resharper

Having used AI tool GitHub Copilot since 08/16/2023, I’ve realized that learning GitHub Copilot is like learning a new framework or library ...