JSP judge the regularity of mobile devices

Time:2020-10-24

I saw a good article, “what do you do in the front end of tmall? 》In it, tmall PHP judges the regularity of mobile devices (personal guess), which is very useful, so it decided to transplant it to JSP.

JSP file name is index.jsp In fact, you can also use the filter to intercept and then jump to other domain names.

The complete code is as follows:

Copy codeThe code is as follows:
<%@page import=”java.util.regex.Matcher”%>
<%@page import=”java.util.regex.Pattern”%>
<%@ page language=”java” pageEncoding=”UTF-8″%>
<%!

//”B” is the word boundary (the logical space between two consecutive (alphabetic and non alphabetic characters), “,
//The string is transcoded once at compile time, so it is “\ \ B”
//Is the logical space within a word (the logical space between two consecutive alphabetic 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|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 = “”;
}
//Match
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 > test mobile device jump
<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=”styles.css”>
–>
</head>

<body>

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

Running! <br>
</div>

</body>
</html>

Recommended Today

be based on fabric.js And qrcodejs2 composite poster image and file format

You first need to write these two elements in the template canvas(id=”canvas”) div(id=’qrcode’) Introduce JS, which is used to upload to alicloud fabric.js 、qrcodejs2 import { upload } from ‘~/static/js/upload.js’; import { fabric } from ‘fabric’; import QRCode from ‘qrcodejs2’; const QRCode = require(‘qrcodejs2’); //Both of these methods can introduce QRcode JS2. If you introduce […]