JSP乱码的一个解决方法

附转:JAVA常见中文问题的解决方法

1.在jsp页面首部加上 <% @page contentType = ”text/html; charset = GB2312“ % >
    在servlet中使用httpServlerResponse.setContentTpye(“text/html;
charset = GB2312”); 可以避免一些中文问题

    2.使用JDBC连接mysql数据库时,连接字符串写成如下形式可以避免一些中文问题:
    jdbc : //mysql://hostname:port/DBname?user=username&password=pwd&
    useUnicode = true & characterEncoding = iso - 8859 - 1
    如果是以数据源的方式连接数据库在配置文件中使用:
    <parameter >
    <name > url </ name >
    <value > jdbc : mysql : //hostname:port/DBname? &useUnicode=true&characterEncoding=iso-8859-1</value>
    </ parameter >
    注意要使用 & 替换 & 符号,否则XML文件在解析的时候会出错。

    3.从数据库读出的数据有可能是乱码,遇到这种问题可以用如下方法解决:
String desc = rs.getString(“desc”);
desc = new String(desc.getBytes(“ISO - 8859 - 1”),”GB2312”);

4.某个页面提交中文内容给Servlet,Servlet要对提交的内容进行转码工作才能正确接收数据,
    通常我们是在servlet中增加以下代码来解决问题。
httpServlerRequest.setCharacterEncoding(“GB2312”);

5.在struts中,对资源文件进行转码,使用JDK字带的转码工具:
    >
    native2ascii - encoding BG2312 Myresource.properties Myresource_zh.properties

    6.在struts中扩展org.apache.struts.action.RequestProcessor类,
并覆写其中的processPreprocess() 方法 :
package com.mypro.action;

public class MyProRequestProcessor
    extends RequestProcessor {
  protected boolean processPreprocess(HttpServletRequest request,
                                      HttpServletResponse response) {
    try {
      request.setCharacterEncoding(“GB2312”);
//other code
    }
    catch (Exception e) {}

    return true;
  }
}

写完上面代码别忘了修改struts - config.xml:
    <controller processorClass = ”com.mypro.action.MyProRequestProcessor”/>

7.用filter实现(推荐)
package com.kefeiannan;

import java.io.IOException;
import javax.servlet.*;

public class SetCharacterEncodingFilter
    implements Filter {

  protected String encoding = null;
  protected FilterConfig filterConfig = null;
  protected boolean ignore = true;

  public void destroy() {
    this.encoding = null;
    this.filterConfig = null;
  }

  public void doFilter(ServletRequest request, ServletResponse response,
                       FilterChain chain) throws IOException, ServletException {
    if (ignore || (request.getCharacterEncoding() == null)) {
      String encoding = selectEncoding(request);
      if (encoding != null)
        request.setCharacterEncoding(encoding);
    }
    chain.doFilter(request, response);
  }

  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);
  }
}

配置你站点下的web.xml, 在 <web - app > 后面加上
    <filter >
    <filter - name > Set Character Encoding </ filter - name >
    <filter - class > com.kefeiannan.SetCharacterEncodingFilter </ filter - class >
    <init - param >
    <param - name > encoding </ param - name >
    <param - value > UTF - 8 </ param - value >
    </ init - param >
    </ filter >

    <filter - mapping >
    <filter - name > Set Character Encoding </filter-name>

[Edit on 2005-4-5 20:40:13 By flashman]

Tomcat 的server.xml中在
 <Connector
port="8080"               maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
               enableLookups="false" redirectPort="8443" acceptCount="100"
               debug="0" connectionTimeout="20000"
               disableUploadTimeout="true" URIEncoding="UTF-8" useBodyEncodingForURI="true" />
添加加亮那段

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值