Package com.*.*.*;
Import java.io.IOException;
Import Java.util.regex.Matcher;
Import Java.util.regex.Pattern;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
/**
* Detects if mobile device access is available
*/
public class Ismodel {
\b is the logical interval between the boundary of a word (two consecutive (alphabetic and non-alphabetic characters),
The string will be transcoded at compile time, so it is "\\b"
\b is the logical interval between the internal logic of a word (two alphabetic characters in a row)
static String Phonereg = "\\b (IP (hone|od) |android|opera m (ob|in) i"
+ "|windows (phone|ce) |blackberry"
+ "|s (Ymbian|eries60|amsung) |p (LAYBOOK|ALM|ROFILE/MIDP")
+ "|laystation portable) |nokia|fennec|htc[-_]"
+ "|mobile|up.browser| [1-4] [0-9] {2}x[1-4][0-9]{2}) \\b ";
static String Tablereg = "\\b (ipad|tablet| ( Nexus 7) |up.browser "
+ "| [1-4] [0-9] {2}x[1-4][0-9]{2}) \\b ";
Mobile device regular match: mobile phone, tablet
static Pattern Phonepat = Pattern.compile (Phonereg,
pattern.case_insensitive);
static Pattern Tablepat = Pattern.compile (Tablereg,
pattern.case_insensitive);
/**
* Detect if mobile device access is available
*
* @Title: Check
* @param useragent
* Browser ID
* @return true: Mobile device access, FALSE:PC end access
*/
public static Boolean check (String useragent) {
if (null = = useragent) {
useragent = "";
}
The
Matcher Matcherphone = Phonepat.matcher (useragent);
Matcher matchertable = Tablepat.matcher (useragent);
if (Matcherphone.find () | | Matchertable.find ()) {
return true;
} else {
return false;
}
}
/**
* Check whether the access mode is mobile
*
* @Title: Check
* @param request
* @throws IOException
*/
public static Boolean check (HttpServletRequest request,
HttpServletResponse response) throws IOException {
Boolean isfrommobile = false;
Check if access has been logged (mobile or PC-side)
try {
Gets the UA that is used to determine whether to access the mobile side
String useragent = Request.getheader ("User-agent"). toLowerCase ();
if (null = = useragent) {
useragent = "";
}
Isfrommobile = Ismodel.check (useragent);
Determine whether to access the mobile side
if (isfrommobile) {
System.out.println ("Mobile Access");
return isfrommobile;
} else {
System.out.println ("PC-side access");
return isfrommobile;
}
} catch (Exception e) {
E.printstacktrace ();
}
return isfrommobile;
}
}
Java to determine the PC or mobile side