我的应用程序的链接是https://xxxx.abcd.com
现在,当在浏览器中点击此URL时,如果我提供
,我将获得字符串中的值 String host=request.getHeader("Host");
答案 0 :(得分:3)
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。