jsp判斷請求來自手機範例程式碼_JSP編程

來源:互聯網
上載者:User
複製代碼 代碼如下:

<%
String wap = "http://3g.ssss.com.cn";

String agent = request.getHeader("user-agent");
//String agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/534.51.22 (KHTML, like Gecko) Version/5.1.1 Safari/534.51.22";
String agentcheck = agent.trim().toLowerCase();
boolean isWAP = false;
String[] keywords = {"mobile", "android",
"symbianos", "iphone", "wp\\d*", "windows phone",
"mqqbrowser", "nokia", "samsung", "midp-2",
"untrusted/1.0", "windows ce", "blackberry","ucweb",
"brew", "j2me", "yulong", "coolpad", "tianyu", "ty-",
"k-touch", "haier", "dopod", "lenovo", "huaqin", "aigo-",
"ctc/1.0", "ctc/2.0", "cmcc", "daxian", "mot-",
"sonyericsson", "gionee", "htc", "zte", "huawei", "webos",
"gobrowser", "iemobile", "wap2.0","WAPI"};
Pattern pf = Pattern.compile("wp\\d*");
Matcher mf = pf.matcher(agentcheck);
if (agentcheck!=null&&(agentcheck.indexOf("windows nt") == -1 && agentcheck
.indexOf("Ubuntu") == -1)
|| (agentcheck.indexOf("windows nt") > -1 && mf.find())) {
for (int i = 0; i < keywords.length; i++) {
Pattern p = Pattern.compile(keywords[i]);
Matcher m = p.matcher(agentcheck);
//排除 蘋果案頭系統 和ipad 、iPod
if (m.find() && agentcheck.indexOf("ipad") == -1
&& agentcheck.indexOf("ipod") == -1
&& agentcheck.indexOf("macintosh") == -1) {
isWAP = true;
break;
}
}
}
if (isWAP) {
response.sendRedirect(wap);
}
%>

複製代碼 代碼如下:

/**手機瀏覽器的User-Agent裡的關鍵詞*/
"Nokia",//諾基亞,有山寨機也寫這個的,總還算是手機,Mozilla/5.0 (Nokia5800 XpressMusic)UC AppleWebkit(like Gecko) Safari/530
"SAMSUNG",//三星手機 SAMSUNG-GT-B7722/1.0+SHP/VPP/R5+Dolfin/1.5+Nextreaming+SMM-MMS/1.2.0+profile/MIDP-2.1+configuration/CLDC-1.1
"MIDP-2",//j2me2.0,Mozilla/5.0 (SymbianOS/9.3; U; Series60/3.2 NokiaE75-1 /110.48.125 Profile/MIDP-2.1 Configuration/CLDC-1.1 ) AppleWebKit/413 (KHTML like Gecko) Safari/413
"CLDC1.1",//M600/MIDP2.0/CLDC1.1/Screen-240X320
"SymbianOS",//塞班系統的,
"MAUI",//MTK山寨機預設ua
"UNTRUSTED/1.0",//疑似山寨機的ua,基本可以確定還是手機
"Windows CE",//Windows CE,Mozilla/4.0 (compatible; MSIE 6.0; Windows CE; IEMobile 7.11)
"iPhone",//iPhone是否也轉wap?不管它,先區分出來再說。Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_1 like Mac OS X; zh-cn) AppleWebKit/532.9 (KHTML like Gecko) Mobile/8B117
"iPad",//iPad的ua,Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; zh-cn) AppleWebKit/531.21.10 (KHTML like Gecko) Version/4.0.4 Mobile/7B367 Safari/531.21.10
"Android",//Android是否也轉wap?Mozilla/5.0 (Linux; U; Android 2.1-update1; zh-cn; XT800 Build/TITA_M2_16.22.7) AppleWebKit/530.17 (KHTML like Gecko) Version/4.0 Mobile Safari/530.17
"BlackBerry",//BlackBerry8310/2.7.0.106-4.5.0.182
"UCWEB",//ucweb是否只給wap頁面? Nokia5800 XpressMusic/UCWEB7.5.0.66/50/999
"ucweb",//小寫ucweb貌似是uc的Proxy 伺服器Mozilla/6.0 (compatible; MSIE 6.0;) Opera ucweb-squid
"BREW",//很奇怪的ua,例如:REW-Applet/0x20068888 (BREW/3.1.5.20; DeviceId: 40105; Lang: zhcn) ucweb-squid
"J2ME",//很奇怪的ua,只有J2ME四個字母
"YULONG",//宇龍手機,YULONG-CoolpadN68/10.14 IPANEL/2.0 CTC/1.0
"YuLong",//還是宇龍
"COOLPAD",//宇龍酷派YL-COOLPADS100/08.10.S100 POLARIS/2.9 CTC/1.0
"TIANYU",//天語手機TIANYU-KTOUCH/V209/MIDP2.0/CLDC1.1/Screen-240X320
"TY-",//天語,TY-F6229/701116_6215_V0230 JUPITOR/2.2 CTC/1.0
"K-Touch",//還是天語K-Touch_N2200_CMCC/TBG110022_1223_V0801 MTK/6223 Release/30.07.2008 Browser/WAP2.0
"Haier",//海爾手機,Haier-HG-M217_CMCC/3.0 Release/12.1.2007 Browser/WAP2.0
"DOPOD",//多普達手機
"Lenovo",// 聯想手機,Lenovo-P650WG/S100 LMP/LML Release/2010.02.22 Profile/MIDP2.0 Configuration/CLDC1.1
"LENOVO",// 聯想手機,比如:LENOVO-P780/176A
"HUAQIN",//華勤手機
"AIGO-",//愛國者居然也出過手機,AIGO-800C/2.04 TMSS-BROWSER/1.0.0 CTC/1.0
"CTC/1.0",//含義不明
"CTC/2.0",//含義不明
"CMCC",//移動定製手機,K-Touch_N2200_CMCC/TBG110022_1223_V0801 MTK/6223 Release/30.07.2008 Browser/WAP2.0
"DAXIAN",//大顯手機DAXIAN X180 UP.Browser/6.2.3.2(GUI) MMP/2.0
"MOT-",//摩托羅拉,MOT-MOTOROKRE6/1.0 LinuxOS/2.4.20 Release/8.4.2006 Browser/Opera8.00 Profile/MIDP2.0 Configuration/CLDC1.1 Software/R533_G_11.10.54R
"SonyEricsson",// 索愛手機,SonyEricssonP990i/R100 Mozilla/4.0 (compatible; MSIE 6.0; Symbian OS; 405) Opera 8.65 [zh-CN]
"GIONEE",//金立手機
"HTC",//HTC手機
"ZTE",//中興手機,ZTE-A211/P109A2V1.0.0/WAP2.0 Profile
"HUAWEI",//華為手機,
"webOS",//palm手機,Mozilla/5.0 (webOS/1.4.5; U; zh-CN) AppleWebKit/532.2 (KHTML like Gecko) Version/1.0 Safari/532.2 Pre/1.0
"GoBrowser",//3g GoBrowser.User-Agent=Nokia5230/GoBrowser/2.0.290 Safari
"IEMobile",//Windows CE手機內建瀏覽器,
"WAP2.0"//支援wap 2.0的
"wapi"//是中國標準 為了對抗wifi (wifi是不讓用的,行貨的這功能都閹割了,只有水貨手機有這功能) 是無線區域網路

常見智能機user agent String

Mozilla/5.0 (iPod; U; CPU iPhone OS 4_3_2 like Mac OS X; zh-cn) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8H7 Safari/6533.18.5

Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_2 like Mac OS X; zh-cn) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8H7 Safari/6533.18.5

MQQBrowser/25 (Linux; U; 2.3.3; zh-cn; HTC Desire S Build/GRI40;480*800)

Mozilla/5.0 (Linux; U; Android 2.3.3; zh-cn; HTC_DesireS_S510e Build/GRI40) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1

Mozilla/5.0 (SymbianOS/9.3; U; Series60/3.2 NokiaE75-1 /110.48.125 Profile/MIDP-2.1 Configuration/CLDC-1.1 ) AppleWebKit/413 (KHTML, like Gecko) Safari/413

Mozilla/5.0 (iPad; U; CPU OS 4_3_3 like Mac OS X; zh-cn) AppleWebKit/533.17.9 (KHTML, like Gecko) Mobile/8J2

Mozilla/5.0 (Windows NT 5.2) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.122 Safari/534.30

Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.202 Safari/535.1

Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/534.51.22 (KHTML, like Gecko) Version/5.1.1 Safari/534.51.22

Mozilla/5.0 (iPhone; CPU iPhone OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A5313e Safari/7534.48.3

Mozilla/5.0 (iPhone; CPU iPhone OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A5313e Safari/7534.48.3

Mozilla/5.0 (iPhone; CPU iPhone OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A5313e Safari/7534.48.3

Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.202 Safari/535.1

Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0; SAMSUNG; OMNIA7)      ----SAMSUNG MP7

Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0; XBLWP7; ZuneWP7)         ----HTC MP7

下面是用javascript實現的 因為涉及到頁面靜態化為題所有要用javascript
複製代碼 代碼如下:

<script type="text/javascript">
function iswap(){
var agentcheck=navigator.userAgent.toLowerCase();
var chesys=true;
var isWAP=false;
var keywords = ["mobile", "android",
"symbianos", "iphone","windows phone",
"mqqbrowser", "nokia", "samsung", "midp-2",
"untrusted/1.0", "windows ce", "blackberry","ucweb",
"brew", "j2me", "yulong", "coolpad", "tianyu", "ty-",
"k-touch", "haier", "dopod", "lenovo", "huaqin", "aigo-",
"ctc/1.0", "ctc/2.0", "cmcc", "daxian", "mot-",
"sonyericsson", "gionee", "htc", "zte", "huawei", "webos",
"gobrowser", "iemobile", "wap2.0","wapi"];
//排除 windows,蘋果等案頭系統 和ipad 、iPod
var rekeywords=["Windows 98", "Windows ME","Windows 2000","Windows XP","Windows NT","Ubuntu","ipad","ipod","macintosh"];

if (agentcheck!=null){
for (var i = 0; i < rekeywords.length; i++) {
if ( agentcheck.indexOf(rekeywords[i].toLowerCase())>-1){
chesys=false;
}
}
}

if (chesys){
for (var i = 0; i < keywords.length; i++) {
if ( agentcheck.indexOf(keywords[i].toLowerCase())>-1) {
isWAP = true;
break;
}
}
}
if (isWAP) {
window.location.href="http://3g.gf.com.cn";
}

}
</script>
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.