简体   繁体   English

Java servlet如何处理以'/'结尾的URL?

[英]How does Java servlet handle a URL that ends with '/'?

Usually, these two requests: 通常,这两个请求:

localhost:8080/test
localhost:8080/test/

have no difference. 没有区别。

However, when I add a servlet-mapping config: 但是,当我添加一个servlet-mapping配置时:

<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.html</url-pattern>
</servlet-mapping> 

then 然后

localhost:8080/test/

Does not work, it would return 404 error, I don't understand that I had added a config like this: 不起作用,它将返回404错误,我不知道我添加了这样的配置:

<servlet-mapping>
    <servlet-name>rest</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<servlet>
    <servlet-name>rest</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/rest-servlet.xml,
            /WEB-INF/interceptor-servlet.xml,
            /WEB-INF/controller-servlet.xml,
        </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

So, why would the request not go to this servlet, what is the magic in *.html ? 那么,为什么请求不转到该servlet, *.html的神奇之处是什么? The others like *.txt , *.jpeg would not cause this problem. 其他的*.txt*.jpeg不会导致此问题。

remove 去掉

<servlet-mapping>
   <servlet-name>default</servlet-name>
   <url-pattern>*.html</url-pattern>
</servlet-mapping> 

leave 离开

<servlet-mapping>
    <servlet-name>rest</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

and let Spring distinguish between rest and html requests. 让Spring区分rest和html请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM