问题
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.
回答1:
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.
来源:https://stackoverflow.com/questions/4120155/how-to-detect-browser-and-os-in-java-applet