|
判断客户端手机类型,并跳转到相应的app下载页面
实现的原理,是检测浏览器的 USER-AGENT 这个header,然后根据正则表达式来确定客户端类型。
如果都不匹配,Fallback回退策略是显示对应的页面,让用户自己选择。
适合采用二维码扫描方式下载APP:
JSP版本的代码如下所示:其他服务端版本请百度搜索。
<%@page import="Java.util.regex.Matcher"%><%@page import="Java.util.regex.Pattern"%><%@ page language="Java" pageEncoding="UTF-8"%><%!// /b 是单词边界(连着的两个(字母字符 与 非字母字符) 之间的逻辑上的间隔),字符串在编译时会被转码一次,所以是 "//b"// /B 是单词内部逻辑间隔(连着的两个字母字符之间的逻辑上的间隔)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 href="http://m.jb51.NET/download.jsp?platform=Android" class="download"><img src="images/p_down_and.png" /></a> </div></body></html>
jsp技术:JSP中实现判断客户端手机类型并跳转到app下载页面,转载需保留来源!
郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。