1 package com.jdon.util; 2 3 import javax.servlet.*; 4 import java.io.IOException ; 5 6 35 36 public class SetCharacterEncodingFilter 37 implements Filter { 38 39 41 45 protected String encoding = null; 46 47 51 protected FilterConfig filterConfig = null; 52 53 56 protected boolean ignore = true; 57 58 60 63 public void destroy() { 64 65 this.encoding = null; 66 this.filterConfig = null; 67 68 } 69 70 81 public void doFilter(ServletRequest request, ServletResponse response, 82 FilterChain chain) throws IOException , ServletException { 83 84 if (ignore || (request.getCharacterEncoding() == null)) { 86 String encoding = selectEncoding(request); 87 if (encoding != null) 88 request.setCharacterEncoding(encoding); 89 } 90 91 chain.doFilter(request, response); 93 94 } 95 96 101 public void init(FilterConfig filterConfig) throws ServletException { 102 103 this.filterConfig = filterConfig; 104 this.encoding = filterConfig.getInitParameter("encoding"); 105 PropsUtil.ENCODING = this.encoding; 106 String value = filterConfig.getInitParameter("ignore"); 107 if (value == null) 108 this.ignore = true; 109 else if (value.equalsIgnoreCase("true")) 110 this.ignore = true; 111 else if (value.equalsIgnoreCase("yes")) 112 this.ignore = true; 113 else 114 this.ignore = false; 115 116 } 117 118 120 132 protected String selectEncoding(ServletRequest request) { 133 134 return (this.encoding); 135 136 } 137 138 } 139 | Popular Tags |