Javaweb: determines whether the current request is accessed by a mobile device.

Source: Internet
Author: User

Javaweb: determines whether the current request is accessed by a mobile device.

During this time, I was working on pc-side business. The result manager found me and said that I may need to use mobile applications. I still wrote the background logic, but I need to modify the logic to see how to handle it.

Because the mobile and pc terminals are slightly different, I think it is best to make a unified judgment in one place without modifying the original code, so that the number of bugs can be reduced to a certain extent. My idea is to first determine whether the current request is a mobile terminal, then set a identifier to the session, and then you can handle it as needed. Whether you process the session independently or in a unified manner, you can directly read the session and make the corresponding judgment.

I encapsulated it into a class and want to share it with you:

Package com. tgb. util; import java. util. regex. matcher; import java. util. regex. pattern;/*** check whether the device is accessed by a mobile device ** @ author: Cuichenglong * @ group: tgb8 * @ Version: 1.00 * @ Date: 01:34:31 */public class CheckMobile {// \ B is the logical interval between two (letter and non-letter) words ), // The string will be transcoded once during compilation, so "\ B" // \ B is the internal logical interval of the word (the logical interval between two letters) 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 expression match: Mobile Phone and tablet static Pattern phonePat = Pattern. compile (phoneReg, Pattern. CASE_INSENSITIVE); static Pattern tablePat = Pattern. compile (tableReg, Pattern. CASE_INSENSITIVE);/*** check whether mobile device access is possible ** @ Title: check * @ Date: 01:29:07 * @ param userAgent browser ID * @ return true: mobile device access, false: pc Access */public static boolean check (String userAgent) {if (null = userAgent) {userAgent = "" ;}// match Matcher matcherPhone = phonePat. matcher (userAgent); Matcher matcherTable = tablePat. matcher (userAgent); if (matcherPhone. find () | matcherTable. find () {return true;} else {return false ;}}}

Usage:

/*** Check whether the access method is mobile ** @ Title: check * @ Date: 03:55:19 * @ param request * @ throws IOException */public boolean check (HttpServletRequest request, HttpServletResponse response) throws IOException {boolean isFromMobile = false; HttpSession session = request. getSession (); // check whether the access method (mobile or pc) has been recorded if (null = session. getAttribute ("ua") {try {// gets the ua, which is used to determine whether it is a mobile access String userAgent = request. getHeader ("USER-AGENT "). toLowerCase (); if (null = userAgent) {userAgent = "";} isFromMobile = CheckMobile. check (userAgent); // determines whether the request is sent to if (isFromMobile) {System. out. println ("mobile access"); session. setAttribute ("ua", "mobile");} else {System. out. println ("pc-side access"); session. setAttribute ("ua", "pc") ;}} catch (Exception e) {}} else {isFromMobile = session. getAttribute ("ua "). equals ("mobile");} return isFromMobile ;}

When logging on, or calling this method in action execute, you can determine whether the request is a mobile terminal without modifying the original business logic, and then perform corresponding processing based on the result, it should be much simpler.

In fact, I encountered a headache during my work. The manager said that mobile applications require the json format, so I want to perform a unified processing, then, read all the attributes set in the request and session to the map or list, and convert them to json output. The results of the beautiful ideas are a little cool. Take logon as an example. After logon, you will jump to the list. jsp page. The result is that the data in the list is output directly, but the page is not redirected. The page Jump conflicts with the returned json. If json is output, the output stream is closed and no jump is allowed. Otherwise, an error is prompted. I don't know if you have any good solutions. If not, you can only process each request separately.



How to determine whether a visitor is accessed by a mobile device and automatically jumps to the wap page

Support for js scripts in Mobile browsers: wap1.2 is not supported at all. wap2.0 is useful for WMLScript.

Can different mobile devices access different web pages?

Blog.csdn.net/..609406
Let's take a look at this. I don't know if it works. I used one before, mainly to judge the client device,

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.