我想用javascript获取我的机器的IP地址,这在我的html页面中可以进一步引用。
我已经引用了所有建议的链接,但没有得到任何答案。
我不想使用任何链接来获取IP,所以我尝试在JavaScript中使用以下代码行
var ip = '<%=request.getRemoteAddr();%>';
要么
var ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
var ip = Request.UserHostAddress.ToString();
但是没有得到结果。
请帮助我获得解决方案。我想将此javascript包含在我的html页面中,并且我不想使用任何链接来获取IP。
All the links I have gone through gives the external links to get the IP address and I do not want to use any external link to get the IP.
解决方案如下:
我认为javascript标准库中没有主机或IP地址的概念。因此,您必须访问一些外部服务才能为您查找主机名。
除非您可能想向服务器发送请求,该请求将向您返回主机IP地址!
编辑
在JSP中,您可以使用getRemoteHost()中的HttpServletRequest方法
获取用户的IP地址。
所以你可以写这样的东西-
var ip = '<%=request.getRemoteHost();%>';
^^上一行是JSP代码,它应该是您从Java servlet容器(例如tomcat)返回的JSP文件的一部分。这在静态HTML页面中不起作用。