SoFunction
Updated on 2025-03-09

JSP determines the rules for mobile devices


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

// \b is the word boundary (the logical interval between two connected (alphabetical characters and non-alphabetical characters),
// The string will be transcoded once during compilation, so it is "\\b"
// \B is the logical interval inside the word (the logical interval between the two connected alphabetical characters)
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||[1-4][0-9]{2}x[1-4][0-9]{2})\\b";
String tableReg = "\\b(ipad|tablet|(Nexus 7)|"
+"|[1-4][0-9]{2}x[1-4][0-9]{2})\\b";
Pattern phonePat = (phoneReg, Pattern.CASE_INSENSITIVE);
Pattern tablePat = (tableReg, Pattern.CASE_INSENSITIVE);

public boolean checkMobile(String userAgent){
if(null == userAgent){
userAgent = "";
}
// Match
Matcher matcherPhone = (userAgent);
Matcher matcherTable = (userAgent);
if(() || ()){
return true;
} else {
return false;
}
}
%>
<%
String path = ();
String basePath = ()+"://"+()+":"+()+path+"/";

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

if(null == userAgent){
userAgent = "";
}
if(checkMobile(userAgent)){
(basePath+"");
//("/").forward(request,response);
} else {
(basePath+"");
//("/").forward(request,response);
}
//
%>

<!DOCTYPE html>
<html lang="zh-cn">
<head>
<base href="<%=basePath%>">

<title>Test mobile device jump</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="test, mobile device, jump">
<meta http-equiv="description" content="test mobile device jump">
<!--
<link rel="stylesheet" type="text/css" href="">
-->
</head>

<body>

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

Running! <br>
</div>

</body>
</html>