1. Overview
1.概述
Occasionally, the initial HTTP Request Handler in our Java Servlet needs to delegate the Request to another resource. In these cases, we can either forward the request further or redirect it to a different resource.
偶尔,我们的Java Servlet中的初始HTTP请求处理程序需要将请求委托给另一个资源。在这种情况下,我们可以进一步转发请求或将其重定向到不同的资源。
We’ll use both mechanisms and discuss differences and best practices of each.
我们将使用这两种机制,并讨论各自的差异和最佳做法。
2. Maven Dependencies
2.Maven的依赖性
First, let’s add the Servlet Maven dependency:
首先,我们来添加Servlet的Maven依赖。
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0</version>
</dependency>
The latest version can be found here.
最新版本可以在这里找到。
3. Forward
3.前进
Let’s now jump right in and have a look at how to do a simple forward:
现在让我们直接跳进去,看看如何做一个简单的前进。
protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
RequestDispatcher dispatcher = getServletContext()
.getRequestDispatcher("/forwarded");
dispatcher.forward(req, resp);
}
We get hold of RequestDispatcher reference from parent Servlet and point it to another server resource.
我们从父级Servlet获得RequestDispatcher引用并将其指向另一个服务器资源。
Simply put, this will forward the request.
简单地说,这将转发该请求。
When a client submits a request to http://localhost:8081/hello?name=Dennis, this logic will run and the request will be forwarded to “/forwarded“.
当客户向http://localhost:8081/hello?name=Dennis提交请求时,这个逻辑将运行,请求将被转发到”/forwarded“。
4. Redirect
4.重新定向
Now that we understand the concept of forwarding, let’s have a look at a quick snippet for redirecting:
现在我们理解了转发的概念,让我们看看重定向的快速片段。
protected void doGet(HttpServletRequest req, HttpServletResponse resp){
resp.sendRedirect(req.getContextPath() + "/redirected");
}
We use original response object to redirect this request to another URL: “/redirected”.
我们使用原始响应对象将此请求重定向到另一个URL:”/redirected”.。
When a client submits a request to http://localhost:8081/welcome?name=Dennis, the request will be redirected to http://localhost:8081/redirected.
当客户向http://localhost:8081/welcome?name=Dennis提交请求时,该请求将被重定向到http://localhost:8081/redirected。。
To find out more about doing redirects in the context of Spring, have a look at our dedicated article here.
要了解有关在Spring背景下进行重定向的更多信息,请看我们专门的文章。
5. Differences
5.差异
We passed the parameter “name” with a value in both cases. Simply put, forwarded requests still carry this value, but redirected requests don’t.
在这两种情况下,我们都传递了带值的参数”name“。简单地说,转发的请求仍然带有这个值,但重定向的请求则没有。
This is because, with a redirect, the request object is different from the original one. If we still want use this parameter, we need to save it in the HttpSession object.
这是因为,在重定向中,请求对象与原来的对象不同。如果我们仍然想使用这个参数,我们需要把它保存在HttpSession对象中。
Here is a list of major differences between servlet forward and redirect:
这里列出了servlet转发和重定向的主要区别。
Forward:
前行。
- The request will be further processed on the server side
- The client isn’t impacted by forward, URL in a browser stays the same
- Request and response objects will remain the same object after forwarding. Request-scope objects will be still available
Redirect:
重定向。
- The request is redirected to a different resource
- The client will see the URL change after the redirect
- A new request is created
- Redirect is normally used within Post/Redirect/Get web development pattern
6. Conclusion
6.结语
Forwarding and redirecting are both about sending a user to different resources, although they have quite different semantics.
转发和重定向都是将用户发送到不同的资源,尽管它们的语义完全不同。
Picking between these is simple. If the previous scope is required, or the user doesn’t need to be informed, but the application also wants to perform an internal action then use forwarding.
在这两者之间做出选择很简单。如果需要前一个范围,或者不需要通知用户,但应用程序还想执行一个内部动作,则使用转发。
To discard the scope or if the new content isn’t associated with the original request – such as a redirect to a login page or completing a form submission – then use redirecting.
要放弃这个范围,或者如果新的内容与原始请求无关–比如重定向到一个登录页面或完成一个表单提交–则使用重定向。
As always, the example code can be found over on GitHub.
一如既往,可以在GitHub上找到示例代码。