tomcat - getServletContext.getContextPath() 和 request.getContextPath() 什么时候可以不同?

标签 tomcat servlets jakarta-ee contextpath

我想不出 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.warmymy.war 放入 Tomcat 的 webapps 文件夹中时,Tomcat 会看到两个不同的 Web 应用程序,因此我无法为这个 “servlet 容器建模可能通过多个上下文路径匹配上下文” 情况。

我不能调用我的 WARs my.warmy/my.war 因为在 Windows 和 Unix 上都不允许在文件名中使用“/”。

最佳答案

关于tomcat - getServletContext.getContextPath() 和 request.getContextPath() 什么时候可以不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59039181/

相关文章:

tomcat - 使用 Tomcat 开发

java - Cookie 与 jsessionid url token 优先级

jakarta-ee - Spring @Profile 的 EJB 对应项

java - 在 XML 配置中引用 JavaConfig 时获取 NoSuchBeanDefinitionException

java - 使用 GlassFish 进行分布式 session

eclipse 错误 : Websockets are currently only supported in Tomcat

java - 使用 BASIC 身份验证集中保护所有 tomcat webapps

java - 当我从 java servlet 传递错误消息时,CSS 不工作

java - 当应用程序部署在 Eclipse 外部时,调用 JSP 文件会出现 NoSuchMethodError

java - 如何访问自定义 taglib Java 类中的 JSP 隐式对象?