判斷客戶端手機(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í)會被轉(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-manifesturl=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>
標(biāo)簽:西藏 宿州 江西 丹東 青島 貴州 紅河 云浮
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《JSP中實(shí)現(xiàn)判斷客戶端手機(jī)類型并跳轉(zhuǎn)到app下載頁面》,本文關(guān)鍵詞 JSP,中,實(shí)現(xiàn),判斷,客戶端,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。