Java 使用HttpServletRequest获取请求发件人的URL

Java 使用HttpServletRequest获取请求发件人的URL,java,servlets,dns,Java,Servlets,Dns,如何使用HttpServletRequest获取源域? 源域是请求者的域 InetAddress ip = InetAddress.getLocalHost(); String hostname = ip.getHostName(); out.print("Your current IP address : " + ip+"\n"); out.print("Your current Hostname : " + hostname); 谢谢。你也可以 // gets client (browse

如何使用HttpServletRequest获取源域? 源域是请求者的域

InetAddress ip = InetAddress.getLocalHost();
String hostname = ip.getHostName();
out.print("Your current IP address : " + ip+"\n");
out.print("Your current Hostname : " + hostname);
谢谢。

你也可以

// gets client (browser)'s hostname
String host = request.getRemoteHost(); 
InetAddress ip = InetAddress.getLocalHost();
String hostname = ip.getHostName();
out.print("Your current IP address : " + ip+"\n");
out.print("Your current Hostname : " + hostname);

InetAddress ip = InetAddress.getLocalHost();
String hostname = ip.getHostName();
out.print("Your current IP address : " + ip+"\n");
out.print("Your current Hostname : " + hostname);

主机名请求

InetAddress ip = InetAddress.getLocalHost();
String hostname = ip.getHostName();
out.print("Your current IP address : " + ip+"\n");
out.print("Your current Hostname : " + hostname);

要获取源域,可以使用
request.getHeader(“origin”)

特别是当请求必须通过代理服务器时。

getRemoteHost()返回客户端(或代理)的主机。@BalusC:是的,我现在意识到了。更新了答案。:)只有当Java直接从浏览器接收到请求时,客户端主机名才起作用。如果Java支持F5big-IP之类的东西,那么应该使用request.getHeader(“X-FORWARDED-FOR”)或随客户机主机名传入的任何请求头来获取实际的客户机主机名。getRemoteHost()将只提供负载平衡器的IP或域名,因为这是向Java发送请求的原因。
InetAddress ip = InetAddress.getLocalHost();
String hostname = ip.getHostName();
out.print("Your current IP address : " + ip+"\n");
out.print("Your current Hostname : " + hostname);