Java response.sendRedirect() 和 request.getRequestDispatcher().forward(request,response) 有什么区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20371220/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 01:26:18  来源:igfitidea点击:

What is the difference between response.sendRedirect() and request.getRequestDispatcher().forward(request,response)

javajsprequestresponseforward

提问by roger

I have got a problem with my page jump when I use JAVA, if I use:

当我使用 JAVA 时,我的页面跳转有问题,如果我使用:

response.sendRedirect("login.jsp")

then I get this url: http://localhost:8080/login.jsp

然后我得到这个网址: http://localhost:8080/login.jsp

But if I use

但如果我使用

request.getRequestDispathcer("login.jsp").forward(request, response)

then I get this url: http://localhost:8080/Shopping/login.jsp(the "Shopping" is the name of my module).

然后我得到这个网址:(http://localhost:8080/Shopping/login.jsp“购物”是我的模块的名称)。

What's the difference?

有什么不同?

采纳答案by Keerthivasan

To simply explain the difference,

简单解释一下区别,

  response.sendRedirect("login.jsp");

doesn't prepend the contextpath (refers to the application/module in which the servlet is bundled)

不预先设置上下文路径(指捆绑 servlet 的应用程序/模块)

but, whereas

但是,而

 request.getRequestDispathcer("login.jsp").forward(request, response);

will prepend the contextpath of the respective application

将在相应应用程序的上下文路径前面加上

Furthermore, Redirect requestis used to redirect to resources to different servers or domains. This transfer of control task is delegated to the browser by the container. That is, the redirect sends a header back to the browser / client. This header contains the resource url to be redirected by the browser. Then the browser initiates a new request to the given url.

此外,重定向请求用于将资源重定向到不同的服务器或域。这个控制权转移任务由容器委托给浏览器。也就是说,重定向将标头发送回浏览器/客户端。此标头包含浏览器要重定向的资源 url。然后浏览器向给定的 url 发起一个新的请求。

Forward requestis used to forward to resources available within the server from where the call is made. This transfer of control is done by the container internally and browser / client is not involved.

转发请求用于转发到进行调用的服务器中可用的资源。这种控制转移是由容器内部完成的,不涉及浏览器/客户端。

回答by sprite

1.redirect return the request to the browser from server,then resend the request to the server from browser.

1.redirect 将请求从服务器返回给浏览器,然后从浏览器向服务器重新发送请求。

2.forward send the request to another servlet (servlet to servlet).

2.forward 将请求发送到另一个servlet(servlet to servlet)。

回答by Lijo

Redirect and Request dispatcher are two different methods to move form one page to another. if we are using redirect to a new page actually a new request is happening from the client side itself to the new page. so we can see the change in the URL. Since redirection is a new request the old request values are not available here.

重定向和请求调度程序是将一个页面移动到另一个页面的两种不同方法。如果我们使用重定向到一个新页面,实际上一个新请求正在从客户端本身发生到新页面。所以我们可以看到 URL 的变化。由于重定向是一个新请求,因此旧请求值在此处不可用。

回答by Anish Rai

forward

向前

Control can be forward to resources available within the server from where the call is made. This transfer of control is done by the container internally and browser / client is not involved. This is the major difference between forward and sendRedirect. When the forward is done, the original request and response objects are transfered along with additional parameters if needed.

控制可以转发到进行调用的服务器内可用的资源。这种控制转移是由容器内部完成的,不涉及浏览器/客户端。这是 forward 和 sendRedirect 之间的主要区别。转发完成后,原始请求和响应对象将与其他参数一起传输(如果需要)。

redirect

重定向

Control can be redirect to resources to different servers or domains. This transfer of control task is delegated to the browser by the container. That is, the redirect sends a header back to the browser / client. This header contains the resource url to be redirected by the browser. Then the browser initiates a new request to the given url. Since it is a new request, the old request and response object is lost.

控制可以重定向到不同服务器或域的资源。这个控制权转移任务由容器委托给浏览器。也就是说,重定向将标头发送回浏览器/客户端。此标头包含浏览器要重定向的资源 url。然后浏览器向给定的 url 发起一个新的请求。由于是新的请求,旧的请求和响应对象都丢失了。

For example, sendRedirect can transfer control from http://google.comto http://anydomain.combut forward cannot do this.

例如,sendRedirect 可以将控制权从http://google.com转移到http://anydomain.com,但是 forward 不能这样做。

‘session' is not lost in both forward and redirect.

“会话”在转发和重定向中都不会丢失。

To feel the difference between forward and sendRedirect visually see the address bar of your browser, in forward, you will not see the forwarded address (since the browser is not involved) in redirect, you can see the redirected address.

要直观感受forward和sendRedirect的区别,看你浏览器的地址栏,在forward中,你不会看到redirect中的转发地址(因为浏览器没有参与),你可以看到重定向的地址。

回答by Maulik Kakadiya

Simply difference between Forward(ServletRequest request, ServletResponse response) and sendRedirect(String url) is

Forward(ServletRequest request, ServletResponse response) 和sendRedirect(String url)之间的简单区别是

forward():

向前():

  1. The forward()method is executed in the server side.
  2. The request is transfer to other resource within same server.
  3. It does not depend on the client's request protocol since the forward ()method is provided by the servlet container.
  4. The request is shared by the target resource.
  5. Only one call is consumed in this method.
  6. It can be used within server.
  7. We cannot see forwarded message, it is transparent.
  8. The forward() method is faster than sendRedirect()method.
  9. It is declared in RequestDispatcherinterface.
  1. forward()方法在服务器端执行。
  2. 请求被转移到同一服务器内的其他资源。
  3. 它不依赖于客户端的请求协议,因为该forward ()方法是由 servlet 容器提供的。
  4. 该请求由目标资源共享。
  5. 此方法仅消耗一次调用。
  6. 它可以在服务器内使用。
  7. 我们看不到转发的消息,它是透明的。
  8. forward() 方法比sendRedirect()方法快。
  9. 它在RequestDispatcher接口中声明。

sendRedirect():

发送重定向():

  1. The sendRedirect()method is executed in the client side.
  2. The request is transfer to other resource to different server.
  3. The sendRedirect()method is provided under HTTPso it can be used only with HTTPclients.
  4. New request is created for the destination resource.
  5. Two request and response calls are consumed.
  6. It can be used within and outside the server.
  7. We can see redirected address, it is not transparent.
  8. The sendRedirect()method is slower because when new request is created old request object is lost.
  9. It is declared in HttpServletResponse.
  1. sendRedirect()方法在客户端执行。
  2. 请求被转移到其他资源到不同的服务器。
  3. sendRedirect()方法是在下面提供的,HTTP因此它只能用于HTTP客户端。
  4. 为目标资源创建新请求。
  5. 消耗了两个请求和响应调用。
  6. 它可以在服务器内部和外部使用。
  7. 我们可以看到重定向的地址,它是不透明的。
  8. sendRedirect()方法较慢,因为创建新请求时旧请求对象丢失。
  9. 它在HttpServletResponse.