如何在Tomcat中从客户端获取自定义标头?

时间:2015-06-10 02:00:42

标签: java tomcat servlets

当我运行以下代码块时,我只看到Tomcat中的一些请求头。我没有看到我添加的一些自定义的。

public void doGet(HttpServletRequest request, HttpServletResponse response) {
    List<String> headerNames = Collections.list((Enumeration<String>) request.getHeaderNames());
    for (String key : headerNames) {
        System.out.println("Key: " + key + " Value: " + request.getHeader(key));
    }
}

以下是我发送的标题:

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.5
Cache-Control: max-age=0
Connection: keep-alive
Cookie: test=test
Host: localhost:8080
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:37.0) Gecko/20100101 Firefox/37.0
CUSTOM_HEADER: test

以下是servlet打印出的内容(注意CUSTOM_HEADER值不存在):

Key: host Value: localhost:8080
Key: connection Value: keep-alive
Key: cache-control Value: max-age=0
Key: accept Value: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Key: user-agent Value: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.124 Safari/537.36
Key: accept-encoding Value: gzip, deflate, sdch
Key: accept-language Value: en-US,en;q=0.8

谁能告诉我我做错了什么?

1 个答案:

答案 0 :(得分:0)

发生了用户错误。我刚刚在我设置的浏览器中再次尝试发送自定义标头并且它有效。