天天看點

使用Java實作B/S系統常見的擷取用戶端資訊功能!

寫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 }   

調用方式:

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

寵辱不驚,看庭前花開花落;去留無意,望天上雲卷雲舒