加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-14 20:06:00 所属栏目:Java 来源:网络整理
导读:这个代码其实不是由我本人发现的,而是在翻看公司项目时无意间发现,觉得很实用所以拿出来大家分享。 框架:Struts2+spring+ibatis, 主要实现是依靠Http请求头Header中的 "User-Agent" 来完成,好了, 废话不多说直接上代码。 Action中: public String exe

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

框架:Struts2+spring+ibatis,

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

Action中:

public String execute() { 
    HttpServletRequest request = ServletActionContext.getRequest(); 
    boolean isMoblie = JudgeIsMoblie(request); 
    if(isMoblie){ 
      return "phone"; 
    } 
    return "pc"; 
  } 
   
  public boolean JudgeIsMoblie(HttpServletRequest request) { 
    boolean isMoblie = false; 
    String[] mobileAgents = { "iphone","android","phone","mobile","wap","netfront","java","opera mobi","opera mini","ucweb","windows ce","symbian","series","webos","sony","blackberry","dopod","nokia","samsung","palmsource","xda","pieplus","meizu","midp","cldc","motorola","foma","docomo","up.browser","up.link","blazer","helio","hosin","huawei","novarra","coolpad","techfaith","alcatel","amoi","ktouch","nexian","ericsson","philips","sagem","wellcom","bunjalloo","maui","smartphone","iemobile","spice","bird","zte-","longcos","pantech","gionee","portalmmm","jig browser","hiptop","benq","haier","^lct","320x320","240x320","176x220","w3c ","acs-","alav","alca","audi","avan","blac","blaz","brew","cell","cmd-","dang","doco","eric","hipt","inno","ipaq","jigs","kddi","keji","leno","lg-c","lg-d","lg-g","lge-","maxo","mits","mmef","mobi","mot-","moto","mwbp","nec-","newt","noki","oper","palm","pana","pant","phil","play","port","prox","qwap","sage","sams","sany","sch-","sec-","send","seri","sgh-","shar","sie-","siem","smal","smar","sph-","symb","t-mo","teli","tim-",/*"tosh",*/ "tsm-","upg1","upsi","vk-v","voda","wap-","wapa","wapi","wapp","wapr","webc","winw","xda-","Googlebot-Mobile" }; 
    if (request.getHeader("User-Agent") != null) { 
      for (String mobileAgent : mobileAgents) { 
        <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> 
        if (request.getHeader("User-Agent").toLowerCase() 
            .indexOf(mobileAgent) >= 0) { 
          isMoblie = true; 
          break; 
        } 
      } 
    } 
    return isMoblie; 
  } 

Struts2

<span style="white-space:pre">    
</span><action name="name" class="你自己的action类"> 
      <result name="pc">download/main.jsp</result> 
      <result name="phone">download/productPH.jsp</result> 
    </action> 

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

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

以上这篇java后台判断客户端是手机/PC并返回不同页面的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持编程小技巧。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读