目的:只是想验证一下filter对URI的过滤
流程讲解:浏览器请求URI,所有请求都走过虑器,在过滤器中处理符合某种请求的URI然后显示对应的页面内容
有2个JSP页面:
index.jsp:
This is index page. 111111111111111111111
- 1111111111
- 2222222222
select.jsp:
- 3333333
- 444444444
filter类UrlFilter.java
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest httpServletRequest=(HttpServletRequest) request; HttpServletResponse httpServletResponse=(HttpServletResponse) response; String requestUrl = httpServletRequest.getRequestURI(); String ContextPath =httpServletRequest.getContextPath(); System.out.println(requestUrl+"========"+ContextPath); String ContextPath1=ContextPath+"/"; String uri=requestUrl.replace(ContextPath1, ""); System.out.println("uri==========="+uri); //String info = uri.substring(0, uri.indexOf(".")); if("index".equals(uri)){ httpServletRequest.getRequestDispatcher("index.jsp").forward(httpServletRequest, httpServletResponse); }else if("select".equals(uri)){ httpServletRequest.getRequestDispatcher("select.jsp").forward(httpServletRequest, httpServletResponse); } }
web.xml配置:
urlFilter filter.UrlFilter urlFilter /*
浏览器访问uri:
1、http://localhost:8080/struts/index显示index.jsp页面内容
2、http://localhost:8080/struts/select显示select.jsp页面内容