Java to determine the PC or mobile side

Source: Internet
Author: User

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

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.