JSP判斷移動設備的正則
來源:易賢網(wǎng) 閱讀:721 次 日期:2015-01-27 15:18:21
溫馨提示:易賢網(wǎng)小編為您整理了“JSP判斷移動設備的正則”,方便廣大網(wǎng)友查閱!

天貓php判斷移動設備的正則(個人猜測),覺得很好用,于是就決定移植到JSP里面,大家可以參考下

看到了一篇很好的文章, 《在天貓,前端做什么?》,里面有天貓php判斷移動設備的正則(個人猜測),覺得很好用,于是就決定移植到JSP里面。

jsp文件名為 index.jsp,其實也可以使用過濾器來進行攔截,然后跳轉(zhuǎn)到其他域名去。

完整代碼如下:

代碼如下:

<import="java.util.regex.Matcher"%>

< import="java.util.regex.Pattern"%>

<%@ page language="java" pageEncoding="UTF-8"%>

<%!

// b 是單詞邊界(連著的兩個(字母字符 與 非字母字符) 之間的邏輯上的間隔),

// 字符串在編譯時會被轉(zhuǎn)碼一次,所以是 "b"

// B 是單詞內(nèi)部邏輯間隔(連著的兩個字母字符之間的邏輯上的間隔)

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";

String tableReg = "b(ipad|tablet|(Nexus 7)|up.browser"

+"|[1-4][0-9]{2}x[1-4][0-9]{2})b";

Pattern phonePat = Pattern.compile(phoneReg, Pattern.CASE_INSENSITIVE);

Pattern tablePat = Pattern.compile(tableReg, Pattern.CASE_INSENSITIVE);

public boolean checkMobile(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;

}

}

%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

//

String userAgent = request.getHeader( "USER-AGENT" ).toLowerCase();

if(null == userAgent){

userAgent = "";

}

if(checkMobile(userAgent)){

response.sendRedirect(basePath+"download.html");

//request.getRequestDispatcher("/download.html").forward(request,response);

} else {

response.sendRedirect(basePath+"index.html");

//request.getRequestDispatcher("/index.html").forward(request,response);

}

//

%>

<!DOCTYPE html>

<html lang="zh-cn">

<head>

<base href="<%=basePath%>">

<title>測試移動設備跳轉(zhuǎn)</title>

<meta http-equiv="pragma" content="no-cache">

<meta http-equiv="cache-control" content="no-cache">

<meta http-equiv="expires" content="0">

<meta http-equiv="keywords" content="測試,移動設備,跳轉(zhuǎn)">

<meta http-equiv="description" content="測試移動設備跳轉(zhuǎn)">

<!--

<link rel="stylesheet" type="text/css" href="styles.css">

-->

</head>

<body>

<div id="pagecontent" style="min-height:500px;_height:500px;">

正在運行!<br>

</div>

</body>

</html>

更多信息請查看IT技術(shù)專欄

更多信息請查看網(wǎng)絡編程
易賢網(wǎng)手機網(wǎng)站地址:JSP判斷移動設備的正則

2025國考·省考課程試聽報名

  • 報班類型
  • 姓名
  • 手機號
  • 驗證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機站點 | 投訴建議
工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務許可證:(云)人服證字(2023)第0102001523號
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)