Tomcat 的 url-pattern 问题
在我的简单应用程序的 web.xml 中
<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/Hai</url-pattern>
</servlet-mapping>
,现在如果我有
<url-pattern>/*</url-pattern>
安全约束,当我尝试访问我部署的应用程序时,它会要求输入密码,但是当我将其更改为
<url-pattern>/Projekt/*</url-pattern>
并尝试输入 Projekt/Hai 时,不会询问我我的密码,为什么?
In my web.xml of my simple app i have
<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/Hai</url-pattern>
</servlet-mapping>
and now if I have
<url-pattern>/*</url-pattern>
in security constraint it asks for password when I try to get to my deployed app, but when I change it to
<url-pattern>/Projekt/*</url-pattern>
and try to enter Projekt/Hai I am not asked for my password, why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在 web.xml 中指定的 url-pattern 始终是相对于 web 应用程序的上下文路径的模式。因此,
/Projekt/*
表示/Projekt
下的所有 URL,在应用程序的上下文路径下。由于您的应用程序部署在
/Projekt
上,这意味着此 url 模式与 URLhttp://localhost:8080/Projekt/projekt/Hai
匹配。它与http://localhost:8080/Projekt/Hai
不匹配,因为当相对于上下文路径写入时,此 URL 是/Hai
,这并不匹配t 与模式/Projekt/*
匹配。好的经验法则:Web 应用程序的代码或部署描述符中的任何内容都不应该依赖于部署应用程序所选择的上下文路径。所有内容都应始终相对于此上下文路径进行指定。
The url-pattern that you specify in web.xml is always a pattern that is relative to the context path of the webapp. So,
/Projekt/*
means all the URLs under/Projekt
, under the context path of the application.Since your app is deployed un
/Projekt
, it means that this url-pattern matches the URLhttp://localhost:8080/Projekt/projekt/Hai
. It doesn't matchhttp://localhost:8080/Projekt/Hai
, because this URL, when written relatively to the context path, is/Hai
, which doesn't matches the pattern/Projekt/*
.Good rule of thumb: nothing in the code or deployment descriptor of a webapp should ever depend on the context path chosen to deploy the application. Everything should always be specified relatively to this context path.