request.getHeader(“Host”)返回什么值

时间:2013-12-03 09:02:42

标签: java jsp

我的应用程序的链接是https://xxxx.abcd.com

现在,当在浏览器中点击此URL时,如果我提供

,我将获得字符串中的值
   String host=request.getHeader("Host");

2 个答案:

答案 0 :(得分:3)

来自RFC 2616-sec14

  

Host request-header字段指定所请求资源的Internet主机和端口号,这是从用户或引用资源给出的原始URI中获取的。

request.getHeader("Host");

将在请求中返回“Host”(在您的情况下为xxxxx.abcd.com)标头的值。

您可以使用以下程序获取所有标头信息。

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
 }
}

相关链接

答案 1 :(得分:0)

它将返回请求中“Host”标头的值,该标头必须是“从用户或引用资源给出的原始URI中获取”。请参阅RFC 2616