이야기앱 세상

post 방식으로 데이터 전송시 한글 깨짐 해결 본문

IT/JSP

post 방식으로 데이터 전송시 한글 깨짐 해결

storya 2016. 3. 22. 11:16

1. jsp 페이지에서 인코딩

 

request.setCharacterEncoding("euc-kr");

 

2. filter 이용법

 

2-1 Filter class 작성 ------------------------

 

package en.filter;

import java.io.IOException;
import javax.servlet.*;


public class EncodingUtil implements Filter {


 private String encoding = null;

 protected FilterConfig filterConfig = null;

 

 public void destroy() {

  this.encoding = null;
  this.filterConfig = null;

 }

 

// web.xml에서 전달된 encoding 값을 초기화

public void init(FilterConfig filterConfig) throws ServletException {
  this.filterConfig = filterConfig;
  this.encoding = filterConfig.getInitParameter("encoding");
 }

 public FilterConfig getFilterConfig() {
  return filterConfig;
 }

 public void setFilterConfig(FilterConfig cfg) {
  filterConfig = cfg;
 }

 

// init 메소드에서 초기화한 encoding 값을 request 객체에 셋팅

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

 

2-2 web.xml 작성 ------------------------

 

  <filter>
      <filter-name>Encoding Filter</filter-name>
      <filter-class>en.filter.EncodingUtil</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>

반응형
Comments