1. Overview
1.概述
In this quick tutorial, we’ll discuss the difference between getRequestURI() and getPathInfo() in the HttpServletRequest class.
在这个快速教程中,我们将讨论getRequestURI()和getPathInfo()在HttpServletRequest类中的区别。
2. Difference Between getRequestURI() and getPathInfo()
2.getRequestURI()和getPathInfo()之间的区别
The function getRequestURI() returns the complete requested URI. This includes the deployment folder and servlet-mapping string. It will also return all extra path information.
函数getRequestURI() 返回完整的请求URI。这包括部署文件夹和servlet-mapping字符串。它还将返回所有额外的路径信息。
The function getPathInfo() only returns the path passed to the servlet. If there is no extra path information passed, this function will return null.
函数getPathInfo()只返回传递给Servlet的路径。如果没有传递额外的路径信息,该函数将返回null。
In other words, if we deploy our application in our web server’s root, and we request the servlet mapped to “/”, both getRequestURI() and getPathInfo() will return the same string. Otherwise, we’ll get different values.
换句话说,如果我们在Web服务器的根部部署我们的应用程序,并且我们请求映射到”/”的servlet,getRequestURI()和getPathInfo()都将返回相同的字符串g。否则,我们会得到不同的值。
3. Example Request
3.请求示例
In order to get a better understanding of the HttpServletRequest methods, let’s say we have a servlet which can be accessed via this URL:
为了更好地理解HttpServletRequest方法,假设我们有一个servlet,可以通过这个URL访问。
http://localhost:8080/deploy-folder/servlet-mapping
This request will hit the “servlet-mapping” servlet in a web application deployed inside “deploy-folder”. Therefore, if we call getRequestURI() and getPathInfo() for this request, they will return different strings.
这个请求将击中部署在 “deploy-folder “中的Web应用程序中的 “servlet-mapping “小程序。因此,如果我们为这个请求调用getRequestURI()和getPathInfo(),它们将返回不同的字符串。
Let’s create a simple doGet() servlet method:
让我们创建一个简单的doGet()Servlet方法。
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
PrintWriter writer = response.getWriter();
if ("getPathInfo".equals(request.getParameter("function")) {
writer.println(request.getPathInfo());
} else if ("getRequestURI".equals(request.getParameter("function")) {
writer.println(request.getRequestURI());
}
writer.flush();
}
Firstly, let’s take a look at the output of the servlet for getRequestURI requests fetched by curl command:
首先,让我们看看Servlet对curl命令获取的getRequestURI请求的输出。
curl http://localhost:8080/deploy-folder/servlet-mapping/request-path?function=getRequestURI
/deploy-folder/servlet-mapping/request-path
Similarly, let’s take a look at the output of the servlet for getPathInfo:
同样地,让我们看看Servlet对getPathInfo的输出。
curl http://localhost:8080/deploy-folder/servlet-mapping/request-path?function=getPathInfo
/request-path
4. Conclusion
4.结论
In this article, we’ve explained the difference between getRequestURI() and getPathInfo() in HttpServletRequest. We also demonstrated it with an example servlet and request.
在这篇文章中,我们已经解释了HttpServletRequest中的getRequestURI()和getPathInfo()之间的区别。我们还用一个Servlet和请求的例子进行了演示。
As always, the servlet implemented to test all these functions is available over on Github.
一如既往,为测试所有这些功能而实施的Servlet可在Github上获取。