The difference Servlet request forwarding and redirection

Servlet forward the request:

Two resource file corresponding to a request and a response. Forwarding is in effect reach the server. The browser is imperceptible.

Forwarding sample code:

RequestDispatcher rd=request.getRequestDispatcher(/url-pattern”);
rd.forward(request,response);
//一般情况下,我们把两行代码合并成一行。
request.getRequestDispatcher(/url-pattern”).forward(request,response);
//如果要转发到一个html
request.getRequestDispatcher(/html的路径”).forward(request,response);

Redirect

Action to jump to jsp us by forwarding hops past. Jump Servlet provides two ways to redirect is in addition to another jump forward out of the way.
Redirects and Forwards achieve the effect is the same.
Redirection request and response process will be twice . After RegistAction run, it will give the browser a redirect response instructions. After the browser receives the redirect instruction, it will automatically request. Users do not have to do anything.

Redirect code:

response.sendRedirect(/appName/url-pattern”);

Forwarding and redirection of the difference between:
the code request response 1. forwarding and redirection code. Forwarding parameters are url-pattern. The redirection is / appName / url-pattern
procedure 2. forwarded requests and responses only once. Redirection process will produce two requests and responses.
3. forwarding address bar shows the path before forwarding resources. Redirection address bar shows the path of the resource after the redirect.
4. Since the redirection request will generate two objects, so when using the redirection request scope not be effective.
5. Redirect refresh, will not cause abnormal database data.

When a forward, when to use redirection.
If you need to use a page where the request scope inside the data, you must use forward. Another point of understanding, if action to give jsp transfer data, be sure to use forward.
There is no need to transfer data, then we can consider the use of redirection.
Additions and deletions, use redirection.
Query uses forward.

The difference between forwarding and redirection

Guess you like

Origin blog.csdn.net/MacWx/article/details/92384605