Java forwarding and redirection instructions

Redirects and Forwards there is one important difference: When using forwarding, JSP container will use an internal call to the target page, a new page to continue processing the same request, and the browser will not know the process. In contrast, the meaning of redirection methods of notification is the first page the browser sends a new page request. Because, when you use the redirect, URL displayed in the browser will become the new page's URL, and when using forwards, the URL will remain the same. Redirect forward faster than slower, because the browser will have to issue a new request. At the same time, due to the redirection methods produce a new request, so after a redirect, the objects within the request will not be used.

How to choose a redirection or forwarding it? Often forward faster, and can keep the object within the request, so he is the first choice. However, due after forwarding, the browser URL still points to the start page, reload time if the current page, start page will be recalled. If you do not want to see this happen, then select Forward.

The difference between forwarding and redirection

Not just to the variable passed to the next page and use the session scope, that would be no reason to increase the scope of the variable, forwards may be able to help you solve this problem.

Redirect: Variable previous request stored in all the failures, and enter a new request scope.

Forward: variable stored in the previous request does not fail, just like the fight to the two pages together.

Text began:

First look different, they call are as follows:

request.getRequestDispatcher("apage.jsp").forward(request, response);//转发到apage.jsp

response.sendRedirect ( "apage.jsp"); // redirect to apage.jsp

In the jsp page you will see the following manner Forward:

<jsp:forward page="apage.jsp" />

When I was a beginner jsp, these two concepts are very vague, others see an example of the time, is confused, do not know when to use which. I hope the following explanation can help you.

Mentioned forwarding and redirection have to mention the request scope. Many beginners know when we submit a form, you create a new request. In fact, when we click on a link, or create a new request. Then act on a request in the end be? For example: There is a link <a href="b.jsp?id=1"> This is pointing to a link b, but also with a parameter </a> page a.jsp in. When we click on this link time, gave rise to a request for the sake of clarity, we call it requestA-> B. Now, we can get information from this request in b.jsp page. In b.jsp you can write out.println (request.getParameter ( "id")) for testing. Here is more complicated, we add the following statement in b.jsp page:

request.setAttribute("name","funcreal");

out.println (request.getAttriblute ( "name")); // success shows the value of the name variable.

Now in b.jsp in to add a link: <a href="c.jsp?age=23"> This is a link to c, but also with a parameter </a>, when we click on this connection time, will generate a new request, then rested requestA-B also, a new request is called requestB-C. By the same token, in c.jsp, we have access to only the variables age, because id, name both variables belong requestA-B, this time he does not exist anymore. Below is the source code:
a.jsp
<@% Page contentType = "text / HTML; GBK charset ="%>
<HTML>
<body bgcolor = "# FFFFFF">
<A the href = "b.jsp, ID. 1 =?" > point b.jsp, but also with a parameter id = 1. requestA-B now born </a>
</ body>
</ HTML>

b.jsp
<%@ page contentType="text/html; charset=GBK" %>
<html>
<body bgcolor="#ffffff">
<%
out.println("id=" + request.getParameter("id"));
request.setAttribute("name","Func Real");
out.println("name=" + request.getAttribute("name"));
%>
<a href="c.jsp?age=23">requestA-B已经结束了。指向c.jsp,而且还带了一个参数age=23</a>
</body>
</html>

c.jsp
<%@ page contentType="text/html; charset=GBK" %>
<html>
<body bgcolor="#ffffff">
<%
out.println("id=" + request.getParameter("id"));
out.println("name=" + request.getAttribute("name"));
out.println("age=" + request.getParameter("age"));
%>
</body>
</html>

Then forwarding is how it happened? Now add a page named d.jsp, and c.jsp the </ body> added to the preceding one <JSP: Page = Forward "d.jsp" />
d.jsp
<% @ Page contentType = "text / HTML; charset GBK = "%>
<HTML>
<body bgcolor =" # FFFFFF ">
requestB-C has claws out into d.jsp page
<%
Out.println (" Age = "+ request.getParameter (" Age "));
%>
</ body>
</ HTML>

Run the program, you will find that content c page does not show up, because the forward is performed automatically, although the address bar is c.jsp But in fact, but the display of the browser is already d.jsp content, and it We see the parameters passed from b.jsp over. You can simply get it this way: forward, is to extend the scope of requestB-C, <jsp: forwardpage = "d.jsp " />, this sentence is actually the c.jsp and d.jsp stick together, they are like a page.
If you used the struts, then you know why in Action, the last sentence is almost always mapping.findForward ( "xxx"); a. Because the variable requirements we set in this Action in scope will have the next page (perhaps another Action) in use, so use forward.

The following is a comparison of the request and the request is redirected RequestDispatcher.forward method HttpServletResponse.sendRedirect way to achieve the realization of forwarding a summary:

(1) RequestDispatcher.forward method can only forward the request to the component with a WEB application; and HttpServletResponse.sendRedirect method can be redirected to other resources in the current application, but also can be redirected to another on the same site application resources, or even use an absolute URL to redirect resources to other sites. If you pass the relative URL HttpServletResponse.sendRedirect methods start with "/", which is relative to the root directory of the entire WEB site; specify when you create RequestDispatcher object relative URL begins with "/", which is relative to the current WEB applications the root directory.

After (2) call redirection method HttpServletResponse.sendRedirect access process, URL displayed in the browser address bar will change, become the target URL redirection from the initial URL address; and forwards the request to call the method RequestDispatcher.forward after the process is complete, the browser address bar to keep the original URL address unchanged.

The results (3) HttpServletResponse.sendRedirect method directly respond to the request of the browser, the response is to tell the browser to re-access request to another URL.

For example: the redirection process like there is nicknamed "browser" who wrote to borrow money to find Joe Smith, Joe Smith replied that there is no money, so that "Browser" John Doe went by, and now John Doe Mailing address told to the "browser." Then, the "browser" and provide communication address to write to borrow money according to John Doe John Doe received a letter after you send your money to the "browser." Therefore, "browser" issued a total of two letters and received two replies, "browser" knows that he borrowed money from the hands of John Doe. RequestDispatcher.forward methods will be forwarded within server-side request to another resource, the browser only knows request was made and received in response to the results, did not know there had been forwarding behavior inside the server program. This process is like the nickname "browser" who wrote to borrow money to find Joe Smith, Joe Smith had no money, so Joe Smith to find John Doe borrowed some money, you can even add some of their own money, then the money remitted to the "browser." Therefore, "browser" only sent a letter and received a reply, he only knew where to borrow money from Joe Smith does not know some of the money out of the hands of John Doe.

They share the same request and response objects between a caller (4) RequestDispatcher.forward method with the caller, that they belong to the same access request and response process; and HttpServletResponse.sendRedirect method caller and callee request using respective and response objects, they belong to two separate access request and response process.

For the jump between internal resources with a WEB application, in particular, to the request before jumping some preliminary pre-processing, and use the results to be passed HttpServletRequest.setAttribute pretreatment method, it should be used RequestDispatcher.forward method.

WEB redirection between different applications, in particular, to be redirected to another resource on the WEB site, the method should make HttpServletResponse.sendRedirect.

(5) whether RequestDispatcher.forward method, or HttpServletResponse.sendRedirect method, before calling them, can not have been the actual output content to the client. If the buffer already have some content that will be removed from the buffer.

Guess you like

Origin www.linuxidc.com/Linux/2019-07/159677.htm