struts解决中文乱码

来源:互联网 发布:sql update嵌套select 编辑:程序博客网 时间:2024/04/30 01:31
jsp,struts处理中文乱码问题
2006年11月20日 星期一 18:03
一、关于jsp处理中文乱码的问题
   在web.xml中设置正确的编码类型,使网页在发送表单时不会出现中文乱码。
/*
* 解决中文乱码问题,该类必须在web.xml设置,指定编码类型
*/
package com.login.app;
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;
     /**
      * Take this filter out of service.
      */
     public void destroy() {
         this.encoding = null;
         this.filterConfig = null;
     }
     public void doFilter(ServletRequest request, ServletResponse response,
                          FilterChain chain)
         throws IOException, ServletException {
         // Conditionally select and set the character encoding to be used
         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);
     }
     /**
      * Place this filter into service.
      *
      * @param filterConfig The filter configuration object
      */
     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);
     }
}
参照tomcat自带的filter类,自己写一个filter的子类,用来设置编码。并在web.xml上指定就可以了:
<!--filer in Chinese-->
<filter>
      <filter-name>Set Character Encoding</filter-name>
      <filter-class>com.login.app.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>*.jsp</url-pattern>
</filter-mapping>
把以上代码放在web.xml中<display-name>Encoding Application</display-name>的后面.
二、在struts中处理中文乱码问题:
要解决在提交表单时,中文不会出显乱码,只须继承RequestProcessor类,重写processPreprocess这个方法,则可以解决。
package com.login.app;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.RequestProcessor;
public class MyRequestProcessor extends RequestProcessor{
public MyRequestProcessor(){}
protected boolean processPreprocess(HttpServletRequest request,
    HttpServletResponse response){
   try{
    request.setCharacterEncoding("UTF-8");//
在此设置字符集
   }
   catch(Exception ex){
    System.out.println("字符集设置失败");
   }
   return true;
}
}
并在struts-config.xml设置相应的<controller>,如下所示:
<controller processorClass="com.login.app.MyRequestProcessor"/>
三、解决插入和读取数据库数据时出现的中文乱码
插入数据到数据库时出现乱码:
这种乱码会使你插入数据库的中文变成乱码,或者读出显示时也是乱码,解决方法如下:
在数据库连接字符串中加入编码字符集
String url=”jdbc:mysql://localhost/test?user=root&password=root&useUnicode=true&characterEncoding=UTF-8"
把characterEncoding的值设置成你期望的编码。
无论你是用哪种方式连接数据库,只要在url后面加上useUnicode=true&characterEncoding=UTF-8"就可以解决中文乱码问题,当然你提交的数据编码也必须是UTF-8的,总而言之,在构造一个网站,整个网站必须采用统一的编码。支持中文的编码有GBK,gb2312,UTF-8等。
例如在struts中利用数据源连接数据库,必须在struts-config.xml做如下配置:
<data-sources>
<!-- 设置数据源标识 -->
<data-source key="mysqlDB1" type="org.apache.tomcat.dbcp.dbcp.BasicDataSource">
<!-- 设置数据库驱动对应类名 -->
<set-property property="driverClassName" value="org.gjt.mm.mysql.Driver"/>
<!-- 设置待连接数据源URL -->
<set-property property="url" value="jdbc:mysql://localhost/firststruts?useUnicode=true&charact erEncoding=UTF-8"/>
<!-- 设置同时打开连接的最大数目 -->
<set-property property="maxActive" value="5"/>
<!-- 设置登录数据库服务器用户名及密码 -->
<set-property property="username" value="root"/>
<set-property property="password" value=""/>
<!-- 设置事务处理中的自动提交 -->
<set-property property="autoCommit" value
转自 http://hi.baidu.com/javasea/blog/item/732ef21b8ea62bd7ad6e75e0.html