Forwarding, Inclusion, Redirection of Javaweb

1. Forwarding and Inclusion

RequestDispatcher dispatcher = request.getRequestDispatcher("/MyServlet");  

Request forwarding: dispatcher.forward(request,response)

The response body is completed by the next servlet, and the current servlet can set the response header! (Leave the head but not the body)

Request includes: dispatcher.include(request, response)

The response body and response headers are not completed jointly by the two servlets.

Request forwarding and request inclusion are all within one request scope! Use the same request and response!

2 redirect

response.sendRedirect("/Javaweb01/MyServlet");

[The difference between request forwarding and redirection]
Request forwarding is one request and one response, while redirection is two requests and two responses. The request forwarding
address bar does not change, while the redirection will display the address of the next request.
Other servlets in this project, and redirection can be redirected to other servlets in this project, and can also be directed to other projects. Servlet
request forwarding is a server-side behavior, just give the forwarded servlet path, and redirection needs to give the requestURI, that is Include the project name!

Request transfer is more efficient than redirection! Because it is a request!

Need the address bar to change, then use redirects!
If you need to get the data in the request field in the next servlet, use forwarding!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325284142&siteId=291194637