Tomcat 的 url-pattern 问题

发布于 2025-01-03 14:05:43 字数 610 浏览 0 评论 0原文

在我的简单应用程序的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉南桥 2025-01-10 14:05:43

您在 web.xml 中指定的 url-pattern 始终是相对于 web 应用程序的上下文路径的模式。因此,/Projekt/* 表示 /Projekt 下的所有 URL,在应用程序的上下文路径下

由于您的应用程序部署在 /Projekt 上,这意味着此 url 模式与 URL http://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 URL http://localhost:8080/Projekt/projekt/Hai. It doesn't match http://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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文