Processing of static files in javaweb, and processing of javaweb

Source: Internet
Author: User

Processing of static files in javaweb, and processing of javaweb

Solution 1: Activate Tomcat's defaservlet servlet to process static files

 Add the following 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: Write it in front of DispatcherServlet and let defaservlet servlet intercept the request first so that the request will not enter Spring. I think the performance is the best.

Tomcat, Jetty, JBoss, and GlassFish default Servlet name -- "default"
Name of the default Servlet that comes with Google App Engine -- "_ ah_default"
Default Servlet name provided by Resin -- "resin-file"
The default Servlet name provided by WebLogic -- "FileServlet"
Name of the default Servlet that comes with WebSphere -- "SimpleFileServlet"

Solution 2: mvc: resources is provided in spring3.0.4 and later versions. Usage:

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

/:/Images/** ing to ResourceHttpRequestHandler for processing. location specifies the location of static resources. it can be under the web application root directory and in the jar package, so that static resources can be compressed into the jar package. Cache-period allows static resources to be used for web cache

Solution 3: Use <mvc: default-servlet-handler/>

<mvc:default-servlet-handler/>  

The "/**" url is registered to the urlMap of SimpleUrlHandlerMapping, and the access to static resources is transferred from HandlerMapping to org. springframework. web. servlet. resource. defaultServletHttpRequestHandler process and return.
DefaultServletHttpRequestHandler is the default Servlet of each Servlet container.

  

 

Note: The execution sequence of HandlerMapping is as follows:

The attribute value of order DefaultAnnotationHandlerMapping is: 0.
<Mvc: resources/> the automatically registered order attribute value of SimpleUrlHandlerMapping is: 2147483646.

<Mvc: default-servlet-handler/> the order attribute value of automatically registered SimpleUrlHandlerMapping is: 2147483647.

Spring will first execute the order value smaller. When copying an a.jpg image file, you must first use DefaultAnnotationHandlerMapping to find the handler. It must be because we didn't call the.jpg Action. Then search by order value in ascending order. Because the last SimpleUrlHandlerMapping matches "/**", the image will be matched and the image will be responded.

 

To access an image, perform layer-by-layer matching. What is the performance?

Finally, solution 2 and solution 3 use the Interceptor to access static resources. If you implement permission check in interception, you must filter these requests for static files.

How to intercept URLs such as *. do in your DispatcherServlet does not save the above problem. There is a suffix for convenience.

 


How to operate files in JSP-Java/Web development mainly uses jsp technology. Can you tell me the details of survival of static page websites?

Oh, it's very troublesome to say. You don't have to understand it for a moment. I'll send you a message... leave your contact information ····

How to Implement Dynamic/static separation and deployment in Java web

In your Requirement Description, dynamic static separation is of little significance. Many things are not absolute.
 

Related Article

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.