Java 如何在 HTTP 响应中设置标头?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19552543/
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
How to set a header in an HTTP response?
提问by Pakira
I've a servlet Awhere I'm setting a header in the HTTP response:
我有一个 servlet A,我在 HTTP 响应中设置了一个标头:
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String userName=request.getParameter("userName");
String newUrl = "http://somehost:port/ServletB";
response.addHeader("REMOTE_USER", userName);
response.sendRedirect(newUrl);
}
Now in a servlet B, I'm trying to get the header value that was set in the servlet A:
现在在 servlet B 中,我试图获取在 servlet A 中设置的标头值:
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String userid = request.getHeader("REMOTE_USER");
}
But here the value of useridis coming as null
. Please let me know what I'm missing here.
但是这里userid的值是null
. 请让我知道我在这里缺少什么。
采纳答案by pubsy
First of all you have to understand the nature of
首先你要了解它的本质
response.sendRedirect(newUrl);
It is giving the client (browser) 302 http code response with an URL. The browser then makes a separate GET request on that URL. And that request has no knowledge of headers in the first one.
它向客户端(浏览器)提供带有 URL 的 302 http 代码响应。然后浏览器对该 URL 发出单独的 GET 请求。并且该请求不知道第一个中的标头。
So sendRedirect won't work if you need to pass a header from Servlet A to Servlet B.
因此,如果您需要将标头从 Servlet A 传递到 Servlet B,则 sendRedirect 将不起作用。
If you want this code to work - use RequestDispatcher in Servlet A (instead of sendRedirect). Also, it is always better to use relative path.
如果您希望此代码工作 - 在 Servlet A 中使用 RequestDispatcher(而不是 sendRedirect)。此外,最好使用相对路径。
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
String userName=request.getParameter("userName");
String newUrl = "ServletB";
response.addHeader("REMOTE_USER", userName);
RequestDispatcher view = request.getRequestDispatcher(newUrl);
view.forward(request, response);
}
========================
========================
public void doPost(HttpServletRequest request, HttpServletResponse response)
{
String sss = response.getHeader("REMOTE_USER");
}
回答by mgorniew
Header fields are not copied to subsequent requests. You should use either cookie for this (addCookie method) or store "REMOTE_USER" in session (which you can obtain with getSession method).
标头字段不会复制到后续请求中。您应该为此使用 cookie(addCookie 方法)或在会话中存储“REMOTE_USER”(您可以使用 getSession 方法获取)。
回答by hd1
In my Controller, I merely added an HttpServletResponse parameter and manually added the headers, no filter or intercept required and it works fine:
在我的控制器中,我只添加了一个 HttpServletResponse 参数并手动添加了标头,不需要过滤器或拦截,它工作正常:
httpServletResponse.setHeader("Access-Control-Allow-Origin", "*");
httpServletResponse.setHeader("Access-Control-Allow-Methods", "GET, OPTIONS");
httpServletResponse.setHeader("Access-Control-Allow-Headers","Origin, X-Requested-With, Content-Type, Accept, X-Auth-Token, X-Csrf-Token, WWW-Authenticate, Authorization");
httpServletResponse.setHeader("Access-Control-Allow-Credentials", "false");
httpServletResponse.setHeader("Access-Control-Max-Age", "3600");