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 = super.getParameter(name); if(value==null) return value; //得到请求方式 String method = super.getMethod(); if("get".equalsIgnoreCase(method)){ try { value = new String(value.getBytes("ISO-8859-1"),super.getCharacterEncoding()); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } return value; } }
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 = super.getParameter(name); if(value==null) return "木有"; for(String dword:words){ if(value.contains(dword)){//判断用户输入的内容中包含脏话 value = value.replace(dword, "**"); } } return value; } }
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 = super.getParameter(name); if(value==null) return value; value = htmlFilter(value);//完成标记过滤 return value; } private String htmlFilter(String message) { if (message == null) return (null); char content[] = new char[message.length()]; message.getChars(0, message.length(), content, 0); StringBuffer result = new StringBuffer(content.length + 50); for (int i = 0; i < content.length; i++) { switch (content[i]) { case '<': result.append("<"); break; case '>': result.append(">"); break; case '&': result.append("&"); break; case '"': result.append("""); break; default: result.append(content[i]); } } return (result.toString()); } }
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>
原文地址:http://blog.csdn.net/ankeyuan/article/details/34980233