JSP to judge the regular of mobile devices

Source: Internet
Author: User

Tmall PHP judged the regularity of mobile devices (personal guess) and found it very useful, so it was decided to port it to JSP, you can refer to it

I saw a very good article, "What is the front end on Tmall? 》, There is Tmall php to judge the regularity of mobile devices (personal guess), I think it is very useful, so I decided to transplant to JSP. The jsp file is named index.jsp. In fact, you can also use filters to intercept it and then jump to other domain names. The complete code is as follows: The 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 (alphabetical and non-alphabetic characters)), // the string will be compiled Transcode once, so it's "b" // B is the logical interval within the word (the logical interval 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 = "";} // 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 </ title> <meta http-equiv = "pragma" content = "no-cache"> <meta http-equiv = "cache-control" con tent = "no-cache"> <meta http-equiv = "expires" content = "0"> <meta http-equiv = "keywords" content = "test, mobile device, jump"> <meta http-equiv = "description" content = "Test mobile jump"> <!-<link rel = "stylesheet" type = "text / css" href = "styles.css">-> </ head> <body> < div id = "pagecontent" style = "min-height: 500px; _height: 500px;"> is running! <br> </ div> </ body> </ html>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.