JSP判斷移動(dòng)設(shè)備的正則
jsp文件名為 index.jsp,其實(shí)也可以使用過(guò)濾器來(lái)進(jìn)行攔截,然后跳轉(zhuǎn)到其他域名去。
完整代碼如下:
<%@page import="java.util.regex.Matcher"%>
<%@page import="java.util.regex.Pattern"%>
<%@ page language="java" pageEncoding="UTF-8"%>
<%!
// \b 是單詞邊界(連著的兩個(gè)(字母字符 與 非字母字符) 之間的邏輯上的間隔),
// 字符串在編譯時(shí)會(huì)被轉(zhuǎn)碼一次,所以是 "\\b"
// \B 是單詞內(nèi)部邏輯間隔(連著的兩個(gè)字母字符之間的邏輯上的間隔)
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>測(cè)試移動(dòng)設(shè)備跳轉(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="測(cè)試,移動(dòng)設(shè)備,跳轉(zhuǎn)">
<meta http-equiv="description" content="測(cè)試移動(dòng)設(shè)備跳轉(zhuǎn)">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<div id="pagecontent" style="min-height:500px;_height:500px;">
正在運(yùn)行!<br>
</div>
</body>
</html>
- js判斷當(dāng)前頁(yè)面在移動(dòng)設(shè)備還是在PC端中打開(kāi)
- js判斷瀏覽器類(lèi)型及設(shè)備(移動(dòng)頁(yè)面開(kāi)發(fā))
- JS和css實(shí)現(xiàn)檢測(cè)移動(dòng)設(shè)備方向的變化并判斷橫豎屏幕
- javascript判斷移動(dòng)端訪問(wèn)設(shè)備并解析對(duì)應(yīng)CSS的方法
- JS判斷移動(dòng)端訪問(wèn)設(shè)備并加載對(duì)應(yīng)CSS樣式
- js判斷是否是移動(dòng)設(shè)備登陸網(wǎng)頁(yè)的簡(jiǎn)單方法
- js判斷設(shè)備是否為PC并調(diào)整圖片大小
- JS如何判斷移動(dòng)端訪問(wèn)設(shè)備并解析對(duì)應(yīng)CSS
- Js如何判斷客戶端是PC還是手持設(shè)備簡(jiǎn)單分析
- JavaScript根據(jù)CSS的Media Queries來(lái)判斷瀏覽設(shè)備的方法
相關(guān)文章
jsp hibernate 數(shù)據(jù)保存操作的原理
當(dāng)執(zhí)行到session.save()方法時(shí),Hibernate并不會(huì)馬上生成insert SQL語(yǔ)句來(lái)進(jìn)行數(shù)據(jù)的保存,而是當(dāng)稍后清理session的緩存時(shí)才有可能執(zhí)行insert SQL語(yǔ)句,那么session.save()方法到底會(huì)執(zhí)行哪些步驟呢?2008-11-11
jsp使用ECharts動(dòng)態(tài)在地圖上標(biāo)識(shí)點(diǎn)
echarts地圖展示功能很強(qiáng)大,官網(wǎng)上靜態(tài)展示的例子很多了,動(dòng)態(tài)的資料少,需要參考本文的可以進(jìn)來(lái)了解一下。2016-10-10
jsp和servlet中實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)的方式實(shí)例總結(jié)
這篇文章主要介紹了jsp和servlet中實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)的方式,結(jié)合實(shí)例形式較為詳細(xì)的總結(jié)分析了jsp和servlet中實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)的常用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10
Eclipse XSD 生成枚舉類(lèi)型的Schema的實(shí)例詳解
這篇文章主要介紹了Eclipse XSD 生成枚舉類(lèi)型的Schema的實(shí)例詳解的相關(guān)資料,希望通過(guò)本能幫助到大家,需要的朋友可以參考下2017-09-09
使用IDEA編寫(xiě)jsp時(shí)EL表達(dá)式不起作用的問(wèn)題及解決方法
在使用IDEA開(kāi)發(fā)maven+springMVC項(xiàng)目時(shí)遇到不加載EL表達(dá)式的問(wèn)題,遇到此問(wèn)題的朋友不在少數(shù),今天小編給大家?guī)?lái)了使用IDEA編寫(xiě)jsp時(shí)EL表達(dá)式不起作用的問(wèn)題及解決方法,一起看看吧2018-08-08
通過(guò)JDBC連接oracle數(shù)據(jù)庫(kù)的十大技巧
通過(guò)JDBC連接oracle數(shù)據(jù)庫(kù)的十大技巧...2006-10-10
詳解struts2的token機(jī)制和cookie來(lái)防止表單重復(fù)提交
這篇文章主要介紹了詳解struts2的token機(jī)制和cookie來(lái)防止表單重復(fù)提交的相關(guān)資料,需要的朋友可以參考下2017-06-06

