JSP中實(shí)現(xiàn)判斷客戶端手機(jī)類型并跳轉(zhuǎn)到app下載頁面
更新時(shí)間:2014年09月04日 09:38:02 投稿:junjie
這篇文章主要介紹了JSP中實(shí)現(xiàn)判斷客戶端手機(jī)類型并跳轉(zhuǎn)到app下載頁面,實(shí)現(xiàn)的原理,是檢測瀏覽器的 USER-AGENT 這個(gè)header,然后根據(jù)正則表達(dá)式來確定客戶端類型,需要的朋友可以參考下
判斷客戶端手機(jī)類型,并跳轉(zhuǎn)到相應(yīng)的app下載頁面
實(shí)現(xiàn)的原理,是檢測瀏覽器的 USER-AGENT 這個(gè)header,然后根據(jù)正則表達(dá)式來確定客戶端類型。
如果都不匹配,F(xiàn)allback回退策略是顯示對應(yīng)的頁面,讓用戶自己選擇。
適合采用二維碼掃描方式下載APP:
JSP版本的代碼如下所示:其他服務(wù)端版本請百度搜索。
<%@page import="java.util.regex.Matcher"%> <%@page import="java.util.regex.Pattern"%> <%@ page language="java" pageEncoding="UTF-8"%> <%! // \b 是單詞邊界(連著的兩個(gè)(字母字符 與 非字母字符) 之間的邏輯上的間隔),字符串在編譯時(shí)會(huì)被轉(zhuǎn)碼一次,所以是 "\\b" // \B 是單詞內(nèi)部邏輯間隔(連著的兩個(gè)字母字符之間的邏輯上的間隔) String androidReg = "\\bandroid|Nexus\\b"; String iosReg = "ip(hone|od|ad)"; Pattern androidPat = Pattern.compile(androidReg, Pattern.CASE_INSENSITIVE); Pattern iosPat = Pattern.compile(iosReg, Pattern.CASE_INSENSITIVE); public boolean likeAndroid(String userAgent){ if(null == userAgent){ userAgent = ""; } // 匹配 Matcher matcherAndroid = androidPat.matcher(userAgent); if(matcherAndroid.find()){ return true; } else { return false; } } public boolean likeIOS(String userAgent){ if(null == userAgent){ userAgent = ""; } // 匹配 Matcher matcherIOS = iosPat.matcher(userAgent); if(matcherIOS.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(); System.out.println("userAgent: "+userAgent); if(null == userAgent){ userAgent = ""; } if(likeAndroid(userAgent)){ System.out.println("likeAndroid: "+true); response.sendRedirect("http://m.jb51.net/download.jsp?platform=android"); return; //request.getRequestDispatcher("/download.html").forward(request,response); } else if(likeIOS(userAgent)){ System.out.println("likeIOS: "+true); response.sendRedirect("http://itunes.apple.com/us/app/id714751061"); return; //request.getRequestDispatcher("/index.html").forward(request,response); } %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no"> <title>下載客戶端 - 永恒記憶</title> <link href="css/style.css" rel="stylesheet" type="text/css" /> </head> <body> <div class="p_down"> <div> <a href="index.html"> <img src="images/p_logo.png" class="p_logo" /> </a> </div> <a href="itms-services://?action=download-manifest&url=http://m.jb51.net/upload/client/yhjyios.plist" class="apple download"><img src="images/p_down_apple.png" /></a> <a class="download"><img src="images/p_down_and.png" /></a> </div> </body> </html>
您可能感興趣的文章:
- JS如何實(shí)現(xiàn)網(wǎng)站中PC端和手機(jī)端自動(dòng)識(shí)別并跳轉(zhuǎn)對應(yīng)的代碼
- JS前端開發(fā)判斷是否是手機(jī)端并跳轉(zhuǎn)操作(小結(jié))
- JS跳轉(zhuǎn)手機(jī)站url的若干注意事項(xiàng)
- js根據(jù)手機(jī)客戶端瀏覽器類型,判斷跳轉(zhuǎn)官網(wǎng)/手機(jī)網(wǎng)站多個(gè)實(shí)例代碼
- 基于JavaScript代碼實(shí)現(xiàn)pc與手機(jī)之間的跳轉(zhuǎn)
- js判斷手機(jī)訪問或者PC的幾個(gè)例子(常用于手機(jī)跳轉(zhuǎn))
- 兩款JS腳本判斷手機(jī)瀏覽器類型跳轉(zhuǎn)WAP手機(jī)網(wǎng)站
- JS腳本根據(jù)手機(jī)瀏覽器類型跳轉(zhuǎn)WAP手機(jī)網(wǎng)站(兩種方式)
- 百度判斷手機(jī)終端并自動(dòng)跳轉(zhuǎn)js代碼及使用實(shí)例
- 手機(jī)平板等移動(dòng)端適配跳轉(zhuǎn)URL的js代碼
- JS自動(dòng)跳轉(zhuǎn)手機(jī)移動(dòng)網(wǎng)頁的實(shí)現(xiàn)方法
相關(guān)文章
淺談SpringMVC jsp前臺(tái)獲取參數(shù)的方式 EL表達(dá)式
下面小編就為大家分享一篇淺談SpringMVC jsp前臺(tái)獲取參數(shù)的方式 EL表達(dá)式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03