JSP转发和重定向的区别

发布于 2024-11-08 14:23:52 字数 85 浏览 0 评论 0原文

请解释一下 jsp:forwardredirect 之间的区别。
每种情况都发生了什么?

Please explain the difference between jsp:forward and redirect.
What is happening in each case?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

明明#如月 2024-11-15 14:23:52
  • redirect 将响应状态设置为 302 [1],并在 Location 标头中设置新 url,并将响应发送到浏览器。然后浏览器根据 http 规范,再次向新 url 发起请求

  • forward 发出另一个请求,这完全发生在服务器上。 servlet 容器只是将相同的请求转发到目标 url,而浏览器不知道这一点。因此,在处理新 url 时,您可以使用相同的请求属性和相同的请求参数。并且浏览器不会知道 url 已更改(因为它完全发生在服务器上)


[1]:这是行业实践与标准相矛盾的一个例子。
HTTP/1.0 规范 (RFC 1945) 要求客户端执行
临时重定向(最初的描述短语是“已移动
暂时”),但流行的浏览器使用 302 实现
303 的功能请参阅其他。因此,HTTP/1.1增加了状态
代码 303 和 307 来区分这两种行为。然而,
一些 Web 应用程序和框架使用 302 状态代码,就好像它
是 303。来源

  • redirect sets the response status to 302 [1], and the new url in a Location header, and sends the response to the browser. Then the browser, according to the http specification, makes another request to the new url

  • forward happens entirely on the server. The servlet container just forwards the same request to the target url, without the browser knowing about that. Hence you can use the same request attributes and the same request parameters when handling the new url. And the browser won't know the url has changed (because it has happened entirely on the server)


[1]: This is an example of industry practice contradicting the standard.
The HTTP/1.0 specification (RFC 1945) required the client to perform a
temporary redirect (the original describing phrase was "Moved
Temporarily"), but popular browsers implemented 302 with the
functionality of a 303 See Other. Therefore, HTTP/1.1 added status
codes 303 and 307 to distinguish between the two behaviours. However,
some Web applications and frameworks use the 302 status code as if it
were the 303. Source

又爬满兰若 2024-11-15 14:23:52

我听过关于重定向和转发的有趣解释。想象一下,您需要朋友提供一些服务。提供什么服务并不重要。假设你的朋友无法帮助你,但知道谁可以。

如果他告诉你:“我无法处理这个问题,但我知道谁可以。这是他的电话号码。给他打电话。”他会重定向你的请求。

如果他告诉你:“没问题”,并亲自打电话给那个人,而不通知你让另一个人来处理你的愿望,他就会转发你的请求。然后,你的朋友就会得到你的愿望整理结果并转发给你。

I've heard interesting explanations of redirect and forward. Imagine that you need some service from your friend. It doesn't matter what service. Suppose that your friend can't help you but knows who can.

He would REDIRECT your request if he would tell you: "I can't handle this, but I know who can. Here his phone number. Call him."

He would FORWARD your request if he would tell you: "No problem" and calls that man by himself without notifying you about involving another person in handling your desire. Then, your friend will get the result of sorting out your wish and transmit it to you.

眼藏柔 2024-11-15 14:23:52

与转发相比,重定向也较慢,因为它必须经过浏览器并等待浏览器发出新请求,因此也会导致重定向后请求范围对象不可用。

Redirect is also slower compared to forward because it has to go through the browser and wait for the browser to make a new request, and also therefore causing request scope objects to be unavailable after redirect.

泪痕残 2024-11-15 14:23:52

重定向:

  1. 用户请求资源。
  2. 响应发送给用户。
  3. 这不是请求的资源,这是 HTTP 代码 302 的响应,包含所请求资源的 URL。
  4. URL 可以与请求的 URL 相同或不同。
  5. 客户端浏览器使用新的 URL 再次请求资源,这次实际的资源被发送给用户。

转发:

是将所请求的资源简单地展示给用户的过程。它完全发生在服务器端。

Redirect :

  1. User requests a resource.
  2. Response sent to the user.
  3. This is not the requested resource, this is response with HTTP code 302 and contains the URL of the requested resource.
  4. URL could be same or different from the requested URL.
  5. Client browser makes request for resource again with the new URL, this time the actual resource is sent to the user.

Forward:

It is the process of simply displaying the requested resource to the user. It happens entirely on the server side.

萧瑟寒风 2024-11-15 14:23:52

这篇文章给出了关于使用一个很好的转发与重定向的非常好的解释现实世界的例子。

送牛奶的人来要求你每月向你支付家里的费用。
在这里,房子是容器,而你是存在于其中的资源。
容器。 Milk man 是客户端或浏览器。

他要求每月向你付款,这是由
浏览器访问资源 A。如果你走进你的房子并询问你的
母亲(同一容器内的另一个资源 B)获得现金和
回来并交付给送奶工,这称为转发。

如果你让送奶工在你的房间里对你的母亲说话
房子里,或者你让送牛奶的人跟你在他家里的父亲说话。
办公室(不同的域),那么这称为重定向。

This post gives a really good explanation about forward vs redirect using a nice real world example.

The milk man comes and asks for monthly payment to you in your house.
Here house is the container and you are a resource existing in the
container. Milk man is the client or browser.

He asks for the monthly payment to you, this is the request made by
the browser to resource A. If you go inside your house and ask your
mother (another resource B inside the same container) for the cash and
come back and deliver to milkman this is called forward.

If you ask the milkman to speak himself to your mother inside your
house or you ask the milkman to speak to your father who is in his
office (different domain) then this is called redirect.

短叹 2024-11-15 14:23:52
+-------------------------------------------------------------------------+-----------------------------------------------------------------------------+
|                             Forwards   vs.                              |                                  Redirects                                  |
+-------------------------------------------------------------------------+-----------------------------------------------------------------------------+
| ServletContext.getRequestDispatcher(location).forward(request,response) | httpServletResponse.sendRedirect(location)                                  |
| Communication between pages directly                                    | Communication b/w pages are indirectly by extra round trip from HTTP client |
| Communication happens within web-container                              | Communication happens outside web-container                                 |
| Use same Request and Response Object                                    | Use different Request and Response Object                                   |
+-------------------------------------------------------------------------+-----------------------------------------------------------------------------+
+-------------------------------------------------------------------------+-----------------------------------------------------------------------------+
|                             Forwards   vs.                              |                                  Redirects                                  |
+-------------------------------------------------------------------------+-----------------------------------------------------------------------------+
| ServletContext.getRequestDispatcher(location).forward(request,response) | httpServletResponse.sendRedirect(location)                                  |
| Communication between pages directly                                    | Communication b/w pages are indirectly by extra round trip from HTTP client |
| Communication happens within web-container                              | Communication happens outside web-container                                 |
| Use same Request and Response Object                                    | Use different Request and Response Object                                   |
+-------------------------------------------------------------------------+-----------------------------------------------------------------------------+
千笙结 2024-11-15 14:23:52

当您转发请求时,

请求和响应对象都会被传输。
-url 保持不变。

当您将请求重定向到另一个 JSP/servlet 时,

请求和响应对象不会传输到新对象。
-Url 更改为新页面的目录。

When you forward a request,

-request and response objectsare transferred.
-url stays the same.

When you redirect the request to another JSP/servlets,

-request and response objects are not transferred to new object.
-Url changes to the directory of new page.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文