To determine the browser type tool class

Source: Internet
Author: User
Package com.test;
Import Java.util.HashMap;
Import Java.util.Map;
Import Java.util.regex.Matcher;

Import Java.util.regex.Pattern;

Import Javax.servlet.http.HttpServletRequest; /**
 * 
 *@authorWeitao * */public class Browserutils {//judge whether IE public static Boolean Isie (HttpServletRequest request) { Return (Request.getheader ("User-agent"). toLowerCase () indexOf ("MSIE") > 0 | | request GetHeader ("user- AGENT "). toLowerCase (). IndexOf (" rv:11.0 ") > 0)?
   True:false; /** * Get IE version * *@paramRequest *@return */public static Double Getieversion (HttpServletRequest request) {double version = 0.0;
      if (Getbrowsertype (request, IE11)) {version = 11.0;
      else if (Getbrowsertype (request, IE10)) {version = 10.0;
      else if (Getbrowsertype (request, IE9)) {version = 9.0;
      else if (Getbrowsertype (request, IE8)) {version = 8.0;
      else if (Getbrowsertype (request, IE7)) {version = 7.0;
      else if (Getbrowsertype (request, IE6)) {Version = 6.0;
   return version; /** * Get Browser type * *@paramRequest *@return * * public static BrowserType Getbrowsertype (HttpServletRequest request) {BrowserType browsertype = null;
      if (Getbrowsertype (request, IE11)) {browsertype = Browsertype.ie11;
      } if (Getbrowsertype (request, IE10)) {browsertype = Browsertype.ie10;
      } if (Getbrowsertype (request, IE9)) {browsertype = BROWSERTYPE.IE9;
      } if (Getbrowsertype (request, IE8)) {browsertype = BROWSERTYPE.IE8;
      } if (Getbrowsertype (request, IE7)) {browsertype = Browsertype.ie7;
      } if (Getbrowsertype (request, IE6)) {browsertype = Browsertype.ie6;
      } if (Getbrowsertype (request, FIREFOX)) {browsertype = Browsertype.firefox;
      } if (Getbrowsertype (request, SAFARI)) {browsertype = Browsertype.safari;
      } if (Getbrowsertype (request, CHROME)) {browsertype = Browsertype.chrome;
       if (Getbrowsertype (request, OPERA)) {  BrowserType = Browsertype.opera;
      } if (Getbrowsertype (Request, "Camino")) {browsertype = Browsertype.camino;
   return browsertype; private static Boolean Getbrowsertype (HttpServletRequest request, String Brosertype) {return request . GetHeader ("User-agent"). toLowerCase (). IndexOf (Brosertype) > 0?
   True:false;
   Private final static String IE11 = "rv:11.0";
   Private final static String IE10 = "MSIE 10.0";
   Private final static String IE9 = "MSIE 9.0";
   Private final static String IE8 = "MSIE 8.0";
   Private final static String IE7 = "MSIE 7.0";
   Private final static String IE6 = "MSIE 6.0";
   Private final static String MAXTHON = "MAXTHON";
   Private final static String QQ = "Qqbrowser";
   Private final static String GREEN = "Greenbrowser";
   Private final static String SE360 = "360SE";
   Private final static String Firefox = "Firefox";
   Private final static String opera = "Opera"; PrivAte final static String chrome = "Chrome";
   Private final static String Safari = "Safari";

   Private final static String other = "others";
      public static String Checkbrowse (HttpServletRequest request) {String useragent = Request.getheader ("user-agent");
      if (Regex (OPERA, useragent)) return to opera;
      if (Regex (CHROME, useragent)) return chrome;
      if (Regex (FIREFOX, useragent)) return to Firefox;
      if (Regex (SAFARI, useragent)) return SAFARI;
      if (Regex (SE360, useragent)) return SE360;
      if (Regex (GREEN, useragent)) return GREEN;
      if (Regex (QQ, useragent)) return QQ;
      if (Regex (MAXTHON, useragent)) return MAXTHON;
      if (Regex (IE11, useragent)) return IE11;
      if (Regex (IE10, useragent)) return IE10;
      if (Regex (IE9, useragent)) return IE9;
      if (Regex (IE8, useragent)) return IE8; if (Regex (IE7, useragent)) return IE7;
      if (Regex (IE6, useragent)) return IE6;
   return to other; 
      The public static Boolean regex (string regex, string str) {pattern p = pattern.compile (regex, pattern.multiline);
      Matcher m = p.matcher (str);
   return M.find ();
   private static map<string, string> Langmap = new hashmap<string, string> ();
   Private final static String en = "en";
   
   Private final static String ZH_CN = "ZH-CN";
   Private final static String en = "en";
   
   
   Private final static String en_US = "EN";
      static {Langmap.put (EN, ZH_CN);
   Langmap.put (EN, en_US); public static string Getbrowserlanguage (HttpServletRequest request) {String Browserlang = request.g
      Etlocale (). GetLanguage ();
      
      String browserlangcode = (string) langmap.get (Browserlang);
      if (Browserlangcode = = null) {Browserlangcode = en_US;
   return browserlangcode; }

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.