1. filter를 이용하기
(1) 요구사항: Container가 servlet 2.3을 지원해야 한다.
(2) 방법
(a) 아래와 같은 필터 클래스 작성
--------------------------------------------------------------------------------
package net.javajigi.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;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
/**
* ServletRequest객체에 인코딩을 설정하는 Filter클래스.
*
* @web.filter name="Encoding Filter"
* display-name="Encoding Filter"
*
* @web.filter-init-param name="encoding"
* value="EUC-KR"
*
* @web.filter-mapping url-pattern="/*"
*
*/
public class EncodingFilter implements Filter {
private String encoding = null;
* ServletRequest객체에 인코딩을 설정하는 Filter클래스.
*
* @web.filter name="Encoding Filter"
* display-name="Encoding Filter"
*
* @web.filter-init-param name="encoding"
* value="EUC-KR"
*
* @web.filter-mapping url-pattern="/*"
*
*/
public class EncodingFilter implements Filter {
private String encoding = null;
protected FilterConfig filterConfig = null;
public void destroy() {
this.encoding = null;
this.filterConfig = null;
this.filterConfig = null;
}
/**
* ServletRequest객체에 web.xml에서 전달된 인코딩을 설정하는 메써드.
*/
public void doFilter(
ServletRequest request,
ServletResponse response,
FilterChain chain)
throws IOException, ServletException {
* 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);
}
}
if (encoding != null) {
request.setCharacterEncoding(encoding);
}
}
chain.doFilter(request, response);
}
}
/**
* web.xml에서 전달된 인코딩 값을 초기화하는 메써드.
*/
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
}
* web.xml에서 전달된 인코딩 값을 초기화하는 메써드.
*/
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
}
public FilterConfig getFilterConfig() {
return filterConfig;
}
return filterConfig;
}
public void setFilterConfig(FilterConfig cfg) {
filterConfig = cfg;
}
}
filterConfig = cfg;
}
}
--------------------------------------------------------------------------------
(b) web.xml에 아래 설정을 추가
--------------------------------------------------------------------------------
<filter>
<filter-name>Encoding Filter</filter-name>
<display-name>Encoding Filter</display-name>
<filter-class>net.javajigi.filter.EncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>EUC-KR</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Encoding Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-name>Encoding Filter</filter-name>
<display-name>Encoding Filter</display-name>
<filter-class>net.javajigi.filter.EncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>EUC-KR</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Encoding Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
--------------------------------------------------------------------------------
2. request 객체 이용
request 객체를 처리하기 전에 아래와 같이 인코딩을 적용
--------------------------------------------------------------------------------
request.setCharacterEncoding("euc-kr");
--------------------------------------------------------------------------------
댓글 없음:
댓글 쓰기