jsp文件名为index.jsp,其实也可以使用过滤器来进行拦截,然后跳转到其他域名去。
完整代码如下:
复制代码代码如下:
<%@pageimport="java.util.regex.Matcher"%>
<%@pageimport="java.util.regex.Pattern"%>
<%@pagelanguage="java"pageEncoding="UTF-8"%>
<%!
//\b是单词边界(连着的两个(字母字符与非字母字符)之间的逻辑上的间隔),
//字符串在编译时会被转码一次,所以是"\\b"
//\B是单词内部逻辑间隔(连着的两个字母字符之间的逻辑上的间隔)
StringphoneReg="\\b(ip(hone|od)|android|operam(ob|in)i"
+"|windows(phone|ce)|blackberry"
+"|s(ymbian|eries60|amsung)|p(laybook|alm|rofile/midp"
+"|laystationportable)|nokia|fennec|htc[-_]"
+"|mobile|up.browser|[1-4][0-9]{2}x[1-4][0-9]{2})\\b";
StringtableReg="\\b(ipad|tablet|(Nexus7)|up.browser"
+"|[1-4][0-9]{2}x[1-4][0-9]{2})\\b";
PatternphonePat=Pattern.compile(phoneReg,Pattern.CASE_INSENSITIVE);
PatterntablePat=Pattern.compile(tableReg,Pattern.CASE_INSENSITIVE);
publicbooleancheckMobile(StringuserAgent){
if(null==userAgent){
userAgent="";
}
//匹配
MatchermatcherPhone=phonePat.matcher(userAgent);
MatchermatcherTable=tablePat.matcher(userAgent);
if(matcherPhone.find()||matcherTable.find()){
returntrue;
}else{
returnfalse;
}
}
%>
<%
Stringpath=request.getContextPath();
StringbasePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
//
StringuserAgent=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);
}
//
%>
<!DOCTYPEhtml>
<htmllang="zh-cn">
<head>
<basehref="<%=basePath%>">
<title>测试移动设备跳转</title>
<metahttp-equiv="pragma"content="no-cache">
<metahttp-equiv="cache-control"content="no-cache">
<metahttp-equiv="expires"content="0">
<metahttp-equiv="keywords"content="测试,移动设备,跳转">
<metahttp-equiv="description"content="测试移动设备跳转">
<!--
<linkrel="stylesheet"type="text/css"href="styles.css">
-->
</head>
<body>
<divid="pagecontent"style="min-height:500px;_height:500px;">
正在运行!<br>
</div>
</body>
</html>
|