如何在Java小程序中检测浏览器和操作系统
我正在开发一个复杂的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过
System#getProperty()
如下:其他请查看
System#getProperties()
。用户代理字符串并不那么相关。毕竟是相同的 JVM 和操作系统。无论如何,您可以让服务器端编程语言从当前请求标头中获取它,并将其作为
并在 applet 中获取它,如下所示:
一旦掌握了用户代理字符串,您就可以在 http://user-agent-string.info 比 Google 代码中的用户代理实用程序更有用。
You can get the necessary OS information by
System#getProperty()
as follows: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:and obtain it in the applet as follows:
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.