Jetty drops Cache-Control

独自空忆成欢 提交于 2020-01-07 06:48:31

问题


I'm having problem with deploying servlet on Jetty. It seems that Jetty simply drops Cache-Control (and Pragma) headers produced in servlet.

public abstract class Servlet extends HttpServlet {
  ...
  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    ...
    resp.setHeader("Cache-Control", "private, no-cache");
    resp.setHeader("Pragma", "no-cache");
    ...
  }
  ...
}

All headers (esp. Cache-Control and Pragma) are as expected when I deploy such servlet into Tomcat. But Jetty seems to swallow these headers.

Any advice?


回答1:


Using the Jetty Distribution 8.0.3.v20111011

With test servlet

package example;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
public class CacheServlet extends HttpServlet
{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {
        resp.setHeader("Cache-Control", "private, no-cache");
        resp.setHeader("Pragma", "no-cache");

        resp.setContentType("text/plain");
        resp.getWriter().println("Hello Cache Test");
    }
}

It works here, this is what the Chrome network inspection panel shows.

HTTP/1.1 200 OK
Date: Mon, 06 Oct 2014 14:45:05 GMT
Cache-Control: private, no-cache
Pragma: no-cache
Content-Type: text/plain;charset=ISO-8859-1
Content-Length: 17
Server: Jetty(8.0.3.v20111011)

And this is what curl shows ...

$ curl --dump-header - http://localhost:8080/cachetest/cachetest
HTTP/1.1 200 OK
Date: Mon, 06 Oct 2014 14:47:51 GMT
Cache-Control: private, no-cache
Pragma: no-cache
Content-Type: text/plain;charset=ISO-8859-1
Content-Length: 17
Server: Jetty(8.0.3.v20111011)

Hello Cache Test

It works.

Apparently something else is removing those headers from your response. Look for things like a filter, a framework library, a cache layer, a transparent proxy, a normal proxy, load balancers, and/or network hardware as your source for your seemingly removed headers.



来源:https://stackoverflow.com/questions/26213870/jetty-drops-cache-control

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