如何在Java小程序中检测浏览器和操作系统

发布于 2024-10-01 05:39:18 字数 432 浏览 3 评论 0原文

我正在开发一个复杂的 Java 小程序,它在 Safari 以及 Windows 和 Linux 上的各种浏览器中运行良好,但在 Macintosh 的 Chrome 和 Firefox 中出现问题。对于调试来说,如果 Java 代码能够检测并报告浏览器、浏览器版本、操作系统以及出现错误时的操作系统版本,那将非常有帮助。

Java 检测该信息最简单、最可靠的方法是什么?

这看起来不错,但需要 Apache log4j,我不知道是否可以说服团队运行它:http://code.google.com/p/user-agent-utils/'

我猜Java可以通过JavaScript获取用户代理字符串,但我找不到任何好的例子代码清楚地展示了如何做到这一点。

I'm working on a complex Java applet which runs well in Safari and in various browsers on Windows and Linux but has problems on Macintosh in Chrome and Firefox. For debugging, it would be really helpful if Java code could detect and report the browser, the browser version, the OS, and the OS version when there are errors.

What is the easiest and most reliable way for Java to detect that info?

This looks good, but requires Apache log4j, and I don't know if I can convince the team to run that: http://code.google.com/p/user-agent-utils/'

I guess Java could get the user-agent string via JavaScript, but I can't find any good example code that clearly shows how to do that.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅浅 2024-10-08 05:39:18

您可以通过 System#getProperty()如下:

String osName = System.getProperty("os.name");
String osVersion = System.getProperty("os.version");
String osArch = System.getProperty("os.arch");
String javaRuntimeVersion = System.getProperty("java.runtime.version");

其他请查看System#getProperties()

用户代理字符串并不那么相关。毕竟是相同的 JVM 和操作系统。无论如何,您可以让服务器端编程语言从当前请求标头中获取它,并将其作为 参数传递给 applet。下面是一个使用 JSP/EL 的示例:

<param name="userAgent" value="${header['user-agent']}" />

并在 applet 中获取它,如下所示:

String userAgent = getParameter("userAgent");

一旦掌握了用户代理字符串,您就可以在 http://user-agent-string.info 比 Google 代码中的用户代理实用程序更有用。

You can get the necessary OS information by System#getProperty() as follows:

String osName = System.getProperty("os.name");
String osVersion = System.getProperty("os.version");
String osArch = System.getProperty("os.arch");
String javaRuntimeVersion = System.getProperty("java.runtime.version");

For others, check the javadoc of System#getProperties().

The user agent string is not so relevant. It's after all the same JVM and OS. Regardless, you could let the server side programming language get it from the current request headers and pass it as an <object> or <applet> parameter to the applet. Here's an example using JSP/EL:

<param name="userAgent" value="${header['user-agent']}" />

and obtain it in the applet as follows:

String userAgent = getParameter("userAgent");

Once having an user agent string at hands, you may find the webservice at http://user-agent-string.info more useful than the user agent utils at Google Code.

故事还在继续 2024-10-08 05:39:18

如果您使用 JavaScript,则使用以下代码来检测客户端的浏览器和操作系统。

<p id="demo5"></p>

<script>
document.getElementById("demo5").innerHTML = "<span>your Browser is: <br> </span> " + navigator.userAgent;
</script>

<p id="demo66"></p>

<script>
document.getElementById("demo66").innerHTML = "<span>your oscpu (Operating System | CPU) is: </span> " + navigator.oscpu;
</script>

In case you use JavaScript, then use the below code to detect Browser and OS of clients.

<p id="demo5"></p>

<script>
document.getElementById("demo5").innerHTML = "<span>your Browser is: <br> </span> " + navigator.userAgent;
</script>

<p id="demo66"></p>

<script>
document.getElementById("demo66").innerHTML = "<span>your oscpu (Operating System | CPU) is: </span> " + navigator.oscpu;
</script>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文