配置一个filterpackage mysolution.common.servlet.filter;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;/**
 * <p>Title: $Id: EncodingFilter.java,v 1.1 2005/09/11 11:21:16 Michael Exp $</p>
 * <p>Description: Request Encoding Filter</p>
 * <p>Copyright: Copyright (c) 2005</p>
 * <p>Company: MJC Studio</p>
 *
 * @author Michael J Chane
 * @version $Revision: 1.1 $ $Date: 2005/09/11 11:21:16 $
 */
public class EncodingFilter implements Filter {  /**
   * Request character set name.
   */
  protected String encoding = null;  /**
   * Filter configurations (not used in this class).
   */
  protected FilterConfig filterConfig = null;  /**
   * Initializations on the filter. It reads the encoding name from the filter's
   * &lt;init-param&gt; parameter
   *
   * @param filterConfig FilterConfig
   * @throws ServletException if an unexpected servlet exception occurs
   */
  public void init(FilterConfig filterConfig) throws ServletException {
    this.filterConfig = filterConfig;
    this.encoding = filterConfig.getInitParameter("encoding");
  }  /**
   * Performs the filter action.
   *
   * @param request ServletRequest
   * @param response ServletResponse
   * @param chain FilterChain
   * @throws IOException if an unexpected I/O exception occurs
   * @throws ServletException if an unexpected servlet exception occurs
   */
  public void doFilter(ServletRequest request,
                       ServletResponse response,
                       FilterChain chain) throws IOException, ServletException {
    // Selects and sets (if needed) the character encoding to be used
    String encoding = selectEncoding(request);    if (encoding != null) {
      request.setCharacterEncoding(encoding);
    }    // Passes the control on to the next filter
    chain.doFilter(request, response);
  }  /**
   * Destories the filter.
   */
  public void destroy() {
    this.encoding = null;
    this.filterConfig = null;
  }  /**
   * Retrieves the encoding.
   *
   * @return the encoding
   */
  public String getEncoding() {
    return encoding;
  }  /**
   * Sets the encoding to the given value.
   *
   * @param encoding the given encoding
   */
  public void setEncoding(String encoding) {
    this.encoding = encoding;
  }  /**
   * Reads the character set name. <br>
   * The default value is the filter's &lt;init-param&gt; parameter.
   *
   * @param request ServletRequest
   * @return the character set name.
   */
  protected String selectEncoding(ServletRequest request) {
    return this.encoding;
  }
}web.xml<filter>
  <filter-name>EncodingFilter</filter-name> 
  <filter-class>mysolution.common.servlet.EncodingFilter</filter-class> 
 <init-param>
  <param-name>encoding</param-name> 
  <param-value>UTF-8</param-value> <!-- 或者你需要的编码格式 -->
  </init-param>
  </filter>
 <filter-mapping>
  <filter-name>EncodingFilter</filter-name> 
  <url-pattern>/*</url-pattern> 
  </filter-mapping>

解决方案 »

  1.   

    各位,是这样的,我知道用filter,已经做了,请问是不是在ActionForm中的reset()方法中也可以用request.setEncodingCharacter("GBK")呢?
    还有,我得到一个form后,如有属性name:
    String myname = form.getName();
    PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("c://1.html")));
    就是说我想把form得到的myname写入c盘的1.html文件中,为什么会提示:
     [ServletException in:/web/client/introduction.jsp] Invalid byte 2 of 2-byte UTF-8 sequence.'  
    希望得到大家帮助!