Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent caching of static files in embedded Jetty instance?

I want to prevent my CSSs from being cached on the browser side. How can I do it in embedded Jetty instance?

If I were using xml configuration file, I would add lines like:

<init-param>
  <param-name>cacheControl</param-name>
  <param-value>max-age=0,public</param-value>
</init-param>

How I can turn that into the code?

Right now I start Jetty this way:

BasicConfigurator.configure();

Server server = new Server();
SocketConnector connector = new SocketConnector();
// Set some timeout options to make debugging easier.
// 1 hour
connector.setMaxIdleTime( 1000 * 60 * 60 );
connector.setSoLingerTime( -1 );
connector.setPort( 8081 );
server.setConnectors( new Connector[] { connector } );

WebAppContext bb = new WebAppContext();
bb.setServer( server );
bb.setContextPath( "/" );
bb.setWar( "src/webapp" );

server.addHandler( bb );

I think I should search setControlCache somewhere in the WebAppContext area of responsibility.

Any advices on this?

like image 314
Sergey Avatar asked Mar 30 '09 10:03

Sergey


2 Answers

I normally use a ServletHolder, like this:

WebAppContext context = new WebAppContext();
ServletHolder servletHolder = new ServletHolder(MyServlet.class);
servletHolder.setInitParameter("cacheControl","max-age=0,public"); 
context.addServlet(servletHolder, "myservletpath");

While this does not exactly match your code you should be able to figure it out from there ?

like image 197
krosenvold Avatar answered Sep 28 '22 02:09

krosenvold


Duh, how to do just the opposite How to configure cache for static resources in web.xml for Jetty??

like image 39
Martin Avatar answered Sep 28 '22 03:09

Martin