java后台判断客户端是手机/PC并返回不同页面

这个代码其实不是由我本人发现的,而是在翻看公司项目时无意间发现,觉得很实用所以拿出来大家分享。

框架:Struts2+spring+ibatis,

   主要实现是依靠Http请求头Header中的 "User-Agent" 来完成,好了, 废话不多说直接上代码。


Action中:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public String execute() {  
  2.         HttpServletRequest request = ServletActionContext.getRequest();  
  3.         boolean isMoblie = JudgeIsMoblie(request);  
  4.         if(isMoblie){  
  5.             return "phone";  
  6.         }  
  7.         return "pc";  
  8.     }  
  9.       
  10.     public boolean JudgeIsMoblie(HttpServletRequest request) {  
  11.         boolean isMoblie = false;  
  12.         String[] mobileAgents = { "iphone""android""phone""mobile",  
  13.                 "wap""netfront""java""opera mobi""opera mini""ucweb",  
  14.                 "windows ce""symbian""series""webos""sony",  
  15.                 "blackberry""dopod""nokia""samsung""palmsource""xda",  
  16.                 "pieplus""meizu""midp""cldc""motorola""foma",  
  17.                 "docomo""up.browser""up.link""blazer""helio""hosin",  
  18.                 "huawei""novarra""coolpad""webos""techfaith",  
  19.                 "palmsource""alcatel""amoi""ktouch""nexian",  
  20.                 "ericsson""philips""sagem""wellcom""bunjalloo""maui",  
  21.                 "smartphone""iemobile""spice""bird""zte-""longcos",  
  22.                 "pantech""gionee""portalmmm""jig browser""hiptop",  
  23.                 "benq""haier""^lct""320x320""240x320""176x220",  
  24.                 "w3c ""acs-""alav""alca""amoi""audi""avan""benq",  
  25.                 "bird""blac""blaz""brew""cell""cldc""cmd-""dang",  
  26.                 "doco""eric""hipt""inno""ipaq""java""jigs""kddi",  
  27.                 "keji""leno""lg-c""lg-d""lg-g""lge-""maui""maxo",  
  28.                 "midp""mits""mmef""mobi""mot-""moto""mwbp""nec-",  
  29.                 "newt""noki""oper""palm""pana""pant""phil""play",  
  30.                 "port""prox""qwap""sage""sams""sany""sch-""sec-",  
  31.                 "send""seri""sgh-""shar""sie-""siem""smal""smar",  
  32.                 "sony""sph-""symb""t-mo""teli""tim-"/*"tosh",*/ "tsm-",  
  33.                 "upg1""upsi""vk-v""voda""wap-""wapa""wapi""wapp",  
  34.                 "wapr""webc""winw""winw""xda""xda-",  
  35.                 "Googlebot-Mobile" };  
  36.         if (request.getHeader("User-Agent") != null) {  
  37.             for (String mobileAgent : mobileAgents) {  
  38.                 <span class="comment" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 130, 0); font-family: Consolas, 'Courier New', Courier, mono, serif; line-height: 18px;">//这里本宝宝表示不怎么了解它的内部原理,但是知道个大概意思就得了。</span>  
  39.                 if (request.getHeader("User-Agent").toLowerCase()  
  40.                         .indexOf(mobileAgent) >= 0) {  
  41.                     isMoblie = true;  
  42.                     break;  
  43.                 }  
  44.             }  
  45.         }  
  46.         return isMoblie;  
  47.     }  


 

Struts2

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <span style="white-space:pre">        </span><action name="name" class="你自己的action类">  
  2.             <result name="pc">download/main.jsp</result>  
  3.             <result name="phone">download/productPH.jsp</result>  
  4.         </action>  


好了,代码就只是这么多,木有了木有了。。。  是不是感觉很简单的说,


虽然不是我写出来的,但是看到这个代码,也是有一种收获的,其实很多设置都是包含在请求头中的,因为不管是什么浏览器什么设备,它要通过http去调用一个请求,那么它就必定是有http协议,只要精通http协议,那么啥事都好办(表示本屌目前不懂http协议,而后努力学习学习,再来装逼)~~

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值