如何为Jetty9启用gzip压缩(静态和动态)?

我使用Jetty9,我想测试压缩文本文件是否会提高性能.起初我对提供脱机压缩文件很感兴趣,就像它在http://marianoguerra.org/posts/201205enable-gzip-compression-in-jetty.html中描述的那样

我的代码看起来像:

static private void set_jetty_handlers(Server server)
    {
    // gzip: I have added it to to serve off-line compressed JavaScript, CSS etc.
    // as described at: http://marianoguerra.org/posts/201205enable-gzip-compression-in-jetty.html
    ServletHolder servletHolder = new ServletHolder(new DefaultServlet());
    servletHolder.setInitParameter("gzip", "true");
    ServletContextHandler s_context_handler = new ServletContextHandler();
    s_context_handler.addServlet(servletHolder,"/*");

    ResourceHandler resource_handler = new ResourceHandler();
    resource_handler.setDirectoriesListed(false);
    resource_handler.setWelcomeFiles(new String[] {"index.html"});
    resource_handler.setResourceBase("");
    resource_handler.setCacheControl(MAX_AGE);

    // gzip: I have added it to to serve offline compressed JavaScript, CSS etc.
    resource_handler.setHandler(s_context_handler);

    // I want to work with aliases (links and symbolic links)
    ContextHandler context_handler = new ContextHandler();
    context_handler.addAliasCheck(new ContextHandler.ApproveAliases());

    HandlerList handlers = new HandlerList();
    handlers.setHandlers(new Handler[] { new my_jetty_handler(), resource_handler, new DefaultHandler() });
    context_handler.setHandler(handlers);

    server.setHandler(context_handler);
    configureThreadPool(server);
    } // set_jetty_handlers

但是当我将test.html压缩到test.html.gz时,我得到了HTTP ERROR:404.我添加的用于启用gzip压缩的行标记为gzip:in comment.

我还发现旧版本的Jetty有ResourceHandler.setMinGzipLength().它在当前的Jetty版本中不可用,但是从文档中可以实现我想要的.

如何启用gzip压缩?起初我想测试静态文件压缩,但是在那些测试之后我想在我的my_jetty_handler()中应用GzipFilter来提供动态内容,并且还不清楚如何从Java代码中执行它.

解决方法:

我可以通过以下方式为我的处理程序添加gzip压缩:

GzipHandler gzipHandlerRES = new GzipHandler();
gzipHandlerRES.setMimeTypes("text/html,text/plain,text/xml,text/css,application/javascript,text/javascript");
gzipHandlerRES.setHandler(resource_handler);

它不能用于脱机压缩文件(请求test.html应该提供test.html.gz).

上一篇:java – Jetty,JNDI,Postgresql:找不到类


下一篇:java – Jetty没有拿起我的spring应用程序