Java – how to prevent spring MVC from blocking all other servlets?

I am using spring 2.5 MVC and want to add another third-party servlet The problem is that spring MVC captures all requests, so the servlet doesn't get any requests Here is a web XML fragment:

Use springmvc.org springframework. web. servlet. DispatcherServlet 2

<servlet-mapping>
    <servlet-name>SpringMVC</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>  

<servlet>
    <description>This is the servlet needed for cache.type servlet,returns the packed resources</description>
    <display-name>PackServlet</display-name>
    <servlet-name>PackServlet</servlet-name>
   <servlet-class>net.sf.packtag.servlet.PackServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>PackServlet</servlet-name>
    <url-pattern>*.pack</url-pattern>
</servlet-mapping>

Applications really need / * mapping, and a pack: tag (third-party servlet) really needs mapping based on file extension Is it possible to tell spring not to process requests? Thank you and salute

Solution

In fact, you don't need spring to do anything. The servlet container can solve this problem for you

When matching, the servlet that sends the request depends on the matching rules defined by the URL pattern No two servlets may have the same pattern, but they may have overlapping patterns Then four rules apply:

1) Exact match takes precedence over wildcard match 2) longer path pattern takes precedence over shorter pattern 3) path match takes precedence over file type match 4) / match any unmatched content

<servlet-mapping>
  <servlet-name>PackServlet</servlet-name>
  <url-pattern>*.pack</url-pattern>
</servlet-mapping>
<servlet-mapping>
  <servlet-name>SpringMVC</servlet-name>
  <url-pattern>/</url-pattern>
</servlet-mapping>

If you use / * for spring MVC, it may match the longest path By removing *, you must follow the default servlet of the servlet specification and comply with Rule 4

Here you can find some more details.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>