标签:
1 String[] 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”, 10 “smartphone”, “iemobile”, “spice”, “bird”, “zte-“, “longcos”, 11 “pantech”, “gionee”, “portalmmm”, “jig browser”, “hiptop”, 12 “benq”, “haier”, “^lct”, “320×320”, “240×320”, “176×220”, 13 “w3c “, “acs-“, “alav”, “alca”, “amoi”, “audi”, “avan”, “benq”, 14 “bird”, “blac”, “blaz”, “brew”, “cell”, “cldc”, “cmd-“, “dang”, 15 “doco”, “eric”, “hipt”, “inno”, “ipaq”, “java”, “jigs”, “kddi”, 16 “keji”, “leno”, “lg-c”, “lg-d”, “lg-g”, “lge-“, “maui”, “maxo”, 17 “midp”, “mits”, “mmef”, “mobi”, “mot-“, “moto”, “mwbp”, “nec-“, 18 “newt”, “noki”, “oper”, “palm”, “pana”, “pant”, “phil”, “play”, 19 “port”, “prox”, “qwap”, “sage”, “sams”, “sany”, “sch-“, “sec-“, 20 “send”, “seri”, “sgh-“, “shar”, “sie-“, “siem”, “smal”, “smar”, 21 “sony”, “sph-“, “symb”, “t-mo”, “teli”, “tim-“, “tosh”, “tsm-“, 22 “upg1”, “upsi”, “vk-v”, “voda”, “wap-“, “wapa”, “wapi”, “wapp”, 23 “wapr”, “webc”, “winw”, “winw”, “xda”, “xda-“, 24 “Googlebot-Mobile” };
1 /** 2 * 判断是否是手机访问 3 * 4 * @param request 5 * @return 6 */ 7 public boolean isMoblie(HttpServletRequest request) { 8 boolean isMoblie = false; 9 String[] mobileAgents = { "iphone", "android", "phone", "mobile", 10 "wap", "netfront", "java", "opera mobi", "opera mini", "ucweb", 11 "windows ce", "symbian", "series", "webos", "sony", 12 "blackberry", "dopod", "nokia", "samsung", "palmsource", "xda", 13 "pieplus", "meizu", "midp", "cldc", "motorola", "foma", 14 "docomo", "up.browser", "up.link", "blazer", "helio", "hosin", 15 "huawei", "novarra", "coolpad", "webos", "techfaith", 16 "palmsource", "alcatel", "amoi", "ktouch", "nexian", 17 "ericsson", "philips", "sagem", "wellcom", "bunjalloo", "maui", 18 "smartphone", "iemobile", "spice", "bird", "zte-", "longcos", 19 "pantech", "gionee", "portalmmm", "jig browser", "hiptop", 20 "benq", "haier", "^lct", "320x320", "240x320", "176x220", 21 "w3c ", "acs-", "alav", "alca", "amoi", "audi", "avan", "benq", 22 "bird", "blac", "blaz", "brew", "cell", "cldc", "cmd-", "dang", 23 "doco", "eric", "hipt", "inno", "ipaq", "java", "jigs", "kddi", 24 "keji", "leno", "lg-c", "lg-d", "lg-g", "lge-", "maui", "maxo", 25 "midp", "mits", "mmef", "mobi", "mot-", "moto", "mwbp", "nec-", 26 "newt", "noki", "oper", "palm", "pana", "pant", "phil", "play", 27 "port", "prox", "qwap", "sage", "sams", "sany", "sch-", "sec-", 28 "send", "seri", "sgh-", "shar", "sie-", "siem", "smal", "smar", 29 "sony", "sph-", "symb", "t-mo", "teli", "tim-", "tosh", "tsm-", 30 "upg1", "upsi", "vk-v", "voda", "wap-", "wapa", "wapi", "wapp", 31 "wapr", "webc", "winw", "winw", "xda", "xda-", 32 "Googlebot-Mobile" }; 33 if (request.getHeader("User-Agent") != null) { 34 for (String mobileAgent : mobileAgents) { 35 if (request.getHeader("User-Agent").toLowerCase() 36 .indexOf(mobileAgent) >= 0) { 37 isMoblie = true; 38 break; 39 } 40 } 41 } 42 return isMoblie; 43 }
1 public class WapFilter implements Filter { 2 @Override 3 public void doFilter(ServletRequest request, ServletResponse response, 4 FilterChain chain) throws IOException, ServletException { 5 response.setContentType("text/html;charset=utf-8"); 6 HttpServletRequest httpRequest = (HttpServletRequest) request; 7 String uri = httpRequest.getRequestURI(); 8 if (isMoblie(httpRequest)) { 9 httpRequest.getRequestDispatcher("/wap" + uri).forward(request, 10 response); 11 } else { 12 chain.doFilter(request, response);// 电脑放行 13 } 14 } 15 //省略其他代码…… 16 }
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>
电脑地址 | 拼接wap后的手机网址 |
/index.jsp | /wap/index.jsp |
/about.jsp | /wap/about.jsp |
标签:
原文地址:http://www.cnblogs.com/AIThink/p/5827758.html