java 自定义标头未插入到 servlet 的请求中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3763015/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 03:16:51  来源:igfitidea点击:

Custom header not inserted in request in servlet

javaservletshttp-headers

提问by xain

There's a thrird party app that needs to get information via custom http headers, so I wrote a simple test app that creates this headers and then redirects to a page that lists all headers.

有一个第三方应用程序需要通过自定义 http 标头获取信息,因此我编写了一个简单的测试应用程序来创建此标头,然后重定向到列出所有标头的页面。

The header-generating servlet snippet is:

生成标头的 servlet 片段是:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/plain");
    response.setHeader("cust-header", "cust-val");
    response.sendRedirect("header.jsp");
}

On the other hand, the relevant code from header.jsp is:

另一方面,header.jsp 中的相关代码是:

<%
    Enumeration enumeration = request.getHeaderNames();
    while (enumeration.hasMoreElements()) {
    String string = (String)enumeration.nextElement();
    out.println("<font size = 6>" +string +": " + request.getHeader(string)+ "</font><br>");
    }
    %>

That displays the following headers:

这将显示以下标题:

Host: localhost:9082
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; es-ES; rv:1.9.2.10) Gecko/20100914 Firefox/3.6.10 (.NET CLR 3.5.30729)
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: es-es,es;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Referer: http://localhost:9082/HdrTest/login.jsp
Cookie: JSESSIONID=0000tubMmZOXDyuM4X9RmaYYTg4:-1

As if the custom header was never inserted. How can I fix it ?

好像从未插入自定义标题一样。我该如何解决?

Thanks

谢谢

回答by BalusC

With a redirectyou're basically instructing the client (the webbrowser) to fire a brand new HTTP request. A brand new request also means a brand new response. Replace it by a forward:

通过重定向,您基本上是在指示客户端(网络浏览器)触发一个全新的 HTTP 请求。全新的请求也意味着全新的响应。将其替换为forward

request.getRequestDispatcher("header.jsp").forward(request, response);

Or if you actually want to have it on the redirected request, then create a Filterwhich is mapped on /header.jspand modifies the header accordingly.

或者,如果您确实希望在重定向的请求上使用它,则创建一个Filter映射/header.jsp并相应地修改标头的对象。

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
    ((HttpServletResponse) response).setHeader("foo", "bar");
    chain.doFilter(request, response);
}

Also note that you're displaying the requestheaders in the header.jspinstead of responseheaders. Since there's no direct API avaliable to display the response headers, you'd like to investigate them using an external HTTP header sniffing tool like Firebug(the Netpanel) or Fiddler.

另请注意,您在而不是响应标头中显示请求标头。由于没有可用于显示响应标头的直接 API,因此您希望使用外部 HTTP 标头嗅探工具(如Firebug网络面板)或Fiddler)来调查它们。header.jsp