查找用户IP地址 [英] Finding user ip address

查看:24
本文介绍了查找用户IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 JSF 2.0 创建了 Web 应用程序.我在托管站点上托管了它,托管站点的服务器位于美国.

I have created web application using JSF 2.0. I have hosted it on hosting site and the server of the hosting site is based in US.

我的客户想要所有访问该站点的用户的详细信息.如何在 JSF 中找到用户的 IP 地址?

My client want the details of the user who all accessed the site. How can I find the IP address of user in JSF?

我试过

    try {
        InetAddress thisIp = InetAddress.getLocalHost();
        System.out.println("My IP is  " + thisIp.getLocalHost().getHostAddress());
    } catch (Exception e) {
        System.out.println("exception in up addresss");
    }

然而,这仅给了我我网站的 IP 地址,即服务器 IP 地址.

however this gives me ip address of my site only i.e. server ip address.

谁能告诉我如何获取使用Java访问网站的IP地址?

Could someone tell me how to get IP address who accessed the website using Java?

推荐答案

我继续了

HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
String ipAddress = request.getHeader("X-FORWARDED-FOR");
if (ipAddress == null) {
    ipAddress = request.getRemoteAddr();
}
System.out.println("ipAddress:" + ipAddress);

这篇关于查找用户IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆