简体   繁体   English

为什么将请求转发到另一个页面时浏览器中的网址没有更改?

[英]why the url in the browser doesn't change when the request is forwarded to another page?

This is a small jsp page : 这是一个小的jsp页面:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page errorPage="errorpage.jsp" %>
<html tags>
<%-- about to be bad ! --%>
<% int x = 10/0; %>
</html tags>

The above page receives request from a servlet named FooServlet .(whose job is only to call the above jsp page). 上面的页面接收来自名为FooServlet的servlet的请求(其工作仅是调用上面的jsp页面)。 As soon as the servlet starts i see the default error page with it's message. servlet启动后,我会看到默认的错误页面及其消息。 But the url in the browser is same as that of the servlet. 但是浏览器中的url与servlet的相同。 Why is it so ? 为什么会这样呢? Like the url of the error page is still http://localhost:8080/WebTesters/FooServlet . 像错误页面的URL仍然是http://localhost:8080/WebTesters/FooServlet

Because this is part of the specification of how request forwarding works. 因为这是请求转发的工作方式规范的一部分。 A forward routes the request to the alternate resource via the server, which acts as a proxy between the client and the alternate resource. 转发服务器通过服务器将请求路由到备用资源,该服务器充当客户端和备用资源之间的代理。

This is different from a redirect, where the URL of the alternate resource is sent to the client as a header field, and the client is responsible for making a followup call to retrieve the alternate resource. 这与重定向不同,在重定向中,备用资源的URL作为标头字段发送到客户端,并且客户端负责进行后续调用以检索备用资源。

Because this is request forwarding not request redirect. 因为这是请求转发而不是请求重定向。 New Request only be made in request redirect. 新请求只能在请求重定向中进行。

In case of Request Dispatcher request is not return to browser & it directly forward to page(servlet) from current servlet(page). 在请求分派器的情况下,请求不会返回到浏览器,而是直接从当前servlet(页面)转发到page(servlet)。 So the browser url is not change. 因此浏览器的URL不会改变。 While in case of Send Redirect first request is return to browser from current page(servlet) & then forward to the specified(page). 在发送重定向的情况下,第一个请求是从当前页面(servlet)返回浏览器,然后转发到指定的(页面)。 So the url is change. 因此,网址已更改。

In sendRedirection the request url will be changed,example:www.sun.com(for java) gives us www.oracle.com.but in forwarding or including mechanism the url dont shows the requested resource. 在sendRedirection中,请求url将被更改,例如:www.sun.com(对于Java)为我们提供了www.oracle.com。,但是在转发或包含机制时,URL不显示请求的资源。

cause is when forwarding/including takes place the requested resource is within the web container. 原因是转发/包含发生时所请求的资源在Web容器内。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM