最近做一个网站,客户要求在访问主域名的时候实现电脑访问时展示电脑页面,手机访问时展示h5的手机页面,这种需求的使用还是比较多的;尤其网站需要百度推广的时候,百度推广就要求同一域名下,手机访问时展示手机页面,电脑访问时展示电脑。
在这里将需求的思路和实现代码展示一下。
首先分析思路,手机和电脑访问的时候会有不同的标记,主要是请求时HTTP协议中的请求头的User-Agent字段,咱们先看一段真是的HTTP协议的请求内容。
使用浏览器的开发者工具就可以请求信息。不同类型的浏览器可以通过UA(User-Agent)去区分,所以我们在服务器端就可以去获取UA,然后我们判断UA来区分是手机浏览器还是电脑浏览器。我在这里整理了大部分常用的手机浏览器的UA:
String[] mobileAgents = { “iphone”, “android”, “phone”, “mobile”,
“wap”, “netfront”, “java”, “opera mobi”, “opera mini”, “ucweb”,
“windows ce”, “symbian”, “series”, “webos”, “sony”,
“blackberry”, “dopod”, “nokia”, “samsung”, “palmsource”, “xda”,
“pieplus”, “meizu”, “midp”, “cldc”, “motorola”, “foma”,
“docomo”, “up.browser”, “up.link”, “blazer”, “helio”, “hosin”,
“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”, “320×320”, “240×320”, “176×220”,
“w3c “, “acs-“, “alav”, “alca”, “amoi”, “audi”, “avan”, “benq”,
“bird”, “blac”, “blaz”, “brew”, “cell”, “cldc”, “cmd-“, “dang”,
“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-“,
“upg1”, “upsi”, “vk-v”, “voda”, “wap-“, “wapa”, “wapi”, “wapp”,
“wapr”, “webc”, “winw”, “winw”, “xda”, “xda-“,
“Googlebot-Mobile” };
将UA存放到字符串数组中。
然后封装成了一个判断是否是手机UA的方法:
/**
* 判断是否是手机访问
*
* @param request
* @return
*/
public boolean isMoblie(HttpServletRequest request) {
boolean isMoblie = false;
String[] mobileAgents = { "iphone", "android", "phone", "mobile",
"wap", "netfront", "java", "opera mobi", "opera mini", "ucweb",
"windows ce", "symbian", "series", "webos", "sony",
"blackberry", "dopod", "nokia", "samsung", "palmsource", "xda",
"pieplus", "meizu", "midp", "cldc", "motorola", "foma",
"docomo", "up.browser", "up.link", "blazer", "helio", "hosin",
"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",
"w3c ", "acs-", "alav", "alca", "amoi", "audi", "avan", "benq",
"bird", "blac", "blaz", "brew", "cell", "cldc", "cmd-", "dang",
"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-",
"upg1", "upsi", "vk-v", "voda", "wap-", "wapa", "wapi", "wapp",
"wapr", "webc", "winw", "winw", "xda", "xda-",
"Googlebot-Mobile" };
if (request.getHeader("User-Agent") != null) {
for (String mobileAgent : mobileAgents) {
if (request.getHeader("User-Agent").toLowerCase()
.indexOf(mobileAgent) >= 0) {
isMoblie = true;
break;
}
}
}
return isMoblie;
}
这个时候我们要考虑的就是如何拦截客户端的请求了,之前想过用Servlet的url匹配去拦截,但是会造成拦截所有请求包括转发,后来想到用Filter拦截器可以产生拦截和放行的效果,果断采用Filter(咱们先不谈框架里的拦截器)。
Filter代码如下:
public class WapFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
response.setContentType("text/html;charset=utf-8");
HttpServletRequest httpRequest = (HttpServletRequest) request;
String uri = httpRequest.getRequestURI();
if (isMoblie(httpRequest)) {
httpRequest.getRequestDispatcher("/wap" + uri).forward(request,
response);
} else {
chain.doFilter(request, response);// 电脑放行
}
}
//省略其他代码……
}
web.xml配置文件代码:
<filter>
<filter-name>wapfilter</filter-name>
<filter-class>cn.dtblog.filter.WapFilter</filter-class>
</filter><filter-mapping>
<filter-name>wapfilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
我们拦截所有的请求(懒汉做法(●ˇ∀ˇ●)),在Filter的代码中我们可以看到,判断为手机的UA后实现转发,并且是转发到wap文件夹下拼接请求的jsp的名字;下面给出一个拼接路径的对照表:
电脑地址 | 拼接wap后的手机网址 |
/index.jsp | /wap/index.jsp |
/about.jsp | /wap/about.jsp |
所以我们还需要在wap目录下创建文件名一样(h5页面)的jsp文件(也可以考虑放到WEB-INF下隐藏访问),这样我们手机在访问时,就会被转发到指定的jsp文件,从而实现地址不变,内容改变的效果。
这是我在做网站的一个小小的思路,希望也能帮助到有需要的朋友,如果你有更好的思路或建议,也欢迎评论中提出。