Javaweb distinguishes between PC-side and mobile-side __java

Source: Internet
Author: User
Javaweb: Determine if the current request is for mobile device accessCategory: "Java" "Good Technology to share" "Javaweb"

This time is doing on the PC side of the business, the result manager found me said, may have to do the mobile end of the application, background logic or with my writing logic, but need to change some, see how to deal with.

Since the mobile end is slightly different from the PC side, I think it is best to make a unified judgment in one place and not to change the original code, so as to reduce the number of bugs to a certain extent. My idea is to first determine whether the current request is a mobile end, then set an identity to the session, and then you can handle it casually. Whether you are dealing alone, or unified processing, direct reading session can make the corresponding judgments.

I encapsulated it into a class that is now shared with everyone:

[Java]  View plain copy package com.tgb.util;      import java.util.regex.matcher;    import java.util.regex.pattern;     /**   *  detection for mobile-end device access    *    *  @author       : Cuichenglong   *  @group        : tgb8   *  @Version       : 1.00   *  @Date         :  2014-7-7  pm 01:34:31   */   public class checkmobile {              // \b  is the word boundary (two consecutive letters   and   non-alphabetic characters) The logical interval between  ),         //  string will be transcoding once at compile time, so it is   "\\b"           // \B  is the internal logical interval of the word (the logical interval between the two alphabetic characters attached)        &NBSp;  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 matching: Mobile end, flat        static  Pattern phonepat = pattern.compile (phonereg, pattern.case_insensitive);          static pattern tablepat = pattern.compile (tableReg,  pattern.case_insensitive);                   /**       *  detect if mobile device access        *         *  @Title: check       *  @Date  : 2014-7-7  pm 01:29:07       *  @param  useragent   Browser logo        *  @return  true: Mobile device access, FALSE:PC terminal access         */       public static boolean check (String  useragent) {             if (null == useragent) {                  useragent =   "";             }             //  Matching               matcher matcherphone = phonepat.matcher (userAgent);              matcher matchertable = tablepat.matcher ( useragent);             if (MatcherPhone.find ()  | |  matchertable.find ()) {                  return true;    

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.