过滤器 电脑端 手机端 判断

编程入门 行业动态 更新时间:2024-10-22 16:36:39

<a href=https://www.elefans.com/category/jswz/34/1771166.html style=过滤器 电脑端 手机端 判断"/>

过滤器 电脑端 手机端 判断

2019独角兽企业重金招聘Python工程师标准>>>

package cn.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;}@Overrideprotected 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.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文件,从而实现地址不变,内容改变的效果。 这是我在做网站的一个小小的思路,希望也能帮助到有需要的朋友,如果你有更好的思路或建议,也欢迎评论中提出。

转载于:

更多推荐

过滤器 电脑端 手机端 判断

本文发布于:2024-02-06 03:51:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1746135.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:过滤器   电脑   手机

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!