What do you mean by default in Web.xml? _servlet

Source: Internet
Author: User

Ext: http://blog.csdn.net/hello5orld/article/details/9407905

This configuration <servlet-name>default</servlet-name> is often seen in web.xml files, which is used for static resources requested by the client such as pictures, JS files, and so on request to the default servlet processing, as follows:

[html]  View Plain  copy <servlet-mapping>   <servlet-name>default</servlet-name >   <url-pattern>*.jpg</url-pattern>   </servlet-mapping>   <servlet-mapping>   <servlet-name>default</servlet-name>   <url-pattern >*.png</url-pattern>   </servlet-mapping>   <servlet-mapping>    <servlet-name>default</servlet-name>   <url-pattern>*.gif</url-pattern>    </servlet-mapping>   <servlet-mapping>   <servlet-name>default </servlet-name>   <url-pattern>*.ico</url-pattern>   </servlet-mapping >   <servlet-mapping>   <servlet-name>default</servlet-name>    <url-pattern>*.gif</url-pattern>   </servlet-mapping>   &LT;SERVLet-mapping>   <servlet-name>default</servlet-name>   <url-pattern>*.js </url-pattern>   </servlet-mapping>   <servlet-mapping>   < servlet-name>default</servlet-name>   <url-pattern>*.css</url-pattern>    </servlet-mapping>  

Here are some of the information extracted from the http://elf8848.iteye.com/blog/875830

How your Dispatcherservlet intercepts URLs with suffixes such as "*.do", there is no question of accessing static resources.

If your dispatcherservlet intercepts "/" and intercepts all requests in order to achieve restful style, access to static files such as *.js,*.jpg is intercepted.

We have to solve this problem.

Objective: Static file can be accessed normally, no static file 404 can be found.


Scenario One: Activate Tomcat's defaultservlet to process static files [html]  View plain  copy <servlet-mapping>           <servlet-name>default</servlet-name>         <url-pattern>*.jpg</url-pattern>        </servlet-mapping>       <servlet-mapping>               <servlet-name>default</servlet-name>            <url-pattern>*.js</url-pattern>        </servlet-mapping>       <servlet-mapping>                <servlet-name> default</servlet-name>              < Url-pattern>*.css</url-pattern>         </servlet-mapping>        to configure multiple, each file is configured with a      

To write in front of the Dispatcherservlet, let Defaultservlet first intercept the request, so that the request will not enter spring, I think performance is the best.


Name of the default servlet with Tomcat, Jetty, JBoss, and GlassFish--"Default"
Google App Engine's default servlet name-"_ah_default"
Resin the name of the default servlet--"Resin-file"
WebLogic the name of the default servlet--"Fileservlet"
The name of the default servlet with WebSphere--"Simplefileservlet"

Option two: Mvc:resources after spring3.0.4, using methods: [HTML] View plain copy <!--access to static resource files--> <mvc:resources mapping= "/images/**" location= "/images/"/>

/images/** maps to Resourcehttprequesthandler for processing, location specifies the location of the static resource. Can be the Web application root directory, jar package, so you can compress the static resources into the jar package. Cache-period can enable static resources for Web cache

If the following error occurs, there may be no reason to configure <mvc:annotation-driven/>.
Error warning:no mapping found for HTTP request with URI [/mvc/user/finduser/lisi/770] in Dispatcherservlet with Name ' Spring MVC '

Using the <mvc:resources/> element, register the mapping URI into the simpleurlhandlermapping UrlMap,
The key is a mapping URI pattern value, and value is Resourcehttprequesthandler,
This cleverly transfers access to static resources from handlermapping to Resourcehttprequesthandler processing and returns, so it supports the Classpath directory, access to static resources within the JAR package.
Another point to note is that do not set DefaultHandler for simpleurlhandlermapping. Because the defaulthandler of the static URI is Resourcehttprequesthandler,
Otherwise, the static resources request cannot be processed.

Scenario three, using <mvc:default-servlet-handler/> [HTML] view plain copy <mvc:default-servlet-handler/>

Will be the "/**" URL, registered in the simpleurlhandlermapping UrlMap, The access to static resources is transferred from handlermapping to Org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler processing and returned.
Defaultservlethttprequesthandler use is the default servlet for each servlet container itself.

Supplemental Note: Multiple handlermapping execution order issues:

Defaultannotationhandlermapping's order attribute value is: 0

The order attribute value of the <mvc:resources/> Autoenrollment simpleurlhandlermapping is: 2147483646

<mvc:default-servlet-handler/> the Simpleurlhandlermapping order attribute value for autoenrollment is: 2147483647

Spring will first perform a smaller order value. When access to a a.jpg picture file, first through the defaultannotationhandlermapping to find the processor, must not be found, because we do not call the a.jpg action. And then in order value ascending search, because the last simpleurlhandlermapping is matching "/**", so will definitely match, you can respond to the picture.

Access to a picture, but also to walk the layer matching. I don't know what the performance is.

Finally, the second, Scheme III in Access to static resources, if there is a matching (approximate) Total interceptor, will go to the interceptor. If you implement a permission check in the interception, be careful to filter these requests for static files.

How your Dispatcherservlet intercept *.do such a URL suffix, does not save the above problem. Still have the suffix convenient.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.