JSP判斷行動裝置的正則

來源:互聯網
上載者:User

 天貓php判斷行動裝置的正則(個人猜測),覺得很好用,於是就決定移植到JSP裡面,大家可以參考下

看到了一篇很好的文章, 《在天貓,前端做什嗎?》,裡面有天貓php判斷行動裝置的正則(個人猜測),覺得很好用,於是就決定移植到JSP裡面。  jsp檔案名稱為 index.jsp,其實也可以使用過濾器來進行攔截,然後跳轉到其他網域名稱去。  完整代碼如下: 代碼如下:<%@page import="java.util.regex.Matcher"%> <%@page import="java.util.regex.Pattern"%> <%@ page language="java" pageEncoding="UTF-8"%> <%!  // b 是單詞邊界(連著的兩個(字母字元 與 非字母字元) 之間的邏輯上的間隔), // 字串在編譯時間會被轉碼一次,所以是 "b" // B 是單詞內部邏輯間隔(連著的兩個字母字元之間的邏輯上的間隔) 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>測試行動裝置跳轉</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="測試,行動裝置,跳轉"> <meta http-equiv="description" content="測試行動裝置跳轉"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head>  <body>  <div id="pagecontent" style="min-height:500px;_height:500px;">  正在運行!<br> </div>  </body> </html>  
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.