今天在学习request的相关方法时遇到一个问题
String remoteAddr = request.getRemoteAddr();
System.out.println("remoteAddr ::: "+remoteAddr);
String remoteHost = request.getRemoteHost();
System.out.println("remoteHost ::: "+remoteHost);
控制台输出结果:
remoteAddr ::: 0:0:0:0:0:0:0:1
remoteHost ::: 0:0:0:0:0:0:0:1
为什么会显示IPV6的地址呢?而且还是0:0:0:0:0:0:0:1
经过研究后发现,通过getRemoteAddr()和getRemoteHost()时,客户机和服务器再同一台电脑导致的
我的请求路径为:
<a href="http://localhost:8080/user?username=Jhon&password=10">
将请求路径中的localhost替换为127.0.0.1
<a href="http://127.0.0.1:8080/user?username=Jhon&password=10">
再次请求观察获取到的IP地址
remoteAddr ::: 127.0.0.1
remoteHost ::: 127.0.0.1
显示称为了本机的IPV4地址!