1. Introduction
1.绪论
DispatcherServlet plays a significant role in Spring applications and provides a single entry point for the application. Whereas the context path defines the URL that the end-user will access the application.
DispatcherServlet在Spring应用程序中发挥着重要作用,为应用程序提供了一个单一的入口。而上下文路径定义了终端用户将访问应用程序的URL。
In this tutorial, we’re going to learn about the differences between context path and servlet path.
在本教程中,我们将了解上下文路径和Servlet路径之间的区别。
2. Context Path
2.内涵路径
Simply put, the context path is a name with which a web application is accessed. It is the root of the application. By default, Spring Boot serves the content on the root context path (“/”).
简单地说,上下文路径是访问网络应用的一个名称。它是应用程序的根。默认情况下,Spring Boot在根上下文路径(”/”)上提供内容。
So, any Boot application with default configuration can be accessed as:
因此,任何具有默认配置的Boot应用程序都可以被访问为。
http://localhost:8080/
However, in some cases, we may wish to change the context of our application. There are multiple ways to configure the context path, and application.properties is one of them. This file resides under the src/main/resources folder.
然而,在某些情况下,我们可能希望改变我们应用程序的上下文。有多种方法可以配置上下文路径,而application.properties就是其中之一。这个文件位于src/main/resources文件夹下。
Let’s configure it using the application.properties file:
让我们使用application.properties文件对其进行配置。
server.servlet.context-path=/demo
As a result, the application main page will be:
因此,应用程序的主页面将是。
http://localhost:8080/demo
When we deploy this application to an external server, this modification helps us to avoid accessibility issues.
当我们把这个应用程序部署到外部服务器时,这种修改有助于我们避免可访问性问题。
3. Servlet Path
3.服务器路径
The servlet path represents the path of the main DispatcherServlet. The DispatcherServlet is an actual Servlet, and it inherits from HttpSerlvet base class. The default value is similar to the context path, i.e. (“/”):
servlet路径代表主DispatcherServlet的路径。DispatcherServlet是一个实际的Servlet,它继承于HttpSerlvet基类。默认值类似于上下文路径,即(”/”):。
spring.mvc.servlet.path=/
In the earlier versions of Boot, the property was in the ServerProperties class and known as server.servlet-path=/.
在Boot的早期版本中,该属性在ServerProperties类中,称为server.servlet-path=/。
From 2.1.x, this property is moved to the WebMvcProperties class and renamed as spring.mvc.servlet.path=/.
从2.1.x开始,这个属性被移到WebMvcProperties类中,并改名为spring.mvc.servlet.path=/。
Let’s modify the servlet path:
我们来修改一下servlet路径。
spring.mvc.servlet.path=/baeldung
Because a servlet belongs to a servlet context, changing the context path will affect the servlet path too. So, after modifications, the application servlet path will become http://localhost:8080/demo/baeldung.
由于servlet属于servlet上下文,改变上下文路径也会影响servlet路径。因此,修改后,应用程序的Servlet路径将变成http://localhost:8080/demo/baeldung.。
In other words, if a style sheet was being served as http://localhost:8080/demo/style.css, now will serve as http://localhost:8080/demo/baeldung/style.css.
换句话说,如果一个样式表被作为http://localhost:8080/demo/style.css,现在将作为http://localhost:8080/demo/baeldung/style.css。。
Usually, we don’t configure the DispatcherServlet by ourselves. But, if we really need to do it, we have to provide the path of our custom DispatcherServlet.
通常情况下,我们不会自己配置DispatcherServlet。但是,如果我们真的需要这样做,我们必须提供我们自定义DispatcherServlet的路径。
4. Conclusion
4.总结
In this quick article, we looked at the semantics of context path and servlet path. We also saw what these terms represent and how they work together in a Spring application.
在这篇快速文章中,我们看了上下文路径和Servlet路径的语义。我们还看到了这些术语所代表的内容,以及它们在Spring应用程序中是如何协同工作的。