According to User-agent to judge the type of mobile phone and jump to the corresponding app download page _android

Source: Internet
Author: User
Tags alphanumeric characters

The principle of implementation is to detect the header of the browser's user-agent, and then to determine the client type based on the regular expression.

If they do not match, the fallback fallback strategy is to display the corresponding page, let the user choose for themselves.
Suitable to use two-dimensional code scanning way to download the app:

The code for the JSP version looks like this: Other service-side versions please Baidu search.

<% @page import= "Java.util.regex.Matcher"%> <% @page import= "java.util.regex.Pattern"%> <%@ page 
Language= "java" pageencoding= "UTF-8"%> <%! \b is a logical interval between a word boundary (a string of two letters and non-alphanumeric characters), the string is coded once at compile time, so it is "\\b"//\b is the logical interval between the internal logical intervals of the words (two alphabetic characters attached) string Androidre g = "\\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 = ""; 
}///match Matcher matcherandroid = Androidpat.matcher (useragent); 
if (Matcherandroid.find ()) {return true; 
else {return false; 
} public boolean Likeios (String useragent) {if (null = = useragent) {useragent = ""; 
}///match Matcher Matcherios = Iospat.matcher (useragent); 
if (Matcherios.find ()) {return true; 
else {return false; 
}%> <% String path = Request.getcontextpath (); StringBasePath = 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.iyhjy.com/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 ">  

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.