关于过滤器的一点小问题
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | package filter; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class DeviceAdapterFilter implements Filter{ @Override public void destroy() { // TODO Auto-generated method stub } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // TODO Auto-generated method stub HttpServletRequest req = (HttpServletRequest)request; HttpServletResponse res = (HttpServletResponse)response; String uri =req.getRequestURI(); System.out.println( "uri: " + uri); if (uri.startsWith( "/desktop" ) || uri.startsWith( "/mobile" )) { chain.doFilter(request, response); } else { String header = req.getHeader( "user-agent" ).toLowerCase(); String targetURI = "" ; if (header.indexOf( "android" ) != - 1 || header.indexOf( "iphone" ) != - 1 ) { targetURI = "/mobile" + uri; System.out.println( "target uri : " + targetURI); res.sendRedirect(targetURI); } else { targetURI = "/desktop" + uri; System.out.println( "target uri : " + targetURI); res.sendRedirect(targetURI); } } } @Override public void init(FilterConfig arg0) throws ServletException { // TODO Auto-generated method stub } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <? xml version = "1.0" encoding = "UTF-8" ?> < web-app xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns = "http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation = "http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id = "WebApp_ID" version = "3.1" > < display-name >filter-chain</ display-name > < welcome-file-list > < welcome-file >index.html</ welcome-file > < welcome-file >index.htm</ welcome-file > < welcome-file >index.jsp</ welcome-file > < welcome-file >default.html</ welcome-file > < welcome-file >default.htm</ welcome-file > < welcome-file >default.jsp</ welcome-file > </ welcome-file-list > < filter > < filter-name >device-adapter</ filter-name > < filter-class >filter.DeviceAdapterFilter</ filter-class > </ filter > < filter-mapping > < filter-name >device-adapter</ filter-name > < url-pattern >*.html</ url-pattern > </ filter-mapping > </ web-app > |
1 2 3 4 5 6 7 8 9 10 11 | <!DOCTYPE html> < html > < head > < meta charset = "utf-8 " > < title >Insert title here</ title > </ head > < body > < img alt = "" src = "/images/mobile.jpg" style = "width:100%" > </ body > </ html > |
1 2 3 4 5 6 7 8 9 10 11 | <!DOCTYPE html> < html > < head > < meta charset = "utf-8" > < title >Insert title here</ title > </ head > < body > < img alt = "" src = "/images/desktop.jpg" > </ body > </ html > |
这是我复现老师的代码,我发现如果在页面里面输入http://localhost/mobile/ 那么过滤器是可以捕捉到uri的
uri: /mobile/, 但是假如我输入 http://localhost/mobile/ 那么过滤器将不会捕捉到任何东西? 但是页面可以正常跳转,问下这个是什么原因呢?
源自:过滤器
3-1 多端设备自动适配
21
收起
正在回答
2回答
同学你好,测试同学的代码并没有出现同学所说的问题。
这里猜测如果是访问 http://localhost/mobile/ ,会访问某个html页面,配置的过滤器对html进行拦截,跳转到过滤器会输出uri。
如果访问路径是 http://localhost/mobile ,会访问某个servlet,不访问html页面,就不会被拦截。
祝:学习愉快~
java工程师2020版
- 参与学习 人
- 提交作业 9403 份
- 解答问题 16556 个
综合就业常年第一,编程排行常年霸榜,无需脱产即可学习,北上广深月薪过万 无论你是未就业的学生还是想转行的在职人员,不需要基础,只要你有梦想,想高薪
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧