2010/09/10 16:12
JSP 한글문제 정리 Hello World/Java/JSP/EJB2010/09/10 16:12
JSP
<% response.setCharacterEncoding("EUC-KR");%>
위 선언으로 POST 방식으로 넘어오는 값을 EUC-KR로 변경 가능.
하지만 GET 방식은 적용안됨.
GET 방식은 new String(str.getBytes("8859-1"),"EUC-KR") 로 변환해주면 정상출력되나 매번 코드 작성이 번거롭기 때문에 해당 웹서버의 $CATALINA_HOME/conf/server.xml 에서 Connector 구문에 URIEncoding="EUC-KR" 을 추가하는 것으로 해결할 수 있음.
Servlet
GET 방식은 동일.
POST 방식은 response.setCharacterEncoding("EUC-KR"); 을 쓰는 것으도로 가능하지만 아래와 같은 방법으로도 가능.
web.xml 의 web-app 엘리먼트 내에 필터 삽입
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>filters.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>euc-kr</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
filters.SetCharacterEncodingFilter.java 생성
package filters;
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 SetCharacterEncodingFilter implements Filter {
protected String encoding = null;
protected FilterConfig filterConfig = null;
protected boolean ignore = true;
public void destroy() {
this.encoding = null;
this.filterConfig = null;
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
if (ignore || (request.getCharacterEncoding() == null)) {
String encoding = selectEncoding(request);
if (encoding != null)
request.setCharacterEncoding(encoding);
}
chain.doFilter(request, response);
}
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
String value = filterConfig.getInitParameter("ignore");
if (value == null)
this.ignore = true;
else if (value.equalsIgnoreCase("true"))
this.ignore = true;
else if (value.equalsIgnoreCase("yes"))
this.ignore = true;
else
this.ignore = false;
}
protected String selectEncoding(ServletRequest request) {
return (this.encoding);
}
}
// SetCharacterEncodingFilter.java 끝
......
서블릿 방식으로 jsp에서도 가능
