Forward & Redirect

xiaoxiao2021-03-06  66

1. The purpose of both is to achieve the sharing of HTTPSERVLETREQUST objects

2, httpservletresponse.sendRedirect (), the container will send the new URL to the client, and the client will happen, so all the Request Attributes object placed in httpservletRequest will be invalid.

RequestDispatcher.Forward () is in the container that does not send the URL to the client, and does not generate a new request, so the Request Attributes object placed in httpservletRequest will not be invalid.

3, the URL is different

Redirect is an absolute path because it is a client.

String contextPath = Req.getContextPath (); string redirectstr = contextPath "/Result.jsp?username=foo&password=bar";resp.sendredirect(resp.encoderedirecturl(RedirectStr);

Forward is because it is in the container, it is a relative path.

String forwardstr = "/Result.jsp?username=foo&password=bar";RequestDispatcher dispatcher = req.getRequestDispatcher (ForwardStr); Dispatcher.Forward (Req, ResP);

转载请注明原文地址:https://www.9cbs.com/read-120440.html

New Post(0)