在 Java Servlet 的上下文中,URL 重写和转发之间有什么区别? [英] In the context of Java Servlet what is the difference between URL Rewriting and Forwarding?

查看:21
本文介绍了在 Java Servlet 的上下文中,URL 重写和转发之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为Java web应用的开发者,什么时候需要用到URL Rewriting,URL Rewriting和Forwarding有什么区别?

As a developer of Java web application, when do I need to use URL Rewriting and what is the difference between URL Rewriting and Forwarding?

我在其他网站上搜索过,我得到了矛盾的信息,这取决于您与谁交谈,就像 SEO 人员会以不同的方式回答这个问题.

I searched on other websites, I get contradictory information depending upon whom you are talking to like SEO people would answer this question differently.

AFAIK 在这两种情况下,客户端(浏览器)都不会被通知更改,最终用户看到的 URL 与客户端从服务器返回 repose 时最初请求的 URL 完全相同.

AFAIK in both cases the client (browser) is not informed of the change and the end user sees exactly same URL that the client originally requested when the repose is returned from the server.

请注意,这个问题是在 Java Servlet API 的上下文中定义的,其中定义了转发方法和 sendRedirect 方法,其中重定向和转发是完全不同的两件事.这个问题是关于转发(由 Servlet API 中的转发方法定义)和 URL 重写之间的区别.该问题明确指出答案应该在 Java servlet 的上下文中.最重要的是我什么时候需要使用 URL 重写,同样是在开发 Java Web 应用程序的上下文中.

Please that this question is in the context of Java Servlet API in which forward method and sendRedirect methods are defined in which redirecting and forwarding are completely 2 different things. This question is about the difference between forward (as defined by forward method in the Servlet API's) and URL rewriting. The question clearly states that the answer should be in the context of Java servlet. Most importantly when do I need to use URL rewriting, again in the context of developing Java web application.

推荐答案

转发"一词在此问题中含糊不清.在 JSP/Servlet 世界中,转发"更多来自 MVC 概念,即请求 URL(在浏览器地址栏中可见)有效调用 servlet(如 web.xml 中的 URL 模式匹配)或 @WebServlet) 作为控制器准备模型并使用 JSP 作为视图来呈现模型.该 JSP 依次被转发"调用.这是由 RequestDispatcher#forward():

The term "forwarding" is ambiguous in this question. In JSP/Servlet world, "forwarding" is more known from the MVC concept that the request URL (as visible in browser address bar) effectively calls the servlet (as matched by its URL pattern in web.xml or @WebServlet) which acts as a controller to prepare the model and uses a JSP as view to present the model. That JSP in turn is been called by "forwarding". This is done by RequestDispatcher#forward():

request.getRequestDispatcher("/WEB-INF/foo.jsp").forward(request, response);

这确实没有反映浏览器地址栏中的 JSP 的 URL.这完全发生在服务器端.基本上,servlet加载"JSP 并将请求/响应传递给它,以便它可以完成生成 HTML 内容的工作.请注意,上例中的 JSP 隐藏在 /WEB-INF 文件夹中,这使得最终用户无法在浏览器地址栏中输入其完整路径.

This does indeed not reflect the JSP's URL in the browser address bar. This takes place entirely server side. Basically, the servlet "loads" the JSP and passes the request/response to it so that it can do its job of generating the HTML stuff. Note that the JSP in the above example is hidden in /WEB-INF folder which makes it inaccessible for endusers trying to enter its full path in browser address bar.

在一般的 Web 开发世界中,术语转发" 来自URL 转发",它与 URL 重定向本质上相同.这反过来确实会导致浏览器地址栏发生变化.这在 JSP/Servlet 世界中更正式地称为重定向"(尽管大多数初学者最初将其与转发"混淆).这是由 HttpServletResponse#sendRedirect():

In general web development world, the term "forwarding" is also known from "URL forwarding" which is essentially the same as URL redirection. This in turn indeed causes a change in the browser address bar. This is in JSP/Servlet world more formally known as "redirecting" (although most starters initially confuse it with "forwarding"). This is done by HttpServletResponse#sendRedirect():

response.sendRedirect("another-servlet-url");

基本上,服务器通过带有 Location 标头的 HTTP 3nn 响应告诉客户端客户端应该对给定的 Location 发出新的 GET 请求.以上实际上与以下相同:

Basically, the server tells the client by a HTTP 3nn response with a Location header that the client should make a new GET request on the given Location. The above is effectively the same as the following:

response.setStatus(302);
response.setHeader("Location", "another-servlet-url");

由于是客户端(网络浏览器)被指示执行这项工作,因此您会看到此 URL 更改反映在浏览器地址栏中.

As it's the client (the webbrowser) who is been instructed to do that job, you see this URL change being reflected back in the browser address bar.

URL 重写"一词含糊不清.在 JSP/Servlet 世界中,URL 重写"是将会话 ID 附加到 URL 的形式,以便无 cookie 浏览器仍然可以与服务器保持会话.您可能曾经在 URL 中看到过 ;jsessionid=somehexvalue 属性.这在默认情况下不会自动完成,但大多数基于 Servlet 的 MVC 框架会自动完成.这是由 HttpServletResponse#encodeURL()encodeRedirectURL():

The term "URL rewriting" is also ambiguous. In JSP/Servlet world, "URL rewriting" is the form of appending the session ID to the URL so that cookieless browsers can still maintain a session with the server. You'll probably ever have seen a ;jsessionid=somehexvalue attribute in the URL. This is by default not done automatically, but most Servlet based MVC frameworks will do it automatically. This is done by HttpServletResponse#encodeURL() or encodeRedirectURL():

String encodedURL = response.encodeURL(url); // or response.encodeRedirectURL(url)
// Then use this URL in links in JSP or response.sendRedirect().

(这又是一个模棱两可的术语.对于URL 编码",您通常会想到 百分比编码.没有为此提供 Servlet API 工具,这通常由 URLEncoder#encode() 或者,MVC 技术上更正确,在 JSP 中由 JSTL 的 或基于 servlet 的 MVC 框架提供的任何 UI 组件,例如 JSF 的 )

(Which in turn is -again- an ambiguous term. With "URL encoding" you'd normally think of percent encoding. There's no Servlet API provided facility for this, this is normally to be done by URLEncoder#encode() or, MVC-technically more correct, in the JSP by JSTL's <c:url> and <c:param> or any UI component provided by the servlet-based MVC framework, such as JSF's <h:outputLink>)

在一般的 Web 开发世界中(尤其是使用 Apache HTTPD/PHP 人员),URL 重写"更多地被称为 Apache HTTPD 的 mod_rewrite 正在做的事情:将传入的 URL 映射到具体 资源而不反映客户端的 URL 变化.在 JSP/Servlet 世界中,这也是可能的,它通常由使用 RequestDispatcher#forward()Filter 实现完成.一个众所周知的实现是Tuckey 的 URLRewriteFilter.

In general web development world (especially with Apache HTTPD / PHP folks), "URL rewriting" is more known as whatever Apache HTTPD's mod_rewrite is doing: mapping incoming URLs to the concrete resources without reflecting the URL change in the client side. In JSP/Servlet world this is also possible and it's usually done by a Filter implementation which uses RequestDispatcher#forward(). A well known implementation is the Tuckey's URLRewriteFilter.

我承认,当我刚开始使用 JSP/Servlet 时,这也让我困惑了很长时间,当然,当我扎根于 Apache HTTPD/PHP 世界时.

I admit that this has also confused me for long when I just started with JSP/Servlet, for sure while having my roots in the Apache HTTPD / PHP world.

这篇关于在 Java Servlet 的上下文中,URL 重写和转发之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆