java中的乱码解决方案

java后台转换编码
str= new String(str.getBytes("iso8859-1"),"UTF-8");
str= new String(str.getBytes("iso8859-1"),"GBK");
 
servlet中设置编码
response.setContentType("text/html; charset=GBK");


JSP中调整乱码
<%@ page language="java" contentType="text/html;charset=GBK" pageEncoding="GBK" %>


服务器 tomcat中设计编码
更改 Tomcat\conf\server.xml,指定浏览器的编码格式为“简体中文”: 
<Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
改成
<Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" URIEncoding="UTF-8"/>
 

web.xml中添加过滤器

<filter>  
    <filter-name>CharacterEncodingFilter</filter-name>  
    <filter-class>com.worthtech.app.filter.CharacterEncodingFilter</filter-class>  
    <init-param>  
      <param-name>encoding</param-name>  
      <param-value>UTF-8</param-value>  
    </init-param>  
    <init-param>  
      <param-name>ignore</param-name>  
      <param-value>true</param-value>  
    </init-param>  
  </filter>  
  <filter-mapping>  
    <filter-name>CharacterEncodingFilter</filter-name>  
    <url-pattern>/*</url-pattern>  
  </filter-mapping> 

 

 

 

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 CharacterEncodingFilter implements Filter {   
  
    private String encoding=null;   
    private boolean ignore=true;   
       
    public void destroy() {   
        // TODO Auto-generated method stub   
        encoding=null;   
    }   
  
    /**   
       * 过滤请求,用户提交请求时起作用   
     */     
    public void doFilter(ServletRequest request, ServletResponse response,   
            FilterChain chain) throws IOException, ServletException {   
        // TODO Auto-generated method stub   
        if(ignore||request.getCharacterEncoding()==null){   
            String encoding=getEncoding();   
            if(encoding!=null){   
                request.setCharacterEncoding(encoding);   
            }   
        }   
//      System.out.println("encoding===="+encoding);   
        response.setContentType("text/html;charset="+encoding);   
//      do something here   
        //当前工作全部完成后,将请求放开给过滤器链的下一filter    
        chain.doFilter(request, response);   
    }   
  
    public void init(FilterConfig filterConfig) throws ServletException {   
        // TODO Auto-generated method stub   
        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 getEncoding(){   
        return this.encoding;   
    }   
}

 

 

 

 

你可能感兴趣的:(java,tomcat,jsp)