----------- web.xml 설정 -------------------------------
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>struts.filter.EncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>euc-kr</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
------------- 필터링 소스 ----------------------------------
package struts.filter;
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 EncodingFilter implements Filter {
private String encoding = null;
protected FilterConfig filterConfig = null;
public void destroy() {
this.encoding = null;
this.filterConfig = null;
}
/**
* ServletRequest객체에 web.xml에서 전달된 인코딩을 설정하는 메써드.
*/
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);
System.out.println("필터 기능 사용");
}
/**
* web.xml에서 전달된 인코딩 값을 초기화하는 메써드.
*Filter 객체생성후 바로 호출되는 메소드
*-web.xml의 파라메타 데이터를 읽어온다.
*/
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
/* <filter-name>
encodingFilter
</filter-name>
<filter-class>
struts.util.EncodingFilter
</filter-class>
<init-param>
<param-name>encoSding</param-name>
<param-value>euc-kr</param-value>
</init-param>
</filter>*/
this.encoding = filterConfig.getInitParameter("encoding");
}
public FilterConfig getFilterConfig() {
return filterConfig;
}
public void setFilterConfig(FilterConfig cfg) {
filterConfig = cfg;
}
}