web.xml:URL映射 [英] web.xml : URL mapping

查看:186
本文介绍了web.xml:URL映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 web.xml中有这两行


< url-pattern> /< ; / url-pattern的> :Index Servlet



< url-pattern> / login< / url-pattern> :登录Servlet


但我打开 http:// localhost:8084 / login / ,它去了到 Index Servlet 当我打开 http:// localhost:8084 / login 时,它会转到登录Servlet


http:// localhost:8084 / login / 有什么区别吗?和 http:// localhost:8084 / login


我的 web.xml

I have these two lines in my web.xml

<url-pattern>/</url-pattern> : Index Servlet
and
<url-pattern>/login</url-pattern> : Login Servlet

but whem I open http://localhost:8084/login/, it goes to Index Servlet and when I open http://localhost:8084/login, it goes to Login Servlet.

Is there any difference in http://localhost:8084/login/ and http://localhost:8084/login?

My web.xml


<servlet>
    <servlet-name>Index</servlet-name>
    <servlet-class>Index</servlet-class>
</servlet>
<servlet>
    <servlet-name>Login</servlet-name>
    <servlet-class>Login</servlet-class>
</servlet>



<servlet-mapping>
    <servlet-name>Index</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>Login</servlet-name>
    <url-pattern>/login</url-pattern>
</servlet-mapping>


推荐答案

/ 的网址格式有特殊的意义。它表示默认ServletURL模式。因此每个请求 web.xml 中的任何其他更具体的网址模式匹配最终会最终结束在这个servlet中。请注意,这也包括静态文件,如普通的HTML / CSS / JS和图像文件!通常,默认Servlet已经由servlet容器本身提供(参见例如 Tomcat的 DefaultServlet 文档)。覆盖您自己的webapp中的Default Servlet应该非常谨慎,绝对不是这样。

The URL pattern of / has a special meaning. It namely indicates the "Default Servlet" URL pattern. So every request which does not match any of the other more specific URL patterns in web.xml will ultimately end up in this servlet. Note that this thus also covers static files like plain vanilla HTML/CSS/JS and image files! Normally, the "Default Servlet" is already provided by the servlet container itself (see e.g. Tomcat's DefaultServlet documentation). Overriding the "Default Servlet" in your own webapp should be done with extreme care and definitely not this way.

您需要为索引servlet提供不同的URL模式。它应该与您在< welcome-file> 中定义的那个相同。

You need to give your index servlet a different URL pattern. It should be the same as the one you definied in <welcome-file>.

所以如果是

<welcome-file-list>
    <welcome-file>index</welcome-file>
</welcome-file-list>

您需要按如下方式映射索引servlet

you need to map the index servlet as follows

<servlet-mapping>
    <servlet-name>Index</servlet-name>
    <url-pattern>/index</url-pattern>
</servlet-mapping>

根据您的特定目的,使用其他答案建议的URL重写过滤器是不必要的。

Using an URL rewrite filter as suggested by other answer is unnecessary for the particular purpose you had in mind.

这篇关于web.xml:URL映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆