拦截器public class EmojiFilter implements Filter { private FilterConfig filterConfig; public void init(FilterConfig filterConfig) throws ServletException { System.out.println("Filter initialized"); this.filterConfig = filterConfig; } public void destroy() { System.out.println("Filter destroyed"); this.filterConfig = null; } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { chain.doFilter(new MyRequestWrapper((HttpServletRequest) request), response); }}
web.xml配置EmojiFilter com.qtkj.base.EmojiFilter encoding UTF-8 EmojiFilter /*
1 实际过滤方法 2 public class MyRequestWrapper extends HttpServletRequestWrapper { 3 public MyRequestWrapper(HttpServletRequest servletRequest) { 4 super(servletRequest); 5 } 6 7 public String[] getParameterValues(String parameter) { 8 String[] results = super.getParameterValues(parameter); 9 if (results == null)10 return null; 11 12 int count = results.length;13 String[] trimResults = new String[count];14 for (int i = 0; i < count; i++) { 15 trimResults[i] = com.qtkj.weixin.utils.EmojiFilter.filterEmoji(results[i].trim());16 trimResults[i]=trimResults[i].replace(" ", "");17 } 18 System.out.println("==========="+trimResults+"----------");19 return trimResults;20 } 21 }22 结合emoji处理方法中的方法