Recently made a website, customer requirements in the access to the main domain name when the computer Access display computer page, mobile phone access to display H5 mobile phone page, this demand is more than the use of, especially when the site needs to promote Baidu, Baidu promotion requires the same domain name, mobile phone access to display mobile page, Show your computer when you visit your computer. Here is the idea of the need and implementation of the code to show. First of all, the analysis of ideas, mobile phone and computer access will have a different mark, mainly the request when the HTTP protocol request Header User-agent field, we first look at a real HTTP protocol request content. Use the browser's developer tools to request information. Different types of browsers can be differentiated by UA (user-agent), so we can get UA on the server side, and then we judge the UA to differentiate between a mobile browser or a computer browser. I am here to organize the UA of most commonly used mobile browsers:
1string[] Mobileagents ={"iphone", "Android", "Phone", "mobile" ,2 "WAP", "NetFront", "Java", "Opera Mobi", "Opera Mini", "UCWeb",3 "Windows CE", "Symbian", "series", "webOS", "Sony",4 "BlackBerry", "Dopod", "Nokia", "Samsung", "PalmSource", "XDA" ,5 "Pieplus", "Meizu", "MIDP", "CLDC", "Motorola", "Foma",6 "DoCoMo", "Up.browser", "Up.link", "Blazer", "Helio", "Hosin" ,7 "Huawei", "Novarra", "Coolpad", "webOS", "TechFaith" ,8 "PalmSource", "Alcatel", "Amoi", "Ktouch", "Nexian",9 "Ericsson", "Philips", "Sagem", "wellcom", "Bunjalloo", "Maui",Ten"Smartphone", "Iemobile", "Spice", "Bird", "zte-" ," Longcos ", One "Pantech", "Gionee", "portalmmm", "Jig Browser", "Hiptop", A"BenQ", "Haier", "^LCT", "320x320", "240x320", "176x220", -"acs-", "the" ," Alav "," Alca "," Amoi "," Audi "," Avan "," BenQ ", -"Bird", "Blac", "Blaz", "Brew", "Cell", "CLDC", "cmd-" ," Dang ", the "DoCo", "Eric", "Hipt", "Inno", "iPAQ", "Java", "Jigs", "KDDI", -"Keji", "Leno", "Lg-c", "lg-d", "Lg-g", "lge-" ," Maui "," Maxo " , -"MIDP", "MITs", "MMEF", "Mobi", "mot-", "Moto", "MWBP", "nec-", - "Newt", "Noki", "oper", "palm", "pana", "Pant", "Phil", "Play" , +"Port", "ProX", "Qwap", "Sage", "Sams", "Sany", "sch-", "sec-", -"Send", "Seri", "sgh-", "Shar", "sie-" ," Siem "," Smal "," Smar " , +"Sony", "sph-", "Symb", "T-mo", "Teli", "tim-", "Tosh", "tsm-", A"Upg1", "Upsi", "Vk-v", "Voda", "wap-" ," Wapa "," Wapi "," Wapp " , at"Wapr", "Webc", "winw", "winw", "XDA", "xda-", -"Googlebot-mobile"};
Stores the UA in a string array. It is then encapsulated as a way to determine if the phone is UA:
1 /**2 * Determine if it is mobile Access3 * 4 * @paramRequest5 * @return6 */7 Public BooleanIsmoblie (HttpServletRequest request) {8 BooleanIsmoblie =false;9String[] mobileagents = {"iphone", "Android", "Phone", "mobile",Ten"WAP", "NetFront", "Java", "Opera Mobi", "Opera Mini", "UCWeb", One"Windows CE", "Symbian", "series", "webOS", "Sony", A"BlackBerry", "Dopod", "Nokia", "Samsung", "PalmSource", "XDA", -"Pieplus", "Meizu", "MIDP", "CLDC", "Motorola", "Foma", -"DoCoMo", "Up.browser", "Up.link", "Blazer", "Helio", "Hosin", the"Huawei", "Novarra", "Coolpad", "webOS", "TechFaith", -"PalmSource", "Alcatel", "Amoi", "Ktouch", "Nexian", -"Ericsson", "Philips", "Sagem", "wellcom", "Bunjalloo", "Maui", -"Smartphone", "Iemobile", "Spice", "Bird", "zte-", "Longcos", +"Pantech", "Gionee", "portalmmm", "Jig Browser", "Hiptop", -"BenQ", "Haier", "^LCT", "320x320", "240x320", "176x220", +"acs-", "Alav", "Alca", "Amoi", "Audi", "Avan", "BenQ", A"Bird", "Blac", "Blaz", "Brew", "Cell", "CLDC", "cmd-", "Dang", at"DoCo", "Eric", "Hipt", "Inno", "iPAQ", "Java", "Jigs", "KDDI", -"Keji", "Leno", "Lg-c", "lg-d", "Lg-g", "lge-", "Maui", "Maxo", -"MIDP", "MITs", "MMEF", "Mobi", "mot-", "Moto", "MWBP", "nec-", -"Newt", "Noki", "oper", "palm", "pana", "Pant", "Phil", "Play", -"Port", "ProX", "Qwap", "Sage", "Sams", "Sany", "sch-", "sec-", -"Send", "Seri", "sgh-", "Shar", "sie-", "Siem", "Smal", "Smar", in"Sony", "sph-", "Symb", "T-mo", "Teli", "tim-", "Tosh", "tsm-", -"Upg1", "Upsi", "Vk-v", "Voda", "wap-", "Wapa", "Wapi", "Wapp", to"Wapr", "Webc", "winw", "winw", "XDA", "xda-", +"Googlebot-mobile" }; - if(Request.getheader ("user-agent")! =NULL) { the for(String mobileagent:mobileagents) { * if(Request.getheader ("User-agent"). toLowerCase () $. IndexOf (mobileagent) >= 0) {Panax NotoginsengIsmoblie =true; - Break; the } + } A } the returnIsmoblie; +}
At this point we have to consider how to intercept the client's request, previously thought of using the servlet URL matching to intercept, but will cause the interception of all requests including forwarding, and later thought that with the filter interceptor can produce interception and release effect, Use filter decisively (let's not talk about interceptors in the frame). The filter code is as follows:
1 Public classWapfilterImplementsFilter {2 @Override3 Public voidDoFilter (servletrequest request, servletresponse response,4Filterchain chain)throwsIOException, servletexception {5Response.setcontenttype ("Text/html;charset=utf-8");6HttpServletRequest HttpRequest =(httpservletrequest) request;7String URI =Httprequest.getrequesturi ();8 if(Ismoblie (HttpRequest)) {9Httprequest.getrequestdispatcher ("/wap" +URI). Forward (Request,Ten response); One}Else { AChain.dofilter (request, response);//Computer Release - } - } the //omit other code ... -}
Web. XML configuration File Code:
1 <Filter>2 <Filter-name>Wapfilter</Filter-name>3 <Filter-class>Cn.dtblog.filter.WapFilter</Filter-class>4 </Filter><filter-mapping>5 <Filter-name>Wapfilter</Filter-name>6 <Url-pattern>/*</Url-pattern>7 </filter-mapping>
We intercept all requests (lazy-ˇ∀ˇ), in the filter code we can see that the call to the mobile phone after the UA implementation of forwarding, and is forwarded to the WAP folder splicing request JSP name; The following is a table of stitching paths:
Computer Address |
Mobile URL after stitching WAP |
/index.jsp |
/wap/index.jsp |
/about.jsp |
/wap/about.jsp |
So we also need to create the same name in the WAP directory (H5 page) of the JSP file (also consider the hidden access under Web-inf), so that our phone when accessed, it will be forwarded to the specified JSP file, so that the address is unchanged, content changes the effect. This is my site in a small way of thinking, hope also can help to have the needs of friends, if you have better ideas or suggestions, also welcome comments in the proposed. The original is written by Bo Master Le Zhi Editor, the copyright belongs to the Bo owner. The original address http://www.dtblog.cn/1105.html reprint please specify the source!
Filter filter to achieve the same address different phone and computer pages