1.过滤器介绍
2.过滤器开发三要素
3.过滤器的配置和注解
配置:
<filter> <filter-name>FilterA</filter-name> <filter-class>filter.FilterA</filter-class> </filter> <filter-mapping> <filter-name>FilterA</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
注解:
@WebFilter(filterName="CharacterEncodingFilter",urlPatterns="/*", initParams= { @WebInitParam(name="encoding" , value="GBK"), @WebInitParam(name="p1" , value="v1"), @WebInitParam(name="p2" , value="v2") })
4.过滤器选择
5.字符过滤器
1 <filter> 2 <filter-name>CharacterEncodingFilter</filter-name> 3 <filter-class>filter.CharacterEncodingFilter</filter-class> 4 <init-param> 5 <param-name>encoding</param-name> 6 <param-value>UTF-8</param-value> 7 </init-param> 8 <init-param> 9 <param-name>encode</param-name> 10 <param-value>GBK</param-value> 11 </init-param> 12 <init-param> 13 <param-name>p1</param-name> 14 <param-value>v1</param-value> 15 </init-param> 16 <init-param> 17 <param-name>p2</param-name> 18 <param-value>v2</param-value> 19 </init-param> 20 </filter>
1 package filter; 2 3 import java.io.IOException; 4 5 import javax.servlet.Filter; 6 import javax.servlet.FilterChain; 7 import javax.servlet.FilterConfig; 8 import javax.servlet.ServletException; 9 import javax.servlet.ServletRequest; 10 import javax.servlet.ServletResponse; 11 import javax.servlet.annotation.WebFilter; 12 import javax.servlet.http.HttpServlet; 13 import javax.servlet.http.HttpServletRequest; 14 import javax.servlet.http.HttpServletResponse; 15 //@WebFilter(filterName="CharacterEncodingFilter",urlPatterns="/*", 16 //initParams= { 17 // @WebInitParam(name="encoding" , value="GBK"), 18 // @WebInitParam(name="p1" , value="v1"), 19 // @WebInitParam(name="p2" , value="v2") 20 //}) 21 public class CharacterEncodingFilter implements Filter { 22 private String encoding; 23 24 public void init(FilterConfig fConfig) throws ServletException { 25 // TODO Auto-generated method stub 26 encoding=fConfig.getInitParameter("encoding"); 27 System.out.println("encoding:"+encoding); 28 } 29 30 31 public void destroy() { 32 // TODO Auto-generated method stub 33 34 } 35 36 @Override 37 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) 38 throws IOException, ServletException { 39 // TODO Auto-generated method stub 40 HttpServletRequest req=(HttpServletRequest) request; 41 req.setCharacterEncoding(encoding); 42 HttpServletResponse res=(HttpServletResponse) response; 43 res.setContentType("text/html;charset="+encoding); 44 45 chain.doFilter(request, response); 46 } 47 48 49 }
6.url-pattern设置过滤范围、ServletRequest接口
7.映射问题
8.注意事项
9.小练习
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 init(FilterConfig filterConfig) throws ServletException { // 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; /* /index.html PC: /desktop/index.html MOBILE: /mobile/index.html /test.html PC: /desktop/test.html MOBILE: /mobile/test.html */ String uri = req.getRequestURI(); System.out.println("URI:" + uri); if(uri.startsWith("/desktop") || uri.startsWith("/mobile")) { chain.doFilter(request, response); }else { String userAgent = req.getHeader("user-agent").toLowerCase(); String targetURI=""; if(userAgent.indexOf("android")!=-1 || userAgent.indexOf("iphone") != -1) { targetURI = "/mobile" + uri; System.out.println("移动端设备正在访问,重新跳转URI:" + targetURI); res.sendRedirect(targetURI); }else { targetURI = "/desktop" + uri; System.out.println("PC端设备正在访问,重新跳转URI:" + targetURI); res.sendRedirect(targetURI); } } } @Override public void destroy() { // TODO Auto-generated method stub } }