--web.xml-------------------------------------------------------------------------
<web-app>
<filter>
<filter-name>encoder</filter-name>
<filter-class>utils.hangle.EncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>euc-kr</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoder</filter-name>
<servlet-name>action</servlet-name>
</filter-mapping>
</web-app>
--EncodingFilter.java---------------------------------------------------------------
package utils.hangle;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* EncodingFilter.java
* @web:filter
* name = "encoder"
* @web:filter-init-param
* name = "encoding"
* value = "euc-kr"
* @web:filter-mapping
* url-pattern = "*.jsp"
*/
public class EncodingFilter implements Filter {
private static Log log = LogFactory.getLog(EncodingFilter.class);
private boolean enabled = true;
private String encoding;
/**
* @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
*/
public void init(final FilterConfig config) throws ServletException {
this.encoding = config.getInitParameter("encoding");
if (log.isInfoEnabled()) {
log.info("Encoding filter has been initialized : " + encoding);
}
}
/**
* @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest,
* javax.servlet.ServletResponse, javax.servlet.FilterChain)
*/
public void doFilter(
final ServletRequest request,
final ServletResponse response,
final FilterChain chain) throws IOException, ServletException {
if (enabled && (encoding != null)) {
try {
request.setCharacterEncoding(encoding);
} catch (UnsupportedEncodingException e) {
if (log.isWarnEnabled()) {
log.warn("Encoding filter has been disabled.", e);
}
this.enabled = false;
}
}
chain.doFilter(request, response);
}
/**
* @see javax.servlet.Filter#destroy()
*/
public void destroy() {
if (log.isInfoEnabled()) {
log.info("Encoding filter has been destoryed.");
}
}
}
댓글 없음:
댓글 쓰기