2012년 3월 14일 수요일

[tomcat] 한글 깨짐 현상

1. Get방식 해결 방안 - URIEncoding="euc-kr"추가


server.xml
---------------------------------------------------------------------------
<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" URIEncoding="euc-kr" />


<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" URIEncoding="euc-kr"/>
--------------------------------------------------------------------------- 





2. Post방식 해결 방안 - servlet filter 사용하기


파일명 : SetCharacterEncodingFilter.java
--------------------------------------------------------------------------- 

package 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 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);
}
}
--------------------------------------------------------------------------- 


파일명 : web.xml(war로 묶일 web.xml, 톰캣 web.xml이 아니다)

--------------------------------------------------------------------------- 

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>recruit</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>filter.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>


</web-app>
--------------------------------------------------------------------------- 


댓글 없음:

댓글 쓰기