Java request.getHeader("Host") 返回什么值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20346783/
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
What value request.getHeader("Host") returns
提问by user1879683
The link to my application is https://xxxx.abcd.com
我的应用程序的链接是https://xxxx.abcd.com
Now when hit this URL in the browser what do I get the value in the string if i give
现在,当在浏览器中点击这个 URL 时,如果我给出字符串中的值,我会得到什么
String host=request.getHeader("Host");
采纳答案by Aniket Kulkarni
From RFC 2616-sec14
The Host request-header field specifies the Internet host and port number of the resource being requested, as obtained from the original URI given by the user or referring resource.
Host request-header 字段指定了被请求资源的 Internet 主机和端口号,从用户或引用资源给出的原始 URI 中获得。
request.getHeader("Host");
will return the value of the "Host" (in your case xxxxx.abcd.com) header in the request.
将返回请求中“主机”(在您的情况下为 xxxxx.abcd.com)标头的值。
You can use following program to get the all header information.
您可以使用以下程序来获取所有标题信息。
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class RequestHeaderServlet
*/
@WebServlet("/RequestHeaderServlet")
public class RequestHeaderServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public RequestHeaderServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
PrintWriter out = response.getWriter();
Enumeration en = request.getHeaderNames();
while(en.hasMoreElements()){
//get header name Accept,Accept-Charset,Authorization,Connection,Host etc.
String headerName = (String) en.nextElement(); //nextElement() returns Object need type cast
//get the value of the headerName
String headerValue = request.getHeader(headerName);
//display on browser
out.print("Header Name = "+ headerName + " " + " Header Value = "+ headerValue + "<br>");
}
out.close();
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}