文档章节

过滤器 电脑端 手机端 判断

林伟琨
 林伟琨
发布于 2016/09/11 17:10
字数 533
阅读 41
收藏 2
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文件,从而实现地址不变,内容改变的效果。 这是我在做网站的一个小小的思路,希望也能帮助到有需要的朋友,如果你有更好的思路或建议,也欢迎评论中提出。

本文转载自:http://www.dtblog.cn/1105.html

林伟琨
粉丝 8
博文 108
码字总数 39035
作品 0
厦门
后端工程师
私信 提问
基于Token认证的多点登录和Api保护

  在文章中有错误的地方,或是有建议或意见的地方,请大家多多指正,邮箱: linjie.rd@gmail.com   一天张三,李四,王五,赵六去动物园,张三没买票,李四制作了个假票,王五买了票,赵六要直接FQ进...

linjierd
2018/12/11
0
0
如何模拟手机弱网络测试

如何模拟手机弱网络测试 发表于:2017-6-29 11:25 作者:51Testing 来源:51Testing软件测试网原创 字体:大 中 小 | 上一篇 | 下一篇 | 打印 |我要投稿 | 推荐标签: 弱网测试   移动互联...

唐磊1118
2017/06/30
0
0
【Python】利用邮件远程控制自己电脑

本文转载至知乎ID:Charles(白露未晞)知乎个人专栏 下载W3Cschool手机App,0基础随时随地学编程>>戳此了解 导语 最近经常需要简单地远程控制一下电脑,比如远程查看一下电脑上模型训练的情...

W3Cschool小编
2018/08/13
0
0
Android ADB 使用教程

adb 译者署名: 移动云_文斌 译者链接:http://blog.csdn.net/caowenbin 版本:Android 2.3 r1 原文 http://developer.android.com/guide/developing/tools/adb.html Android Debug Bridge ......

阿酷
2011/11/16
10.7K
0
判断Http请求由手机端发起,还是有电脑端发起

某些情形,我们需要判断Http请求是来自手机端还是电脑端,关键是取得User-Agent的信息,进行筛选判断即可。 核心类如下: public static boolean isMobileDevice(String requestHeader){/**...

WangPerry
2014/04/14
5.4K
4

没有更多内容

加载失败,请刷新页面

加载更多

Experts say the weaker pound is drawing investors to the UK tech sector

UK tech companies secured a record £5.5bn in foreign investment in the first seven months of this year, research shows. This was more than the amount invested per capita in th......

wowloop
29分钟前
5
0
Add support for Android 9-patch images in BorderImage

The 9-patch image implementation in Qt Quick Controls 1 is an internal implementation detail of the Android style. It cannot handle .9.png image files out of the box, but takes ......

shzwork
34分钟前
4
0
c/c++日期时间处理函数小结

日期时间处理函数: 日期时间转为字符串 strftime/std::put_time 字符串解析成日期时间 strptime/std::get_time 时间结构转换:time_t->tm localtime:time_t->tm 时间结构转换:tm->time_t ...

chuqq
38分钟前
5
0
Apache Flink 进阶入门(二):Time 深度解析

前言 Flink 的 API 大体上可以划分为三个层次:处于最底层的 ProcessFunction、中间一层的 DataStream API 和最上层的 SQL/Table API,这三层中的每一层都非常依赖于时间属性。时间属性是流处...

大涛学长
39分钟前
4
0
创龙基于Xilinx Artix-7系列FPGA处理器

SOM-TLA7是一款由广州创龙基于Xilinx Artix-7系列FPGA自主研发的核心板,可配套广州创龙Artix-7开发板使用。核心板尺寸仅70mm*50mm,采用沉金无铅工艺的10层板设计,专业的PCB Layout保证信号...

Tronlong创龙
45分钟前
6
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部