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,443
  • 36
  • 114
  • 172
siva636
  • 16,109
  • 23
  • 97
  • 135

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
  • 13,052
  • 4
  • 54
  • 77
2

One way is using HttpServletResponseWrapper

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

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
jmj
  • 237,923
  • 42
  • 401
  • 438
  • 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,384
  • 10
  • 20