jsp,struts处理中文乱码问题

一、关于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="true"/>

</data-source>

</data-sources>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值