如何在Java小程序中检测浏览器和操作系统 [英] how to detect browser and OS in Java applet

查看:22
本文介绍了如何在Java小程序中检测浏览器和操作系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

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

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/'

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

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.

推荐答案

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

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");

对于其他人,请查看 System#getProperties().

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

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

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");

一旦有了用户代理字符串,您就可以在 http://user-agent-string 找到网络服务.info 比 Google Code 中的用户代理实用程序更有用.

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.

这篇关于如何在Java小程序中检测浏览器和操作系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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