我正在尝试使用以下代码在 jsp 页面中获取我的 IP 地址:
<%
out.println("Your IP address is " + request.getRemoteAddr());
%>
这会打印 0:0:0:0:0:0:0:1,而不是我的 IP 地址。这是应该的吗?为什么不显示我的IP地址?
谢谢
那是您的 IP 地址,但不是 IPv4 (xx.xx.xx.xx),而是 IPv6 (xx:xx:xx:xx:xx:xx:xx:xx)
编辑:如果您的系统正在将 IPv4 从/映射到 IPv6,您可以通过阅读本文来推断算法在它们之间进行更改
JSP代码:
<html>
<head><title>Hello World</title></head>
<body>
Hello World!<br/>
<%
out.println("Your Host IP address is " + request.getRemoteHost()+"</br>");
out.println("Your Addr address is " + request.getRemoteAddr()+"</br>");
out.println("Your Port Post address is " + request.getRemotePort()+"</br>");
%>
</body>
</html>
使用 http://127.0.0.1 : 8081/ hello.jsp得到结果:
Hello World!
Your Host IP address is 0:0:0:0:0:0:0:1
Your Addr address is 0:0:0:0:0:0:0:1
Your Port Post address is 32432
使用 http:// localhost :8081/hello.jsp 得到结果:
Hello World!
Your Host IP address is 0:0:0:0:0:0:0:1
Your Addr address is 0:0:0:0:0:0:0:1
Your Port Post address is 32432