再论Tomcat中的编码问题

     Tomcat作为一个轻量的J2EE Web容器,使用非常广泛...上文写了使用中文编码时,出现问题的解决方案。可以解决使用GBK编码时的一些问题。

     使用对于以后的应用来说,使用UTF-8是一个比较好的选择。那么如果,原来用GBK方案,后来想转化为UTF-8方案,如果快速的进行切换咧?本文通过一个简单的例子, 说明一种快速切换的可能性。

     对于WEB容器来说,出现乱码问题的环节有两个,通过get方法传递的数据和通过post方法传递的数据。一般的解决方案里面,二者没有很好的区分,而随着Tomcat配置的不同,就可能造成有的服务器上是正常的,有的服务器上乱码的情况。

     目前,我基于Tomcat 5.5.12进行相关的测试和配置。对于get和post方法,我编写了两个函数来获取传递过来的数据,函数如下:

//对于没有进行任何设置的Tomcat,配置如下
 static final String getEncode="ISO-8859-1";
 static final String postEncode="ISO-8859-1";
 static final String mainEncode="UTF-8";

 //如果配置为UTF-8,那么就不用管了..
  String getQueryString(javax.servlet.http.HttpServletRequest req, String paramName) { 
  String param = req.getParameter(paramName);
    if (param == null) {
      return "";
    } else {
//    return param;    
   return transEncode(param,getEncode,mainEncode);
  }
  }

 //从Reuqest_Post获得数据,默认串型
  String getParam(javax.servlet.http.HttpServletRequest req, String paramName) {
  String param = req.getParameter(paramName);
    if (param == null) {
      return "";
    } else {
//    return param;
   return transEncode(param,postEncode,mainEncode);
  }
  }

 String transEncode(String vValue,String srcEncode,String targetEncode){
  try {
   return new String(vValue.getBytes(srcEncode), targetEncode);
  } catch (Exception e) {
   return vValue; 
  } 
  }
那么对于Tomcat 5.5,我们可以通过在server.xml 设置 URLEncode="UTF-8" 修改默认getEncode,就是get模式下默认的编码格式,如果改为UTF-8,那么就可以不用进行转换工作

同样的,对于Post方法传递的数据,可以通过增加编码过滤器的方式,指定其编码为UTF-8.代码清单如下:
Web.xml中增加
<filter>
    <filter-name>Set Character Encoding</filter-name>
    <filter-class>com.cecp.filter.SetCharacterEncodingFilter</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>Set Character Encoding</filter-name>
      <url-pattern>/*</url-pattern>
</filter-mapping>
过滤器的代码:
package com.cecp.filter;

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

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 {

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

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

 }

}

那么,到这一步,对于使用UTF-8编码的应用,可以直接在应用中使用所有的数据,不用作任何地编码转换。而对于服务器未作任何配置的应用,通过修改getEncode,postEncode,mainEncode,三个数据,就可以方便的使用getParam和getQueryString获得传递过来的数据.

当然,如果通过配置,使getEncode,postEncode,mainEncode的编码完全一致,那么编码转换工作就可以省略.

看懂没...Good Luck...希望我们不要再被编码问题所困扰。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值