코드가 깨지지 않도록 J2EE 프로젝트는 UTF-8 인코딩 설정 방법을 통일적으로 사용합니다(회전)

6492 단어
1. 데이터베이스를 UTF-8 형식으로 만듭니다. 2.항목 오른쪽 버튼 속성은 UTF-8 형식입니다. 3.모든 페이지가 UTF-8 4.로 표시됩니다.JDBC URL: UTF-8 jdbc:mysql://localhost:3306/company?useUnicode=true&characterEncoding=utf-8 5.데이터베이스 드라이브 UTF-8 형식 6. 선택Tomcat 인코딩을 UTF-8로 서버에 변경합니다.xml에 URIEncoding 추가 = "UTF-8"
Xml 코드
  •   
  • <Connector port="8080" maxHttpHeaderSize="8192" maxThreads="150" minSpareThreads="25" maxSpareThreads="75" enableLookups="false" redirectPort="8443" acceptCount="100" connectionTimeout="20000" disableUploadTimeout="true" URIEncoding="UTF-8" />
    

    7. 인코딩 필터 SetCharacterEncodingFilter.java 주의: 가방 이름에 따라 어떤 가방을 사용하는지 확인
    Java 코드
  • 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 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;   
  •   
  •     }   
  •   
  •     public void doFilter(ServletRequest request, ServletResponse response,   
  •             FilterChain chain) throws IOException, ServletException {   
  •   
  • //TODO 자동 생성 메소드 스텁
  •   
  •         if (ignore || (request.getCharacterEncoding() == null)) {   
  •   
  •             String encoding = selectEncoding(request);   
  •   
  •             if (encoding != null)   
  •   
  •                 request.setCharacterEncoding(encoding);   
  •   
  •         }   
  •   
  •         chain.doFilter(request, response);   
  •   
  •     }   
  •   
  •     public void destroy() {   
  •   
  • //TODO 자동 생성 메소드 스텁
  •   
  •         this.encoding = null;   
  •   
  •         this.filterConfig = null;   
  •   
  •     }   
  •   
  •     protected String selectEncoding(ServletRequest request) {   
  •   
  •         return (this.encoding);   
  •   
  •     }   
  •   
  • }  
  • 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 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;
    }
    public void doFilter(ServletRequest request, ServletResponse response,
    FilterChain chain) throws IOException, ServletException {
    // TODO         
    if (ignore || (request.getCharacterEncoding() == null)) {
    String encoding = selectEncoding(request);
    if (encoding != null)
    request.setCharacterEncoding(encoding);
    }
    chain.doFilter(request, response);
    }
    public void destroy() {
    // TODO         
    this.encoding = null;
    this.filterConfig = null;
    }
    protected String selectEncoding(ServletRequest request) {
    return (this.encoding);
    }
    }
    

    웹에서.xml 맵 설정
    Xml 코드
  •   
  •   Set Character Encoding  
  •   com.yourcompany.util.SetCharacterEncodingFilter  
  •     
  •     encoding  
  •     UTF-8  
  •     
  •   
  •   
  •   Set Character Encoding  
  •   /*  
  •  
  • 좋은 웹페이지 즐겨찾기