欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

jsp判断请求来自手机示例代码

程序员文章站 2023-01-04 09:30:44
复制代码 代码如下: <% string wap = "http://3g.ssss.com.cn"; string agent = request.getheade...
复制代码 代码如下:

<%
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的代理服务器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>