JSP to determine the client mobile phone type and jump to the app download page, jspapp
Determine the client mobile phone type and jump to the corresponding app Download Page
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.
<% @ Page import = "java. util. regex. matcher "%> <% @ page import =" java. util. regex. pattern "%> <% @ page language =" java "pageEncoding =" UTF-8 "%> <%! // \ B is the logical interval between two consecutive words (letter and non-letter). A string is transcoded once during compilation, so "\ B" // \ B is the logical interval between words (the logical interval between two consecutive letters) 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.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> download client-permanent memory </title> <link href =" css/style.css "rel =" stylesheet "type =" text/css" />
How does JSP identify a mobile client jump to a WAP webpage?
Determine the browser header, such as the password and browser
String agent = request. getHeader ("user-agent ");
Then judge agent. contains ("MicroMessenger")
Agent. contains ("YiXin") is
In jsp, how does one automatically jump to the previous page in 3 seconds?
<%
} Else {
Response. setHeader ("refresh", "3; URL = Login. jsp"); // here, 3 is the URL of the second to be redirected.
%>
<Font color = "red" size = "5"> you have not logged on. Please log on first <br>
The logon page will be displayed in three seconds. <br> if no jump is made, click <a href = "Login. jsp"> here </a> !!!
<Br> </font>
<%
}
%>