1

I want to set the "Expires" HTTP response header when my Glassfish server serves static resources like Javascript and image files. (because to force the browser to cache them)

How can I do this in Glassfish V 3.0.1 server?

javamonkey79
  • 17,217
  • 36
  • 108
  • 170
siva636
  • 15,428
  • 23
  • 93
  • 131

3 Answers3

2

Glassfish uses catalina for servlet container. Extend the DefaultServlet, override something like doGet() and add the headers you need.

http://tomcat.apache.org/tomcat-7.0-doc/api/org/apache/catalina/servlets/DefaultServlet.html

Mount your own default servlet to /:

   <servlet-mapping>
       <servlet-name>mydefault</servlet-name>
       <url-pattern>/</url-pattern>
   </servlet-mapping>
Martin Algesten
  • 12,267
  • 3
  • 50
  • 75
2

One way is using HttpServletResponseWrapper

Another approch is using filter , here is very good article :Caching static resources in glassfish

Bill the Lizard
  • 386,424
  • 207
  • 554
  • 861
jmj
  • 232,312
  • 42
  • 391
  • 431
  • Filter Servlets not working for me. If I set the header in the filter, it is then overridden automatically somewhere in some stage latter in my JSF application. – siva636 Dec 03 '10 at 12:09
  • @MISS_DUKE - did you ever find a fix for this? I'm hitting the same problem myself now. Thanks. – Oversteer Jan 16 '12 at 22:04
  • Afraid not - the problem is that when I set the ETag, Last-Modified & Cache-Control headers only the Cache-Control header gets sent in the response. The DefaultServlet seems to overwrite the ETag and Last-Modified headers, which is unfortunate because they're getting set to (or a derivative of) the deployment time. No way can I get, say, jsf.js to cache. – Oversteer Jan 17 '12 at 07:43
2

Are you referencing resources from secure page?? Then The answer of Jigar Joshi i.e.

http://blogs.oracle.com/cwebster/entry/caching_static_resources_in_glassfish

will not work...

If you are having secure page then following will help you out.

Static resources are not cached referenced from glassfish secure page

Community
  • 1
  • 1
Jitesh
  • 1,374
  • 10
  • 19