Summary of common methods of handling static files in javaweb

  • 2020-04-01 03:27:27
  • OfStack

The example of this paper summarizes the common processing methods of static files in javaweb, which is of great practical value in the development of javaweb program. The specific methods are summarized as follows:

Method 1: activate Tomcat's defaultServlet to handle 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>

Note: to write in front of the DispatcherServlet, let the defaultServlet intercept the request first, so that the request will not go into Spring, I think the performance is the best.

The name of the default Servlet that comes with Tomcat, Jetty, JBoss, and GlassFish -- "default"
The name of the default Servlet that comes with Google App Engine -- "_ah_default"
Resin's default Servlet name - "resin-file"
The name of the default Servlet that comes with WebLogic - "FileServlet"
The name of the default Servlet that comes with WebSphere - "SimpleFileServlet"

Method 2: MVC :resources,   provided after spring3.0.4; Usage:


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

Description: / images / * * mapped to ResourceHttpRequestHandler for processing, the location specified the location of the static resources. Can be a web application root directory, the jar inside the package, so that we can put the static resources compressed into the jars. Cache-period enables static resources to be cached on the web

Method 3: using < MVC: default - servlet - handler / >


<mvc:default-servlet-handler/> 

The url "/ * *", registered with the SimpleUrlHandlerMapping urlMap, turn by static resource access HandlerMapping to org. Springframework. Web. Servlet. Resource. DefaultServletHttpRequestHandler processing and return.
DefaultServletHttpRequestHandler USES is the Servlet container own default Servlet.

Note: execution order of multiple HandlerMapping:

DefaultAnnotationHandlerMapping order attribute value is: 0
< MVC: resources / > The order attribute value of the auto-registered SimpleUrlHandlerMapping is 2147483646

< MVC: default - servlet - handler / > The order attribute value of the auto-registered SimpleUrlHandlerMapping is 2147483647

Spring will execute the order value first which is smaller. When accessing a a. pg image file, the first to find by DefaultAnnotationHandlerMapping processor, must be found, because we don't have a name is a. pg Action. Then, in ascending order of order, the last SimpleUrlHandlerMapping matches "/**", so it must match, and then it can respond to the image.
 
To access an image, you have to go through layers of matching. How is the performance?

Finally, scenarios 2 and 3, when accessing a static resource, if there is a matching (approximate) total interceptor, the interceptor will go. If you implement permission checks in intercepts, be careful to filter these requests to static files.

How your DispatcherServlet intercepts URL suffixes like *.do is not a problem. Suffixes are still handy.


Related articles: