简体   繁体   中英

Does request.getRemoteHost() return the IP of browser host?

<HTML>
<BODY>
<%
    // This scriptlet declares and initializes "date"
    System.out.println( "Evaluating date now" );
    java.util.Date date = new java.util.Date();
%>
Hello!  The time is now
<%
    out.println( date );
    out.println( "<BR>Your machine's address is " );
    out.println( request.getRemoteHost());
%>
</BODY>
</HTML>

In the JSP code above, I thought request.getRemoteHost() would return the ip address of the browser host. However, it returned something like: 0:0:0:0:0:0:0:1 . What's the problem?

getRemoteHost() Returns the fully qualified name of the client that sent the request.

getRemoteAddr() Returns the Internet Protocol (IP) address of the client that sent the request.

request.getRemoteAddr() returns the IP address of the comp whereas request.getRemoteHost() tries to retireve the host name. If host name is empty , then it tries to retrieve the IP address, just like how getRemoteAddr() works.

Ref:

它确实是运行浏览器的计算机的IP,因此它将返回127.0.0.1 ,或者在您的情况下返回::1 ,这是IPv6中的localhost。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM