Determine the client mobile phone type, and jump to the corresponding app download page, client app

Source: Internet
Author: User
Tags servervariables

Determine the client mobile phone type, and jump to the corresponding app download page, client app

The principle is to check the browser's USER-AGENT header and then determine the client type based on the regular expression.

If none of them match, the Fallback rollback policy displays the corresponding page for you to choose.
It is suitable for downloading apps by QR code scanning:

The JSP version code is as follows: Baidu searches for other server versions.

Original post address: [You can also visit the upstream forum to view more industry, open-source, and recruitment information]

<% @ page import = "java.util.regex.Matcher"%>
<% @ page import = "java.util.regex.Pattern"%>
<% @ page language = "java" pageEncoding = "UTF-8"%>
<%!
// \ b is the word boundary (the logical interval between two consecutive (alphabetic characters and non-alphabetic characters)), the string will be transcoded once during compilation, so it is "\\ b"
// \ B is the logical interval within the word (the logical interval between two consecutive alphabetic characters)

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 = "";
}
// 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 ();
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.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">
<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> Download Client-Eternal Memory </ 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>
The
<a href="itms-services://?action=download-manifest&url=http://m.iyhjy.com/upload/client/yhjyios.plist" class="apple download"> <img src = "images / p_down_apple.png "/> </a>
<a href="http://m.iyhjy.com/download.jsp?platform=android" class="download"> <img src = "images / p_down_and.png" /> </a>
The
</ div>
</ body>
</ html>



The ASP implementation detects that the client is a PC or a mobile phone and automatically jumps to the corresponding URL?
<%
if instr (request.servervariables ("http_user_agent"), "Mobile")> 0 then
'Mobile
else
'PC
end if
%>

Limited to the conditions, I only tested the Android system phones and tablets. If it is a smartphone of Apple, Saipan, Blackberry and other systems, you can test it yourself by creating a new ASP file with only one line:
<% = request.servervariables ("http_user_agent")%>
Then put it on your website, and then use the browser on your phone to open it, see what character string appears in the displayed text, and then use it as a judgment condition.
 
Who wrote me an ASP to determine whether the client is a mobile phone or a computer, and then add it to the head of the web page, automatically jump to the WAP page or WEB page

Check whether the http response type HTTP_ACCEPT in the environment variable has wap information
<%
if InStr (LCase (Request.ServerVariables ("HTTP_ACCEPT")), "text / vnd.wap.wml")> 0 then
response.redirect "wap.asp" 'If it is a mobile phone visit, jump to wap.asp
else
response.redirect "index.asp" 'If the computer visits jump to the home page
end if
response.end
%>

Code 2 <% If InStr (LCase (Request.ServerVariables ("HTTP_ACCEPT")), "text / vnd.wap.wml")> 0 Then Response.Redirect "wap.asp"%>

three
<% If InStr (LCase (Request.ServerVariables ("HTTP_ACCEPT")), "text / vnd.wap.wml")> 0 Then Response.Redirect "big5 / index.htm" Elseif InStr (LCase (Request.ServerVariables (" HTTP_USER_AGENT "))," iphone ")> 0 Then Response.Redirect" big5 / index.htm "else Response.Redirect" index.html "end if%>

Related Article

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.