解决Tomcat 编码问题的Character Encoding 另类过滤器实现.

类别:Java 点击:0 评论:0 推荐:

使用我写的这个Class,然后再Tomcat里配置一下,比在自己的Webapp里配置的过滤器还要方便,高效。
一行配置就搞定:

server.xml里增加下面的配置:
<Valve className="org.apache.catalina.valves.CharSetValve" encoding="GBK"/>
可以放到Engine, Host, 或者 Context 元素中:

例如:
<Context docBase="d:\lizongbo\mywebapp" path="/lizongbo"
         privileged="true" antiResourceLocking="false" antiJARLocking="false">
<Valve className="org.apache.catalina.valves.CharSetValve" encoding="GBK"/>
</Context>

当然这个class不存在的,于是,下面是java代码,
把编译好的class放到 D:\jakarta-tomcat-5.5.7
\server\classes\org\apache\catalina\valves\下,就可以了。

java 代码:

package org.apache.catalina.valves;

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

import org.apache.catalina.*;
import org.apache.catalina.connector.*;
import org.apache.catalina.util.*;

/**
 * <p>Title: CharSetValve </p>
 * <p>Description: a CharSet Filter for Tomcat Application</p>
 * <p>Copyright: Apache License Version 2.0  </p>
 * <p>Company: lizongbo</p>
 * @author lizongbo @ gmail.com
 * @version 1.0
 */
public class CharSetValve
    extends ValveBase implements Lifecycle {
  public CharSetValve() {
    super();
  }

  protected LifecycleSupport lifecycle = new LifecycleSupport(this);

  private StringManager sm =
      StringManager.getManager(Constants.Package);

  private boolean started = false;
  private String encoding = System.setProperty("file.encoding", "GBK");

  public String getEncoding() {
    return encoding;
  }

  public void setEncoding(String encoding) {
    this.encoding = encoding;
  }

  public void invoke(Request request, Response response) throws IOException,
      ServletException {
    request.getRequest().setCharacterEncoding(getEncoding());
    getNext().invoke(request, response);

  }

  public void addLifecycleListener(LifecycleListener listener) {
    lifecycle.addLifecycleListener(listener);
  }

  public LifecycleListener[] findLifecycleListeners() {
    return lifecycle.findLifecycleListeners();

  }

  public void removeLifecycleListener(LifecycleListener listener) {

    lifecycle.removeLifecycleListener(listener);

  }

  public void start() throws LifecycleException {

    if (started) {
      throw new LifecycleException
          (sm.getString("accessLogValve.alreadyStarted"));
    }

  }

  public void stop() throws LifecycleException {
    if (!started) {
      throw new LifecycleException
          (sm.getString("accessLogValve.notStarted"));
    }
    lifecycle.fireLifecycleEvent(STOP_EVENT, null);
    started = false;

  }
}



详情请参考:
http://issues.apache.org/bugzilla/show_bug.cgi?id=32795
http://jakarta.apache.org/tomcat/tomcat-5.5-doc/config/valve.html

对了,Tomcat的示例里:
D:\jakarta-tomcat-5.5.7\webapps\servlets-examples\WEB-INF\web.xml

里被日本人强制指定默认值为日文编码了,FT!!!!!!!!!!!!!!!!!!!!!!!!!!!

<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>filters.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>EUC_JP</param-value>
</init-param>
</filter>

本文地址:http://com.8s8s.com/it/it11328.htm