Servlet-- request forwarding / redirection

One or two jump method difference:

classification

Request forwarding

Redirect

Object ownership

request

response

url

/ Connection name

/ Application name / connection name

Jump range

This application

This application or external

The browser address

constant

Variety

Transfer request

Transfer request

Re-request

speed

fast

slow

 Second, the request is forwarded:

  (1request.getRequestDispatcher("/BServlet").forward(request, response);

  (2request.getRequestDispatcher("/BServlet").includ(request, response);

    // If a forward jump back response of the output is not performed;

    // and use include to jump, then include the servlet executed, and then returns to the original output servlet execution response (if any);

  important point:

  (1) must be a jump in the current application;

  (2) url must be the shortest path to the resource;

    // For the servlet is urlparrent; for html html page name is .html

  Address unchanged (3) forward; is a request;

 Third, the redirection:

  // response code 302: indicates redirection;

  1. redirected created:

    (1) setting the status code and the response header;

            response.setStatus(302);

            response.setHeader("Location", "http://www.baidu.com");

    ( 2 ) the sendRedirect () Method:

      response.sendRedirect("http://www.baidu.com");

       //response.sendRedirect("/hello/BServlet");

  2. Redirect Summary:

  (1) is twice redirected request;

  (2) redirect url is not limited to this application, it may be other applications;

  (3 do not use the post) redirection response.getWriter () or response.getOutputStream () output data;

 

Guess you like

Origin www.cnblogs.com/Tractors/p/11279038.html