1 63 64 65 package filters; 66 67 68 import java.io.IOException ; 69 import javax.servlet.Filter ; 70 import javax.servlet.FilterChain ; 71 import javax.servlet.FilterConfig ; 72 import javax.servlet.ServletException ; 73 import javax.servlet.ServletRequest ; 74 import javax.servlet.ServletResponse ; 75 import javax.servlet.UnavailableException ; 76 77 78 106 107 public class SetCharacterEncodingFilter implements Filter { 108 109 110 112 113 117 protected String encoding = null; 118 119 120 124 protected FilterConfig filterConfig = null; 125 126 127 130 protected boolean ignore = true; 131 132 133 135 136 139 public void destroy() { 140 141 this.encoding = null; 142 this.filterConfig = null; 143 144 } 145 146 147 158 public void doFilter(ServletRequest request, ServletResponse response, 159 FilterChain chain) 160 throws IOException , ServletException { 161 162 if (ignore || (request.getCharacterEncoding() == null)) { 164 String encoding = selectEncoding(request); 165 if (encoding != null) 166 request.setCharacterEncoding(encoding); 167 } 168 169 chain.doFilter(request, response); 171 172 } 173 174 175 180 public void init(FilterConfig filterConfig) throws ServletException { 181 182 this.filterConfig = filterConfig; 183 this.encoding = filterConfig.getInitParameter("encoding"); 184 String value = filterConfig.getInitParameter("ignore"); 185 if (value == null) 186 this.ignore = true; 187 else if (value.equalsIgnoreCase("true")) 188 this.ignore = true; 189 else if (value.equalsIgnoreCase("yes")) 190 this.ignore = true; 191 else 192 this.ignore = false; 193 194 } 195 196 197 199 200 212 protected String selectEncoding(ServletRequest request) { 213 214 return (this.encoding); 215 216 } 217 218 219 } 220 | Popular Tags |