使用Java从服务器检测浏览器

时间:2015-04-08 07:24:55

标签: java jsp java-ee

我使用以下代码缓存不同的浏览器,我无法缓存特定的代码。

有人可以给我一些建议吗?

if (user.contains("msie")) {
                    String substring = userAgent.substring(userAgent.indexOf("MSIE")).split(";")[0];
                    browser = substring.split(" ")[0].replace("MSIE", "IE") + "-"+ substring.split(" ")[1];
                } else if (user.contains("safari") && user.contains("version")) {
                    browser = (userAgent.substring(userAgent.indexOf("Safari")).split(" ")[0]).split("/")[0]+ "-"+ (userAgent.substring(userAgent.indexOf("Version")).split(" ")[0]).split("/")[1];
                } else if (user.contains("opr") || user.contains("opera")) {
                    if (user.contains("opera"))
                        browser = (userAgent.substring(userAgent.indexOf("Opera")).split(" ")[0]).split("/")[0]+ "-"+ (userAgent.substring(userAgent.indexOf("Version")).split(" ")[0]).split("/")[1];
                    else if (user.contains("opr"))
                        browser = ((userAgent.substring(userAgent.indexOf("OPR")).split(" ")[0]).replace("/", "-")).replace("OPR","Opera");
                } else if (user.contains("chrome")) {
                    browser = (userAgent.substring(userAgent.indexOf("Chrome")).split(" ")[0]).replace("/", "-");
                } else if ((user.indexOf("mozilla/36.0") > -1)
                        || (user.indexOf("netscape6") != -1)
                        || (user.indexOf("mozilla/35.0") != -1)
                        || (user.indexOf("mozilla/34.0") != -1)
                        || (user.indexOf("mozilla/33.1") != -1)
                        || (user.indexOf("mozilla/33.0") != -1)
                        || (user.indexOf("mozilla/32.0") != -1)
                        || (user.indexOf("mozilla/31.0") != -1)) {

                    browser = "Netscape-?";

                } else if (user.contains("firefox")) {
                    browser = (userAgent.substring(userAgent.indexOf("Firefox")).split(" ")[0]).replace("/", "-");
                } else if (user.contains("rv")) {
                    browser = "IE";
                } else {
                    browser = "UnKnown, More-Info: " + userAgent;
                }
            } catch (Exception e) {
                browser = "Exception: " + userAgent;
            }

1 个答案:

答案 0 :(得分:0)

为什么需要服务器端代码,您可以在javascript中轻松完成。

function detect() {
                var browser;
                var version;                
                var platform = navigator.platform;

                if (platform == "iPad" || platform == "iPhone" || platform == "iPod" || platform == "iPod touch") { // For iPhones iPads and iPods

                    var userAgent = navigator.userAgent.toUpperCase();

                    var chromeIndex = userAgent.indexOf("CRIOS");
                    var opera = userAgent.indexOf("OPIOS");
                    var ucIndex = userAgent.indexOf("UCBROWSER");
                    var coast = userAgent.indexOf("COAST");
                    var mercury = userAgent.indexOf("MERCURY");

                    var safari = userAgent.indexOf("SAFARI");
                    var webkit = userAgent.indexOf("APPLEWEBKIT");

                    // Chrome detection
                    if(chromeIndex > 0) {
                        browser = "chrome";

                        var arr = userAgent.split("CRIOS/");
                        arr = arr[1].split(".");
                        version = arr[0];
                    }
                    // Safari detection
                    else if(opera < 0 && ucIndex < 0 && coast < 0 && mercury < 0 && safari > 0 && webkit > 0) {
                        browser = "safari";

                        var arr = userAgent.split("APPLEWEBKIT/");
                        arr = arr[1].split(".");
                        version = arr[0];
                    } else {
                        browser = "unknown";
                        version = -1;
                    }

                } else if(platform == "Win32" || platform == "MacIntel") { // For windows and mac

                    // Browser detection
                    var isOpera = !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0;
                    // Opera 8.0+ (UA detection to detect Blink/v8-powered Opera)
                    var isFirefox = typeof InstallTrigger !== 'undefined'; // Firefox 1.0+
                    var isSafari = Object.prototype.toString.call(window.HTMLElement).indexOf('Constructor') > 0;
                    // At least Safari 3+: "[object HTMLElementConstructor]"
                    var isChrome = !!window.chrome && !isOpera; // Chrome 1+
                    var isIE = /*@cc_on!@*/false || !!document.documentMode; // At least IE6


                    if (isOpera) {
                        browser = "opera";
                    } else if (isFirefox) {
                        browser = "firefox";
                    } else if (isChrome) {
                        browser = "chrome";
                    } else if (isIE) {
                        browser = "ie";
                    } else if (isSafari) {
                        browser = "safari";
                    } else {
                        browser = "unknown";
                    }

                    if (browser == "firefox") {
                        var arr = navigator.userAgent.split("Firefox/");
                        arr = arr[1].split(".");
                        version = arr[0];
                    } else if (browser == "chrome") {
                        var arr = navigator.userAgent.split("Chrome/");
                        arr = arr[1].split(".");
                        version = arr[0];
                    } else if (browser == "ie") {
                        var v1 = navigator.userAgent.indexOf("MISE");
                        var v2 = navigator.userAgent.indexOf("rv:");

                        if(v1 > 0) {
                            var arr = navigator.userAgent.split("MSIE ");
                            arr = arr[1].split(".");
                            version = arr[0];
                        } else if(v2 > 0){
                            var arr = navigator.userAgent.split("rv:");
                            arr = arr[1].split(".");
                            version = arr[0];
                        } else {
                            version = 11;
                        }
                    } else if (browser == "safari") {
                        var arr = navigator.userAgent.split("AppleWebKit/");
                        arr = arr[1].split(".");
                        version = arr[0];
                    } else if (browser == "opera") {
                        var arr = navigator.userAgent.split("OPR/");
                        arr = arr[1].split(".");
                        version = arr[0];
                    } else if (browser == "unknown") {
                        version = -1;
                    }

                } else if(platform == "Android" || platform == "Linux armv7l") { // For android
                    var userAgent = navigator.userAgent;

                    var chromeIndex = userAgent.indexOf("Chrome");
                    var opera = userAgent.indexOf("OPR");
                    var ucIndex = userAgent.indexOf("UCBrowser");
                    var coast = userAgent.indexOf("Coast");
                    var firefox = userAgent.indexOf("Firefox");

                    // Chrome detection
                    if(chromeIndex > 0 && opera < 0 && ucIndex < 0 && coast < 0) {
                        browser = "chrome";

                        var arr = userAgent.split("Chrome/");
                        arr = arr[1].split(".");
                        version = arr[0];

                    } else if(firefox > 0) { // Firefox detection
                        browser = "firefox";

                        var arr = userAgent.split("Firefox/");
                        arr = arr[1].split(".");
                        version = arr[0];

                    } else if(opera > 0) { // Opera detection
                        browser = "opera";

                        var arr = userAgent.split("OPR/");
                        arr = arr[1].split(".");
                        version = arr[0];

                    } else { // Unknown browser
                        browser = "unknown";
                        version = -1;
                    }

                } else { // For other platforms
                    version = -1;
                    browser = "other_platform";
                }

我在我的应用中使用过这个脚本,你会发现这很有帮助。