zoukankan      html  css  js  c++  java
  • 验证是手机端登陆还是web端登陆

    目的:

      手机端H5页面&web浏览器端 =》共用同一个域名的 且 跳转不同html

    实现:

      试了一下好用收藏一下。

    public class SysUtil {

      //  是单词边界(连着的两个(字母字符 与 非字母字符) 之间的逻辑上的间隔), // 字符串在编译时会被转码一次,所以是 "\b" // B 是单词内部逻辑间隔(连着的两个字母字符之间的逻辑上的间隔) static String phoneReg = "\b(ip(hone|od)|android|opera m(ob|in)i" +"|windows (phone|ce)|blackberry" +"|s(ymbian|eries60|amsung)|p(laybook|alm|rofile/midp" +"|laystation portable)|nokia|fennec|htc[-_]" +"|mobile|up.browser|[1-4][0-9]{2}x[1-4][0-9]{2})\b"; static String tableReg = "\b(ipad|tablet|(Nexus 7)|up.browser" +"|[1-4][0-9]{2}x[1-4][0-9]{2})\b"; //移动设备正则匹配:手机端、平板 static Pattern phonePat = Pattern.compile(phoneReg, Pattern.CASE_INSENSITIVE); static Pattern tablePat = Pattern.compile(tableReg, Pattern.CASE_INSENSITIVE); /** * 检测是否是移动设备访问 * @Title: check * @param userAgent 浏览器标识 * @return true:移动设备接入,false:pc端接入 */ public static boolean check(String userAgent){ if(null == userAgent){ userAgent = ""; } // 匹配 Matcher matcherPhone = phonePat.matcher(userAgent); Matcher matcherTable = tablePat.matcher(userAgent); if(matcherPhone.find() || matcherTable.find()){ return true; } else { return false; } } /** * 验证客户端类型 * true -> 手机 false ->web */ public static boolean ClientType() { HttpServletRequest request = HttpContext.getRequest(); boolean isFromMobile=false; HttpSession session= request.getSession(); //检查是否已经记录访问方式(移动端或pc端) if(null==session.getAttribute("ua")){ try{ //获取ua,用来判断是否为移动端访问 String userAgent = request.getHeader( "USER-AGENT" ).toLowerCase(); if(null == userAgent){ userAgent = ""; } isFromMobile=check(userAgent); //判断是否为移动端访问 if(isFromMobile){ System.out.println("移动端访问"); session.setAttribute("ua","mobile"); } else { System.out.println("pc端访问"); session.setAttribute("ua","pc"); } }catch(Exception e){} }else{ isFromMobile=session.getAttribute("ua").equals("mobile"); } return isFromMobile; } }

    调用: 

         if(ClientType()){// 移动端登陆
                return "/mobile.html";
            }else{// pc端登陆
                return "/index.html";
            }

    @

  • 相关阅读:
    slf4j的使用
    hashMap
    HBase
    HBase应用快速开发
    初学MongoDB 遇到提示由于目标计算机积极拒绝,无法连接
    Flask学习中运行“helloworld”出现UnicodeDecodeError: 'utf-8' codec can't decode问题
    doGet或doPost方法没有调用的一个原因
    markdown测试
    tomcat集成到IDEA与部署项目
    tomcat部署项目的方式
  • 原文地址:https://www.cnblogs.com/DarGi2019/p/14024113.html
Copyright © 2011-2022 走看看