後臺java代碼:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 查看是否爲手機登陸
*
*/
public class CheckMobile {
<span style="white-space:pre"> </span>// \b 是單詞邊界(連着的兩個(字母字符 與 非字母字符) 之間的邏輯上的間隔),
<span style="white-space:pre"> </span>// 字符串在編譯時會被轉碼一次,所以是 "\\b"
<span style="white-space:pre"> </span>// \B 是單詞內部邏輯間隔(連着的兩個字母字符之間的邏輯上的間隔)
<span style="white-space:pre"> </span>static String phoneReg = "\\b(ip(hone|od)|android|opera m(ob|in)i"
<span style="white-space:pre"> </span>+ "|windows (phone|ce)|blackberry"
<span style="white-space:pre"> </span>+ "|s(ymbian|eries60|amsung)|p(laybook|alm|rofile/midp"
<span style="white-space:pre"> </span>+ "|laystation portable)|nokia|fennec|htc[-_]"
<span style="white-space:pre"> </span>+ "|mobile|up.browser|[1-4][0-9]{2}x[1-4][0-9]{2})\\b";
<span style="white-space:pre"> </span>static String tableReg = "\\b(ipad|tablet|(Nexus 7)|up.browser"
<span style="white-space:pre"> </span>+ "|[1-4][0-9]{2}x[1-4][0-9]{2})\\b";
<span style="white-space:pre"> </span>// 移動設備正則匹配:手機端、平板
<span style="white-space:pre"> </span>static Pattern phonePat = Pattern.compile(phoneReg,
<span style="white-space:pre"> </span>Pattern.CASE_INSENSITIVE);
<span style="white-space:pre"> </span>static Pattern tablePat = Pattern.compile(tableReg,
<span style="white-space:pre"> </span>Pattern.CASE_INSENSITIVE);
<span style="white-space:pre"> </span>/**
<span style="white-space:pre"> </span> * 檢測是否是移動設備訪問
<span style="white-space:pre"> </span> *
<span style="white-space:pre"> </span> * @Title: check
<span style="white-space:pre"> </span> * @Date : 2014-7-7 下午01:29:07
<span style="white-space:pre"> </span> * @param userAgent
<span style="white-space:pre"> </span> * 瀏覽器標識
<span style="white-space:pre"> </span> * @return true:移動設備接入,false: pc端接入
<span style="white-space:pre"> </span> */
<span style="white-space:pre"> </span>public static boolean check(String userAgent) {
<span style="white-space:pre"> </span>if (null == userAgent) {
<span style="white-space:pre"> </span>userAgent = "";
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>// 匹配
<span style="white-space:pre"> </span>Matcher matcherPhone = phonePat.matcher(userAgent);
<span style="white-space:pre"> </span>Matcher matcherTable = tablePat.matcher(userAgent);
<span style="white-space:pre"> </span>if (matcherPhone.find() || matcherTable.find()) {
<span style="white-space:pre"> </span>return true;
<span style="white-space:pre"> </span>} else {
<span style="white-space:pre"> </span>return false;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
}
判斷應用:
/**
* 檢查訪問方式是否爲移動端
*
* @Title: check
* @Date : 2014-7-7 下午03:55:19
* @param request
* @throws IOException
*/
public boolean check(HttpServletRequest request,HttpServletResponse response) throws IOException{
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=CheckMobile.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;
}