EB开发乱码处理总结

在开发J2EE系统是,经常遇到jsp,数据库出现乱码问题,其解决方法分如下四个步骤:
1
首先检查jsp页面最上面的pageEncoding,将其设置为:GBK
<%@ page language="java"  pageEncoding="GBK"%>
<%@ page contentType="text/html;charset=GBK"%>

2 我们开发工具用eclipse,检查ecpilpse的默认字体格式,方法是:
Project->Properties->Info
查看Text file encoding是否为GBK ,如果不是将其设置为GBK

3 给工程新建一个包,包名为filters,编写一个类SetCharacterEncodingFilter,次类在包filters里面
SetCharacterEncodingFilter
实现Filter接口 ,该类的代码如下:
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() {
  // TODO Auto-generated method stub
         this.encoding = null ;
         this.filterConfig = 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 = selectEncoding(request);
            if (encoding != null)
                  request.setCharacterEncoding(encoding);
          }
  
       // Pass control on to the next 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 selectEncoding(ServletRequest request) {
  
           return (this.encoding);
  
      }

}
4
配置web.xml ,具体web.xml文件如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="
http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_2.xsd">
  <servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
      <param-name>config</param-name>
      <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <init-param>
      <param-name>debug</param-name>
      <param-value>3</param-value>
    </init-param>
    <init-param>
      <param-name>detail</param-name>
      <param-value>3</param-value>
    </init-param>
    <load-on-startup>0</load-on-startup>
  </servlet>
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>EncodingServlet</servlet-name>
    <servlet-class>EncodingServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>EncodingServlet</servlet-name>
    <url-pattern>/servlet/EncodingServlet</url-pattern>
  </servlet-mapping>

   <taglib>
     <taglib-uri>http://java.sun.com/jstl/core</taglib-uri>
     <taglib-location>/WEB-INF/c.tld</taglib-location>
   </taglib>
  
   <taglib>
     <taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>
     <taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
   </taglib>
  
   <taglib>
     <taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
     <taglib-location>/WEB-INF/struts-html.tld</taglib-location>
   </taglib>
  
   <taglib>
     <taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
     <taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
   </taglib>
  
   <taglib>
     <taglib-uri>/WEB-INF/struts-template.tld</taglib-uri>
     <taglib-location>/WEB-INF/struts-template.tld</taglib-location>
   </taglib>
  
   <taglib>
     <taglib-uri>/WEB-INF/struts-tiles.tld</taglib-uri>
     <taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>
   </taglib>
  
     <filter>
        <filter-name>Set Character Encoding</filter-name>
        <filter-class>filters.SetCharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>GBK</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>Set Character Encoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

 

对于Java由于默认的编码方式是 UNICODE,所以用中文也易出问题,常见的解决是

  String s2 = new String(s1.getBytes(“ISO-8859-1”),”GBK”);

  1utf8解决JSP中文乱码问题

  一般说来在每个页面的开始处,加入:

以下是引用片段:
<%@ page language="java" contentType="text/html; charset=UTF-8" 
pageEncoding="UTF-8"%> 
<% 
request.setCharacterEncoding("UTF-8"); 
%> 

  charset=UTF-8 的作用是指定JSP向客户端输出的编码方式为“UTF-8”

  pageEncoding="UTF-8" 为了让JSP引擎能正确地解码含有中文字符的JSP页面,这在LINUX中很有效

  request.setCharacterEncoding("UTF-8"); 是对请求进行了中文编码

  有时,这样仍不能解决问题,还需要这样处理一下:

  String msg = request.getParameter("message");

  String str=new String(msg.getBytes("ISO-8859-1"),"UTF-8");

  out.println(st);

  2Tomcat 5.5 中文乱码

  1)只要把%TOMCAT安装目录%/ webapps/servlets-examples/WEB-INF/classes/filters/SetCharacterEncodingFilter.class文件拷到你的webapp目录/filters下,如果没有filters目录,就创建一个。

  2)在你的web.xml里加入如下几行:

以下是引用片段:
<filter> 
<filter-name>Set Character Encoding</filter-name> 
<filter-class>filters.SetCharacterEncodingFilter</filter-class> 
<init-param> 
<param-name>encoding</param-name> 
<param-value>GBK</param-value> 
</init-param> 
</filter> 
<filter-mapping> 
<filter-name>Set Character Encoding</filter-name> 
<url-pattern>/*</url-pattern> 
</filter-mapping> 

  3)完成.

  2 get方式的解决办法

  1) 打开tomcatserver.xml文件,找到区块,加入如下一行:

  URIEncoding=”GBK”

  完整的应如下:

  

以下是引用片段:
 <Connector 
port="80" maxThreads="150" minSpareThreads="25" maxSpareThreads="75" 
enableLookups="false" redirectPort="8443" acceptCount="100" 
debug="0" connectionTimeout="20000" 
disableUploadTimeout="true" 
URIEncoding="GBK" 
/> 

<CONNECTOR< p>  2)重启tomcat,一切OK

  3xmlHttpRequest中文问题

  页面jsp用的GBK编码

 

以下是引用片段:
 代码 
<%@ page contentType="text/html; charset=GBK"%> 
  
javascript
部分
  代码
  
function addFracasReport() { 
var url="controler?actionId=0_06_03_01&actionFlag=0010"; 
var urlmsg="&reportId="+fracasReport1.textReportId.value; //
故障报告表编号 
var xmlHttp=Common.createXMLHttpRequest(); 
xmlHttp.onreadystatechange = Common.getReadyStateHandler(xmlHttp, eval("turnAnalyPage")); 
xmlHttp.open("POST",url,true); 
xmlHttp.setRequestHeader( " Content-Type " , " application/x-www-form-urlencoded); 
xmlHttp.send(urlmsg); 

  后台java中获得的reportId是乱码,不知道该怎么转,主要是不知道xmlHttp.send(urlmsg);以后是什么编码?在后面用java来转,试了几种,都没有成功,其中有:

  

以下是引用片段:
代码
  public static String UTF_8ToGBK(String str) {
  try {
  return new String(str.getBytes("UTF-8"), "GBK");
  } catch (Exception ex) {
  return null;
  }
  }
  public static String UTF8ToGBK(String str) {
  try {
  return new String(str.getBytes("UTF-16BE"), "GBK");
  } catch (Exception ex) {
  return null;
  }
  }
  public static String GBK(String str) {
  try {
  return new String(str.getBytes("GBK"),"GBK");
  } catch (Exception ex) {
  return null;
  }
  }
  public static String getStr(String str) {
  try {
  String temp_p = str;
  String temp = new String(temp_p.getBytes("ISO8859_1"), "GBK");
  temp = sqlStrchop(temp);
  return temp;
  } catch (Exception e) {
  return null;
  }
  

 

你可能感兴趣的:(EB开发乱码处理总结)