PackageCom.java.basic.pattern;ImportJava.util.regex.Matcher;ImportJava.util.regex.Pattern;/*** Get client operating System Information * Get to User-agent from the header of HTTP, then use regular expressions to determine which operating system * * Temporarily matches only win 7, WinXP, Win2003, Win2000, MAC, WinNT, Linux, mac68k, Win9x * *@paramuseragent request.getheader ("User-agent") return value * *@author * */ Public classClientos {Private Static FinalString user_agent = ""; Public Static voidMain (string[] args) {String Clientos=Getclientos (user_agent); } Public Staticstring Getclientos (String useragent) {string cos= "Unknow os"; Pattern P= Pattern.compile (". * (Windows NT 6\\.1). *"); Matcher m=P.matcher (useragent); if(M.find ()) {cos= "Windows7"; returnCos; } P= Pattern.compile (". * (Windows NT 5\\.1| Windows XP). * "); M=P.matcher (useragent); if(M.find ()) {cos= "WinXP"; returnCos; } P= Pattern.compile (". * (Windows NT 5\\.2). *"); M=P.matcher (useragent); if(M.find ()) {cos= "Win2003"; returnCos; } P= Pattern.compile (". * (win2000| Windows 2000| Windows NT 5\\.0). * "); M=P.matcher (useragent); if(M.find ()) {cos= "Win2000"; returnCos; } P= Pattern.compile (". * (mac|apple| MACOS8). * "); M=P.matcher (useragent); if(M.find ()) {cos= "MAC"; returnCos; } P= Pattern.compile (". * (winnt| WINDOWSNT). * "); M=P.matcher (useragent); if(M.find ()) {cos= "WinNT"; returnCos; } P= Pattern.compile (". *linux.*"); if(M.find ()) {cos= "Linux"; returnCos; } P= Pattern.compile (". *68k|68000.*"); M=P.matcher (useragent); if(M.find ()) {cos= "mac68k"; returnCos; } P= Pattern.compile (". * (9x 4.90| Win9 (5|8) | Windows 9 (5|8) |95/nt| win32|32bit). * "); M=P.matcher (useragent); if(M.find ()) {cos= "Win9x"; returnCos; } returnCos; } }View Code
Java Web get client operating system Information