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

ⅰ亾dé卋堺 提交于 2019-11-30 17:27:11

问题


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?


回答1:


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 ?




回答2:


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




回答3:


And here's just a working code with no need to figure out, guess and try. It's provided with respect to code in question and jetty 6. For jetty 7 and higher need to change Context to ServletContextHandler.

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 } );

//--- The difference with code in question starts here

DefaultServlet defaultServlet = new DefaultServlet();
ServletHolder holder = new ServletHolder(defaultServlet);
holder.setInitParameter("useFileMappedBuffer", "false");
holder.setInitParameter("cacheControl", "max-age=0, public");

Context bb = new Context();
bb.setResourceBase("src/webapp");
bb.addServlet(holder, "/");

//--- Done. Caching is off!

server.addHandler( bb );
// Run server as usual with server.run();

My sample also sets useFileMappedBuffer to false which is needed for not blocking files on a disk if you are developing on Windows by any reason.




回答4:


I use resourceHandler for static contents. Here's a code working fine on Jetty 9.

    ResourceHandler rh = new ResourceHandler();
    rh.setResourceBase([your_resource_base_dir]);
    rh.setCacheControl("no-store,no-cache,must-revalidate");


来源:https://stackoverflow.com/questions/696738/how-to-prevent-caching-of-static-files-in-embedded-jetty-instance

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