首页 > 代码库 > 过滤器和包装类设计模式的应用
过滤器和包装类设计模式的应用
1.中文乱码过滤器。get和set中文字体的设置
public class AllCharacterEncodingFilter implements Filter { private FilterConfig filterConfig; public void init(FilterConfig filterConfig) throws ServletException { this.filterConfig = filterConfig; } public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest)req; HttpServletResponse response = (HttpServletResponse)resp; //获取配置的过滤器参数encoding的值:如果没有,给定一个默认值 String encoding = filterConfig.getInitParameter("encoding"); if(encoding==null) encoding = "UTF-8"; //设置POST请求方式的中文请求参数的编码 request.setCharacterEncoding(encoding); //设置响应输出时的编码:字符流和字节流 response.setCharacterEncoding(encoding);//字节流输出时通知客户端的解码码表 response.setContentType("text/html;charset="+encoding);//字节流输出时通知客户端的解码码表;字符流:字符流输出时使用的码表及通知客户端解码用的码表 MyHttpServletRequest mrequest = new MyHttpServletRequest(request); //放行 chain.doFilter(mrequest, response); } public void destroy() { } } //使用包装类设计模式 //HttpServletRequestWrapper内部的代码与连接池原理中那个默认适配器一样 class MyHttpServletRequest extends HttpServletRequestWrapper{ public MyHttpServletRequest(HttpServletRequest request){ super(request); } //只对get请求方式进行改写 public String getParameter(String name) { String value = http://www.mamicode.com/super.getParameter(name);>
2.脏话过滤器public class DirtyWordsFilter implements Filter { public void destroy() { } public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest)req; HttpServletResponse response = (HttpServletResponse)resp; DirtyWordsHttpServletRequest drequest = new DirtyWordsHttpServletRequest(request); chain.doFilter(drequest, response); } public void init(FilterConfig filterConfig) throws ServletException { } } class DirtyWordsHttpServletRequest extends HttpServletRequestWrapper{ private String words[] = {"傻B","畜生","禽兽"}; public DirtyWordsHttpServletRequest(HttpServletRequest request){ super(request); } @Override public String getParameter(String name) { String value = http://www.mamicode.com/super.getParameter(name);>3. HTML特殊标记过滤器
//HTML标记过滤器:< < public class HtmlFilter implements Filter { public void destroy() { } public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest)req; HttpServletResponse response = (HttpServletResponse)resp; HtmlHttpServletRequest hrequest = new HtmlHttpServletRequest(request); chain.doFilter(hrequest, response); } public void init(FilterConfig filterConfig) throws ServletException { } } class HtmlHttpServletRequest extends HttpServletRequestWrapper{ public HtmlHttpServletRequest(HttpServletRequest request){ super(request); } @Override public String getParameter(String name) { String value = http://www.mamicode.com/super.getParameter(name);>
web.xml中配置
<filter> <filter-name>AllCharacterEncodingFilter</filter-name> <filter-class>cn.itcast.filter.AllCharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>GBK</param-value> </init-param> </filter> <filter-mapping> <filter-name>AllCharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter> <filter-name>DirtyWordsFilter</filter-name> <filter-class>cn.itcast.filter.DirtyWordsFilter</filter-class> </filter> <filter-mapping> <filter-name>DirtyWordsFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter> <filter-name>HtmlFilter</filter-name> <filter-class>cn.itcast.filter.HtmlFilter</filter-class> </filter> <filter-mapping> <filter-name>HtmlFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter>
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。