In JSP it can judge the type of client phone and jump to the download page of app

  • 2020-12-07 04:10:06
  • OfStack

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

The principle of the implementation is to detect the USER-ES5en of the browser, and then determine the client type based on the regular expression.

If none of them match, the Fallback fallback strategy is to display the corresponding page and let the user choose.
Suitable for downloading APP with 2-d code scanning:

JSP version of the code as shown below: other server version please Baidu search.


<%@page import="java.util.regex.Matcher"%>
<%@page import="java.util.regex.Pattern"%>
<%@ page language="java" pageEncoding="UTF-8"%>
<%!
// \b  It's the word boundary ( Two attached ( Alphabetic characters   with   Non-alphabetic character )  The logical interval between ), Strings are transcoded at compile time 1 time , 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 = "";
	}
	//  matching 
	Matcher matcherAndroid = androidPat.matcher(userAgent);
	if(matcherAndroid.find()){
		return true;
	} else {
		return false;
	}
}
public boolean likeIOS(String userAgent){
	if(null == userAgent){
		userAgent = "";
	}
	//  matching 
	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.ofstack.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> 
		
		<a href="itms-services://?action=download-manifest&url=http://m.ofstack.com/upload/client/yhjyios.plist" class="apple download"><img src="images/p_down_apple.png" /></a>
		<a href="http://m.ofstack.com/download.jsp?platform=android" class="download"><img src="images/p_down_and.png" /></a>
		
</div>
</body>
</html>


Related articles: