0

Developed application using GWT which contains lot of static files(javascript,css,images) which i want to cache for 30 days. I read lot of blogs but didn't get any clue.

- How to cache static files? - What are the possible option to achieve caching (do i need to configure in server or GWT application, here i am using glassfish/payara server for deployment)

Any idea? Note: I want do achieve this with minimal code changes, even i read this Client side caching in GWT but don't want to go with dispatcher approach

Community
  • 1
  • 1
moh
  • 1,426
  • 2
  • 16
  • 43

1 Answers1

1

You will need to add something like the ExpiresFilter to your servlet container.

I'm adding the details for the configuration from the link mentioned above just in case the content of the link goes away:

You will need to edit web.xml to add a filter and a filter mapping:

<filter>
    <filter-name>ExpiresFilter</filter-name>
    <filter-class>server.ExpiresFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>ExpiresFilter</filter-name>
    <url-pattern>/images/\*</url-pattern> <!-- these patterns should match cached resources -->
    <url-pattern>/resources/\*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
</filter-mapping> 

If you need a more portable approach for use in different application servers, you might end up writing a generic servlet Filter like I did.

After all that is no big deal as you just have set the Expires and Cache-Control: max-age headers for HttpServletRequests on your given paths.

As a starter, I stumpled upon this implementation and how Tomcat does it.

  • Thanks sebastain(+1)..what will do? is it mandatory?here i m getting one more issue.cache.html files is giving 404 but i want to cache this file as well – moh Apr 19 '17 at 10:52
  • I think the example for Glassfish uses it to apply the filter also to forwarded requests as `REQUEST` is the default. This [site](https://docs.oracle.com/cd/E19575-01/819-3669/bnagf/index.html) clarifies it a bit. I think for your use case you could just leave out both `dispatcher` lines. But a 404 should not result from the filter, it just tells your browser to not request the file again within a given period of time. – Sebastian Baumhekel Apr 19 '17 at 18:24