This period of time is to do PC-side business, the result manager found me, may want to do mobile applications, background logic or I write the logic, but need to change some, to see how to deal with.
Because the mobile and PC side is slightly different, I think it is best to be in a unified judgment, and do not change the original code, this can reduce the number of bugs to a certain extent. my idea is to first determine whether the current request is mobile, then set a logo to the session, and then you can handle it casually. Whether you are dealing with it alone or in a unified process, you can judge the session directly by reading it.
I encapsulated it into a class that I now share with you:
Package Com.tgb.util;import Java.util.regex.matcher;import java.util.regex.pattern;/** * detects if mobile device access * * @author: Cuic Henglong * @group: TGB8 * @Version: 1.00 * @Date: 2014-7-7 pm 01:34:31 */public class Checkmobile {//\b is a word boundary (two in a row) (Alphabetic and non-alphabetic characters), the//string is transcoded at compile time, so "\\b"//\b is the logical interval within the word (a logical interval between two alphabetic characters connected) static string phonereg = "\ \ B (IP (hone|od) |android|opera m (ob|in) i "+" |windows (phone|ce) |blackberry "+" |s (Ymbian|eries60|amsung) |p (l AYBOOK|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, flat static Pattern Phonepat = Pattern.compile (Phonereg, pattern.case_insensitive); static Pattern Tablepat = Pattern.compile (Tablereg, pattern.case_insensitive); /** * Detection is mobile device access * * @Title: Check * @Date: 2014-7-7 pm 01:29:07 * @param USEragent Browser ID * @return true: Mobile device access, false:pc-side 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; } }}
How to use:
/** * Check access mode for mobile * * @Title: Check * @Date: 2014-7-7 pm 03:55:19 * @param request * @throws IOException */public boolean check (HTTPSERVL Etrequest request,httpservletresponse response) throws Ioexception{boolean Isfrommobile=false; HttpSession session= request.getsession (); Check if access is logged (mobile or PC side) if (Null==session.getattribute ("ua")) {try{//gets UA, used to determine if string useragent is accessed for the mobile end = Request.getheader ("User-agent"). toLowerCase (); if (null = = useragent) {useragent = ""; }isfrommobile=checkmobile.check (useragent);//Determines whether to access if (Isfrommobile) {System.out.println ("Mobile Access") for the mobile side; 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;}
In the login, or in the action of the Execute call this method, do not change the original business logic, you can determine whether the request is mobile, and then according to the results to do the corresponding processing, it should be much simpler.
In fact, I am in the process of doing, still encountered a more headache problem. The manager said that the mobile application requires the use of JSON format, so I want to do a unified processing, if it is accessed from the mobile side, then the request and the session set the attribute all read into the map or list, and then converted to JSON format output. Idea of the good, the result a little cruel. Take the login, login to jump to list.jsp page, the results now directly output the data in the list, but the page did not jump. Page jumps and returns JSON are conflicting. Output JSON, the output stream will be closed, do not let jump, otherwise it will prompt the error. I do not know if you have a good solution, if not, only each request processing alone.