过滤器 电脑端 手机端 判断

2016/09/11 17:10
阅读数 101
package cn.com.yyg.front.filter;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.filter.OncePerRequestFilter;

public class MobileOrPcFilter extends OncePerRequestFilter {

	/**
	 * pc mobile 判断
	 * 
	 * @param request
	 * @return
	 * @author linwk 2016年9月11日
	 */
	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;
	}

	@Override
	protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
                response.setContentType("text/html;charset=utf-8");
		HttpServletRequest httpRequest = (HttpServletRequest) request;
		String uri = httpRequest.getRequestURI();
		String mobileString = (String) httpRequest.getSession().getAttribute("mobile");
			// TODO 资源
		if (StringUtils.contains(uri, "assets")) {
filterChain.doFilter(request, response);// 放行
			return;
		}
		if (isMoblie(httpRequest)) {
			httpRequest.getRequestDispatcher("/wap" + uri).forward(request, response);
		} else {
			filterChain.doFilter(request, response);// 电脑放行
		}
	}
}

web.xml :


<!-- 电脑手机过滤器 -->
	<filter>
		<filter-name>wapfilter</filter-name>
		<filter-class>cn.com.yyg.front.filter.MobileOrPcFilter</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文件,从而实现地址不变,内容改变的效果。 这是我在做网站的一个小小的思路,希望也能帮助到有需要的朋友,如果你有更好的思路或建议,也欢迎评论中提出。

展开阅读全文
打赏
0
2 收藏
分享
加载中
更多评论
打赏
0 评论
2 收藏
0
分享
返回顶部
顶部