java 如何在 JSP 中显示操作系统信息?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4117536/
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
How to display Operating System information in JSP?
提问by Tyler Vernon
i want my jsp page to display the operating system information to the user.
我希望我的 jsp 页面向用户显示操作系统信息。
回答by vishnu
Server side OS information
JSP which is java can find the OS of the computer where it is executing. we can use the getProperty() method to find the System properties of the system. It returns a String with the name of OS. It supports various OSs in java.
服务器端操作系统信息
JSP 是java 可以找到它正在执行的计算机的操作系统。我们可以使用 getProperty() 方法来查找系统的系统属性。它返回一个带有操作系统名称的字符串。它支持java中的各种操作系统。
Example:
例子:
<%@ page language="java"%>
<html>
<head>
<title>Example for Printing the OS name</title>
</head>
<body>
<%
out.println("OS: " + System.getProperty("os.name"));
%>
</body>
</html>
Client side information
客户端信息
String agent = request.getHeader ("user-agent");
StringTokenizer st = new StringTokenizer (agent ,";");
st.nextToken ();
// Get the user's browser name
String userBrowser = st.nextToken ();
// Get the user's operating system name
String userOs = st.nextToken ();
回答by BalusC
Your best bet is user agent sniffing. It's available by request.getHeader("user-agent")
. There's even a 3rd party webservice to which you can submit this string and obtain detailed information: http://user-agent-string.info
最好的选择是用户代理嗅探。可通过request.getHeader("user-agent")
. 甚至还有一个 3rd 方网络服务,您可以向其提交此字符串并获取详细信息:http: //user-agent-string.info
Another way is using a Java applet and gather the information by System.getProperty("os.name")
. This is a bit more reliable since the user agent can be spoofed by the client.
另一种方法是使用 Java 小程序并通过System.getProperty("os.name")
. 这更可靠一点,因为用户代理可以被客户端欺骗。
回答by Xingda Wang
You can use script to get the PC info, such as:
您可以使用脚本来获取 PC 信息,例如:
<script>
document.write(navigator.userAgent + "<br />");
</script>