Custom header not inserted in request in servlet

前端 未结 1 997
北海茫月
北海茫月 2020-12-11 08:19

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 l

相关标签:
1条回答
  • 2020-12-11 08:45

    With a redirect you'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:

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

    Or if you actually want to have it on the redirected request, then create a Filter which is mapped on /header.jsp and modifies the header accordingly.

    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 request headers in the header.jsp instead of response headers. 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 Net panel) or Fiddler.

    0 讨论(0)
提交回复
热议问题