我似乎无法让我的码头使用缓存:( 我有一个只显示html的jetty服务器。 我在那里有javascripts和很多我不想每次都加载的图像。例如,我的应用程序需要在3页之间切换10秒。加载JQuery三次是耗时的,并加载徽标图片,所有这些都需要更长的时间。这就是为什么我想启用缓存。
这就是我得到的:
server = new Server(); //new Jetty Server
//setting up a ssl connection
try
{
SslContextFactory cf = new SslContextFactory();
cf.setKeyStorePath(keystore);
cf.setKeyStorePassword("password");
HttpConfiguration config = new HttpConfiguration();
config.addCustomizer(new SecureRequestCustomizer());
config.setSecureScheme("https");
config.setSecurePort(8443);
HttpConfiguration sslConfiguration = new HttpConfiguration(config);
sslConfiguration.addCustomizer(new SecureRequestCustomizer());
httpConnector = new ServerConnector(server);
httpConnector.addConnectionFactory(new HttpConnectionFactory(config));
httpConnector.setPort(suggestHttpPort());
httpConnector.setName("unsecured");
sslConnector = new ServerConnector(server,
new SslConnectionFactory(cf, HttpVersion.HTTP_1_1.toString()),
new HttpConnectionFactory(sslConfiguration));
sslConnector.setPort(suggestHttpSSLPort());
sslConnector.setName("secured");
server.setConnectors(new Connector[] { httpConnector, sslConnector });
}
catch (Exception e)
{
log.error("setting up Jetty failed", e);
}
// This is where i want to enable caching, at least that is what i have figuged out so far but its not working
DefaultServlet defaultServlet = new DefaultServlet();
ServletHolder holder = new ServletHolder(defaultServlet);
holder.setInitParameter("cacheControl", "max-age=120, public");
ServletContextHandler bb = new ServletContextHandler();
bb.addServlet(holder, "/");
server.setHandler(bb);
server.start();
我做错了什么?我想为所有内容启用缓存......
答案 0 :(得分:0)
不要直接使用ServletHolder
针对已启动的Servlet,让ServletHolder
创建并初始化类引用。
您需要ServletContextHandler
和DefaultServlet
可以使用的已定义的基本资源(这应该是指向静态文件内容根的URI)
示例:
ServletContextHandler context = new ServletContextHandler();
context.setContextPath("/");
context.setBaseResource(Resource.newResource(webRootUri));
ServletHolder holderPwd = new ServletHolder("default", DefaultServlet.class);
holderPwd.setInitParameter("dirAllowed", "true");
holderPwd.setInitParameter("cacheControl", "max-age=3600,public");
context.addServlet(holderPwd,"/");
server.setHandler(context);