Jetty drops Cache-Control

2019-08-27 03:06发布

问题:

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.