我想不出 getServletContext.getContextPath()
和 request.getContextPath()
会返回不同字符串的情况。
HttpServletRequest.getContextpath()文档:
It is possible that a servlet container may match a context by more than one context path. In such cases this method will return the actual context path used by the request and it may differ from the path returned by the ServletContext.getContextPath() method. The context path returned by ServletContext.getContextPath() should be considered as the prime or preferred context path of the application.
ServletContext.getContextPath()文档:
It is possible that a servlet container may match a context by more than one context path. In such cases the HttpServletRequest.getContextPath() will return the actual context path used by the request and it may differ from the path returned by this method. The context path returned by this method should be considered as the prime or preferred context path of the application.
当我的应用程序位于 Tomcat 的 webapps/ROOT
文件夹中时,两种方法都返回“”(空字符串)。
在所有其他情况下,如果我的应用程序在 myapp.war 中(放在 Tomcat 的 webapps 文件夹中),这两种方法都会按预期返回“/myapp”。
当我将 my.war
和 mymy.war
放入 Tomcat 的 webapps 文件夹中时,Tomcat 会看到两个不同的 Web 应用程序,因此我无法为这个 “servlet 容器建模可能通过多个上下文路径匹配上下文” 情况。
我不能调用我的 WARs my.war
和 my/my.war
因为在 Windows 和 Unix 上都不允许在文件名中使用“/”。
最佳答案
这是支持的,因为 it's pretty straightforward to have multiple context paths mapped to the same webapp .
这里有一些其他的方法可以达到同样的效果:
关于tomcat - getServletContext.getContextPath() 和 request.getContextPath() 什么时候可以不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59039181/