반응형

EncodingFilter.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class CommonEncodingFilter implements Filter {
    private String encoding = null;
    protected FilterConfig filterConfig = null;
    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig = filterConfig;
        this.encoding = filterConfig.getInitParameter("encoding");
    }
 
   public void destroy() {
        this.encoding = null;
        this.filterConfig = null;
   }
 
   public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) 
   throws IOException, ServletException {
        if (request.getCharacterEncoding() == null) {
            if (encoding != null) {
                request.setCharacterEncoding(encoding);
            }
        }
        chain.doFilter(request, response);
   }
 
    public FilterConfig getFilterConfig() {
        return filterConfig;
    }
 
    public void setFilterConfig(FilterConfig cfg) {
        filterConfig = cfg;
    }
}
cs

web.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
<filter>
    <filter-name>Encoding Filter</filter-name>
    <filter-class>com.exam.common.CommonEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>Encoding Filter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
 
cs


반응형

'IT > 언어' 카테고리의 다른 글

ByteUtil.java  (0) 2014.05.29
RSA 암호화  (0) 2014.05.29
정규 표현식 예제  (0) 2014.05.21
Servlet 에러 페이지 처리  (0) 2014.05.21
Jquery validation  (0) 2014.05.21

+ Recent posts