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

问题描述

作为Java Web应用程序的开发人员,我何时需要使用URL重写以及URL重写和转发之间的区别?

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?

我在其他网站上搜索,根据你的谈话对象,我会得到相互矛盾的信息,比如搜索引擎优化的人会以不同的方式回答这个问题。

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。

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的上下文中,其中定义了forward方法和sendRedirect方法,其中重定向和转发完全是两个不同的东西。这个问题是关于forward(由Servlet API中的forward方法定义)和URL重写之间的区别。问题清楚地表明答案应该在Java servlet的上下文中。最重要的是,在开发Java Web应用程序的上下文中,我何时需要使用URL重写。

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模式匹配) / code>或)作为控制器来准备模型并使用JSP作为视图来呈现模型。反过来,JSP被称为转发。这是通过:

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世界中更正式地称为重定向(尽管大多数初学者最初将其与转发混淆)。这是由:

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");

基本上,服务器通过带有位置的HTTP 3nn响应告诉客户端客户端应在给定位置上发出新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");

因为它是指示执行该工作的客户端(webbrowser),所以您会看到此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框架都会自动完成。这是由或:

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().

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

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重写和转发有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 09:04