写B/S系统的时候,获取并将每个客户的信息保存起来,对于以后的维护和优化可以更有目的性
可以知道使用系统的用户中,使用最多的是哪种版本的浏览器,然后可以有针对性的,将系统兼容性尽量提高!
以下是工具类(ClientInfo.java):
1 package com.ITPlus.tools;
2
3 import java.util.regex.Matcher;
4 import java.util.regex.Pattern;
5
6 /*
7 * 返回客户端信息工具类
8 * by netwild
9 */
10 public class ClientInfo {
11
12 private String info = "";
13 private String explorerVer = "未知";
14 private String OSVer = "未知";
15
16 /*
17 * 构造函数
18 * 参数:String request.getHeader("user-agent")
19 *
20 * IE7:Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C)
21 * IE8:Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C)
22 * Maxthon:Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; Maxthon 2.0)
23 * firefox:Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.11) Gecko/20101012 Firefox/3.6.11
24 * Chrome:Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.7 (KHTML, like Gecko) Chrome/7.0.517.44 Safari/534.7
25 * Opera:Opera/9.80 (Windows NT 6.1; U; zh-cn) Presto/2.6.30 Version/10.63
26 *
27 * 操作系统:
28 * Win7 : Windows NT 6.1
29 * WinXP : Windows NT 5.1
30 */
31 public ClientInfo(String info){
32 this.info = info;
33 }
34
35 /*
36 * 获取核心浏览器名称
37 */
38 public String getExplorerName(){
39 String str = "未知";
40 Pattern pattern = Pattern.compile("");
41 Matcher matcher;
42 if(info.indexOf("MSIE") != -1){
43 str = "MSIE"; //微软IE
44 pattern = Pattern.compile(str + "\\s([0-9.]+)");
45 }else if(info.indexOf("Firefox") != -1){
46 str = "Firefox"; //火狐
47 pattern = Pattern.compile(str + "\\/([0-9.]+)");
48 }else if(info.indexOf("Chrome") != -1){
49 str = "Chrome"; //Google
50 pattern = Pattern.compile(str + "\\/([0-9.]+)");
51 }else if(info.indexOf("Opera") != -1){
52 str = "Opera"; //Opera
53 pattern = Pattern.compile("Version\\/([0-9.]+)");
54 }
55 matcher = pattern.matcher(info);
56 if(matcher.find()) explorerVer = matcher.group(1);
57 return str;
58 }
59
60 /*
61 * 获取核心浏览器版本
62 */
63 public String getExplorerVer(){
64 return this.explorerVer;
65 }
66
67 /*
68 * 获取浏览器插件名称(例如:遨游、世界之窗等)
69 */
70 public String getExplorerPlug(){
71 String str = "无";
72 if(info.indexOf("Maxthon") != -1)
73 str = "Maxthon"; //遨游
74 return str;
75 }
76
77 /*
78 * 获取操作系统名称
79 */
80 public String getOSName(){
81 String str = "未知";
82 Pattern pattern = Pattern.compile("");
83 Matcher matcher;
84 if(info.indexOf("Windows") != -1){
85 str = "Windows"; //Windows NT 6.1
86 pattern = Pattern.compile(str + "\\s([a-zA-Z0-9]+\\s[0-9.]+)");
87 }
88 matcher = pattern.matcher(info);
89 if(matcher.find()) OSVer = matcher.group(1);
90 return str;
91 }
92
93 /*
94 * 获取操作系统版本
95 */
96 public String getOSVer(){
97 return this.OSVer;
98 }
99 }
2
3 import java.util.regex.Matcher;
4 import java.util.regex.Pattern;
5
6 /*
7 * 返回客户端信息工具类
8 * by netwild
9 */
10 public class ClientInfo {
11
12 private String info = "";
13 private String explorerVer = "未知";
14 private String OSVer = "未知";
15
16 /*
17 * 构造函数
18 * 参数:String request.getHeader("user-agent")
19 *
20 * IE7:Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C)
21 * IE8:Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C)
22 * Maxthon:Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; Maxthon 2.0)
23 * firefox:Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.11) Gecko/20101012 Firefox/3.6.11
24 * Chrome:Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.7 (KHTML, like Gecko) Chrome/7.0.517.44 Safari/534.7
25 * Opera:Opera/9.80 (Windows NT 6.1; U; zh-cn) Presto/2.6.30 Version/10.63
26 *
27 * 操作系统:
28 * Win7 : Windows NT 6.1
29 * WinXP : Windows NT 5.1
30 */
31 public ClientInfo(String info){
32 this.info = info;
33 }
34
35 /*
36 * 获取核心浏览器名称
37 */
38 public String getExplorerName(){
39 String str = "未知";
40 Pattern pattern = Pattern.compile("");
41 Matcher matcher;
42 if(info.indexOf("MSIE") != -1){
43 str = "MSIE"; //微软IE
44 pattern = Pattern.compile(str + "\\s([0-9.]+)");
45 }else if(info.indexOf("Firefox") != -1){
46 str = "Firefox"; //火狐
47 pattern = Pattern.compile(str + "\\/([0-9.]+)");
48 }else if(info.indexOf("Chrome") != -1){
49 str = "Chrome"; //Google
50 pattern = Pattern.compile(str + "\\/([0-9.]+)");
51 }else if(info.indexOf("Opera") != -1){
52 str = "Opera"; //Opera
53 pattern = Pattern.compile("Version\\/([0-9.]+)");
54 }
55 matcher = pattern.matcher(info);
56 if(matcher.find()) explorerVer = matcher.group(1);
57 return str;
58 }
59
60 /*
61 * 获取核心浏览器版本
62 */
63 public String getExplorerVer(){
64 return this.explorerVer;
65 }
66
67 /*
68 * 获取浏览器插件名称(例如:遨游、世界之窗等)
69 */
70 public String getExplorerPlug(){
71 String str = "无";
72 if(info.indexOf("Maxthon") != -1)
73 str = "Maxthon"; //遨游
74 return str;
75 }
76
77 /*
78 * 获取操作系统名称
79 */
80 public String getOSName(){
81 String str = "未知";
82 Pattern pattern = Pattern.compile("");
83 Matcher matcher;
84 if(info.indexOf("Windows") != -1){
85 str = "Windows"; //Windows NT 6.1
86 pattern = Pattern.compile(str + "\\s([a-zA-Z0-9]+\\s[0-9.]+)");
87 }
88 matcher = pattern.matcher(info);
89 if(matcher.find()) OSVer = matcher.group(1);
90 return str;
91 }
92
93 /*
94 * 获取操作系统版本
95 */
96 public String getOSVer(){
97 return this.OSVer;
98 }
99 }
调用方式:
1 //初始化ClientInfo对象,唯一的参数:request.getHeader("user-agent")
2 ClientInfo clientInfo = new ClientInfo(request.getHeader("user-agent"));
3 //获取核心浏览器名称并保存到登录信息对象(loginlog)的相应属性中
4 loginlog.setExplorerName(clientInfo.getExplorerName());
5 loginlog.setExplorerVer(clientInfo.getExplorerVer());
6 loginlog.setExplorerPlug(clientInfo.getExplorerPlug());
7 loginlog.setOsname(clientInfo.getOSName());
8 loginlog.setOsver(clientInfo.getOSVer());
2 ClientInfo clientInfo = new ClientInfo(request.getHeader("user-agent"));
3 //获取核心浏览器名称并保存到登录信息对象(loginlog)的相应属性中
4 loginlog.setExplorerName(clientInfo.getExplorerName());
5 loginlog.setExplorerVer(clientInfo.getExplorerVer());
6 loginlog.setExplorerPlug(clientInfo.getExplorerPlug());
7 loginlog.setOsname(clientInfo.getOSName());
8 loginlog.setOsver(clientInfo.getOSVer());
宠辱不惊,看庭前花开花落;去留无意,望天上云卷云舒