How to configure jetty as css/js file server with expires header?

纵然是瞬间 提交于 2020-01-05 03:17:03

问题


<Configure class="org.eclipse.jetty.server.handler.ContextHandler">
  <Set name="contextPath">/</Set>
  <Set name="resourceBase"><SystemProperty name="jetty.home" default="."/>/webapps/foo</Set>
  <Set name="handler">
    <New class="org.eclipse.jetty.server.handler.ResourceHandler">
      <Set name="cacheControl">max-age=3600,public</Set>
    </New>
  </Set>
<Set name="virtualHosts">
    <Array type="java.lang.String">
          <Item>foo.bar</Item>
    </Array>
</Set>
</Configure>

This is my configuration, but no expires header can be added to the http response.

Because there is no property related to expires header in ResourceHandler.

I found MovedContexHandler have this property, shall i use that?


回答1:


I make a silly hack to get this work done! If you have a better solution, tell me.

package org.eclipse.jetty.server.handler;

import javax.servlet.http.HttpServletResponse;

import org.eclipse.jetty.http.HttpHeaders;
import org.eclipse.jetty.util.resource.Resource;
/**
 * @author jilen.zhang@gmail.com
 * @date 12-7-6
 */
public class StaticResourceHandler extends ResourceHandler{
    private String expires = "xxx";

    @Override
    protected void doResponseHeaders(HttpServletResponse response, Resource resource, String mimeType) {
        super.doResponseHeaders(response, resource, mimeType);
        response.setHeader(HttpHeaders.EXPIRES, expires);
    }
}



回答2:


I'll do you one better. See https://stackoverflow.com/a/23307057/14731 for a Filter that injects arbitrary headers into the response.



来源:https://stackoverflow.com/questions/11383645/how-to-configure-jetty-as-css-js-file-server-with-expires-header

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