Summary of common processing methods for static files in Javaweb _java

Source: Internet
Author: User

This article summarizes the common processing methods of static files in Javaweb, and it has practical value in the development of Javaweb programs, which are summarized as follows:

Method One: Activate Tomcat's defaultservlet to process static files

Add in Web.xml:

<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>

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

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"

Method Two: After spring3.0.4 version provides mvc:resources, use method:

<!--access to static resource files-->  
<mvc:resources mapping= "/images/**" location= "/images/"/> 

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

Method III, using <mvc:default-servlet-handler/>

<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. Do not know how the performance?

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.