1 package jodd.servlet.filters; 2 3 import java.io.IOException; 4 5 import javax.servlet.Filter; 6 import javax.servlet.FilterChain; 7 import javax.servlet.FilterConfig; 8 import javax.servlet.ServletException; 9 import javax.servlet.ServletRequest; 10 import javax.servlet.ServletResponse; 11 12 13 38 39 public class SetCharacterEncoding implements Filter { 40 41 45 protected String encoding = null; 46 47 51 protected FilterConfig filterConfig = null; 52 53 56 protected boolean ignore = true; 57 58 59 62 public void destroy() { 63 this.encoding = null; 64 this.filterConfig = null; 65 } 66 67 68 81 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { 82 83 if (ignore || (request.getCharacterEncoding() == null)) { 85 String encoding = selectEncoding(request); 86 if (encoding != null) { 87 request.setCharacterEncoding(encoding); 88 } 89 } 90 chain.doFilter(request, response); 91 } 92 93 98 public void init(FilterConfig filterConfig) throws ServletException { 99 100 this.filterConfig = filterConfig; 101 this.encoding = filterConfig.getInitParameter("encoding"); 102 String value = filterConfig.getInitParameter("ignore"); 103 if (value == null) { 104 this.ignore = true; 105 } else if (value.equalsIgnoreCase("true")) { 106 this.ignore = true; 107 } else { 108 this.ignore = false; 109 } 110 } 111 112 124 protected String selectEncoding(ServletRequest request) { 125 return(this.encoding); 126 } 127 } 128 129 | Popular Tags |