1 63 64 65 package net.myvietnam.mvncore.servlet.filter; 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 76 77 105 106 public class SetCharacterEncodingFilter implements Filter { 107 108 109 111 112 116 protected String encoding = null; 117 118 119 123 protected FilterConfig filterConfig = null; 124 125 126 129 protected boolean ignore = true; 130 131 132 134 135 138 public void destroy() { 139 140 this.encoding = null; 141 this.filterConfig = null; 142 143 } 144 145 146 157 public void doFilter(ServletRequest request, ServletResponse response, 158 FilterChain chain) 159 throws IOException , ServletException { 160 161 if (ignore || (request.getCharacterEncoding() == null)) { 163 String encoding = selectEncoding(request); 164 if (encoding != null) { 165 request.setCharacterEncoding(encoding); 166 } 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 186 if (this.encoding != null && this.encoding.equals("")) { 188 this.encoding = null; 190 } 191 193 if (value == null) 194 this.ignore = true; 195 196 else if (value.equals("")) 198 this.ignore = true; 199 201 else if (value.equalsIgnoreCase("true")) 202 this.ignore = true; 203 else if (value.equalsIgnoreCase("yes")) 204 this.ignore = true; 205 else 206 this.ignore = false; 207 208 } 209 210 211 213 214 226 protected String selectEncoding(ServletRequest request) { 227 228 return (this.encoding); 229 230 } 231 232 233 } 234 | Popular Tags |