javascript 在 Web 应用程序中获取客户端计算机名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33960771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get client computer name in web application
提问by Level15
I have a Java web application that needs to know the computer name of clients connecting to it. My first idea was to get it via JavaScript and fill a hidden form field with it, but after some digging, it appears JS cannot access that information.
我有一个 Java Web 应用程序,它需要知道连接到它的客户端的计算机名称。我的第一个想法是通过 JavaScript 获取它并用它填充一个隐藏的表单字段,但经过一番挖掘后,JS 似乎无法访问该信息。
Then I tried using an applet and accessing the applet methods via JavaScript. This seems to work on Firefox, but newer Chrome versions don't run applets.
然后我尝试使用小程序并通过 JavaScript 访问小程序方法。这似乎适用于 Firefox,但较新的 Chrome 版本不运行小程序。
Then I considered switching to a Java Webstart application, which, as far as I know, should work under Chrome, but in this scenario, since the Webstart application runs outside the browser,JavaScript cannot access its methods.
然后我考虑切换到 Java Webstart 应用程序,据我所知,它应该可以在 Chrome 下运行,但在这种情况下,由于 Webstart 应用程序在浏览器之外运行,JavaScript 无法访问其方法。
Then I tried saving the hostname in the environment TEMP directory, which works, in Firefox + Linux + Java7, but not in Firefox + Windows + Java8: the applet just doesn't run, and, in addition, I haven't found a way to access the defined TEMP directory and read the file in JavaScript.
然后我尝试将主机名保存在环境 TEMP 目录中,该目录在 Firefox + Linux + Java7 中有效,但在 Firefox + Windows + Java8 中无效:小程序无法运行,此外,我还没有找到访问定义的 TEMP 目录并在 JavaScript 中读取文件的方法。
At this point I'm out of ideas and would love to have some input from you guys. Any hints on how to achieve this? Did I miss any obvious solution?
在这一点上,我没有想法,很想从你们那里得到一些意见。关于如何实现这一目标的任何提示?我错过了任何明显的解决方案吗?
Please notice I need the computer defined hostname, not what the computer's IP resolves to via DNS.
请注意,我需要计算机定义的主机名,而不是计算机的 IP 通过 DNS 解析的内容。
Thanks.
谢谢。
采纳答案by Dominik Pfefferle
Your Javawebstartet Application could Host Websocket listener. So you could access this application via Websocket from javascript. Works only with http, not https
您的 Javawebstartet 应用程序可以托管 Websocket 侦听器。所以你可以从javascript通过Websocket访问这个应用程序。仅适用于 http,不适用于 https
On JavaSide use the websocket implementation of http://java-websocket.org/
在 JavaSide 上使用http://java-websocket.org/的 websocket 实现
In Javascript I use https://code.google.com/p/jquery-websocket/You can find examples there too. The websocket communication is async. Create the WS with the callback method for the response
在 Javascript 中,我使用https://code.google.com/p/jquery-websocket/您也可以在那里找到示例。websocket 通信是异步的。使用响应的回调方法创建 WS
var ws = $.websocket("ws://127.0.0.1:" + lport + "/", {
events: {
say: function (e) {
//showMsg(e.data);
}
}
});
and call the server with
并调用服务器
ws.send(0, jsonData)
回答by Sakthi
You cannot get computer name using javascript. Javascript does not have access to computer name as a security reason
您无法使用 javascript 获取计算机名称。出于安全原因,Javascript 无权访问计算机名称
回答by Prim
You can use InetAddress.getLocalHost().getHostName()
which doesn't resolve hostname from DNS according to the documentation
您可以InetAddress.getLocalHost().getHostName()
根据文档使用which 不从 DNS 解析主机名
回答by ΦXoc? ? Пepeúpa ツ
if you have the ip add the you can do:
如果您有 ip add 您可以执行以下操作:
InetAddress inetAddress =InetAddress.getByName("127.64.84.2");//get the host Inet using ip
System.out.println ("Host Name: "+ inetAddress.getHostName());//display the host
回答by Sheetal Mohan Sharma
Tried servlet?
试过servlet?
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
final String hostname = request.getRemoteHost(); // hostname
System.out.println("hostname"+hostname);
String remoteMachineName = null;
String remoteMachineAddress = request.getRemoteAddr();
System.out.println("remoteMachineAddress: " + remoteMachineAddress);
try {
InetAddress inetAddress = InetAddress.getByName(remoteMachineAddress);
remoteMachineName = inetAddress.getHostName();
System.out.println("remoteMachineName: " + remoteMachineName);
}
} catch (UnknownHostException e) {
}
System.out.println("remoteMachineName: " + remoteMachineName);
}