configuring Tomcat for leveraging browser caching?

雨燕双飞 提交于 2019-12-01 02:16:27

问题


I ran Google's Page Speed on our web app to analyze and optimize our web site .

One of the many items under Web Performance Best Practices as listed in Page Speed says "To take advantage of the full benefits of caching consistently across all browsers, we recommend that you configure your web server to explicitly set caching headers and apply them to all cacheable static resources, not just a small subset (such as images). Cacheable resources include JS and CSS files, image files, and other binary object files (media files, PDFs, Flash files, etc.). In general, HTML is not static, and shouldn't be considered cacheable."

How do I configure tomcat to achieve the same ? I know it can be done via Filters by putting some HTTP headers but can we do it without touching code just by configuration ?

Edit : Just for information we use JSF 1.2 although I think this is irrelevant in context of this question.


回答1:


If you are on Tomcat7, there is a built-in filter for that. http://tomcat.apache.org/tomcat-7.0-doc/config/filter.html#Expires_Filter

We use the wonderful URlRewriteFilter to do this. No code change, just configuration to web.xml, that's all. Link and rule below.

http://tuckey.org/urlrewrite/

  <rule>
        <from>^.*\.(js|css|gif)$</from>
        <set type="expires">6 hours</set>
  </rule>


来源:https://stackoverflow.com/questions/11207515/configuring-tomcat-for-leveraging-browser-caching

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!