tomcat自带的编码过滤器

转载 2007年10月08日 22:08:00
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License.  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/


package dgut.ke.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>Example filter that sets the character encoding to be used in parsing the
 * incoming request, either unconditionally or only if the client did not
 * specify a character encoding.  Configuration of this filter is based on
 * the following initialization parameters:</p>
 * <ul>
 * <li><strong>encoding</strong> - The character encoding to be configured
 *     for this request, either conditionally or unconditionally based on
 *     the <code>ignore</code> initialization parameter.  This parameter
 *     is required, so there is no default.</li>
 * <li><strong>ignore</strong> - If set to "true", any character encoding
 *     specified by the client is ignored, and the value returned by the
 *     <code>selectEncoding()</code> method is set.  If set to "false,
 *     <code>selectEncoding()</code> is called <strong>only</strong> if the
 *     client has not already specified an encoding.  By default, this
 *     parameter is set to "true".</li>
 * </ul>
 *
 * <p>Although this filter can be used unchanged, it is also easy to
 * subclass it and make the <code>selectEncoding()</code> method more
 * intelligent about what encoding to choose, based on characteristics of
 * the incoming request (such as the values of the <code>Accept-Language</code>
 * and <code>User-Agent</code> headers, or a value stashed in the current
 * user's session.</p>
 *
 * @author Craig McClanahan
 * @version $Revision: 466607 $ $Date: 2006-10-21 17:09:50 -0600 (Sat, 21 Oct 2006) $
 */

public class SetCharacterEncodingFilter implements Filter {


    // ----------------------------------------------------- Instance Variables


    /**
     * The default character encoding to set for requests that pass through
     * this filter.
     */
    protected String encoding = null;


    /**
     * The filter configuration object we are associated with.  If this value
     * is null, this filter instance is not currently configured.
     */
    protected FilterConfig filterConfig = null;


    /**
     * Should a character encoding specified by the client be ignored?
     */
    protected boolean ignore = true;


    // --------------------------------------------------------- Public Methods


    /**
     * Take this filter out of service.
     */
    public void destroy() {

        this.encoding = null;
        this.filterConfig = null;

    }


    /**
     * Select and set (if specified) the character encoding to be used to
     * interpret request parameters for this request.
     *
     * @param request The servlet request we are processing
     * @param result The servlet response we are creating
     * @param chain The filter chain we are processing
     *
     * @exception IOException if an input/output error occurs
     * @exception ServletException if a servlet error occurs
     */
    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 Methods


    /**
     * Select an appropriate character encoding to be used, based on the
     * characteristics of the current request and/or filter initialization
     * parameters.  If no character encoding should be set, return
     * <code>null</code>.
     * <p>
     * The default implementation unconditionally returns the value configured
     * by the <strong>encoding</strong> initialization parameter for this
     * filter.
     *
     * @param request The servlet request we are processing
     */
    protected String selectEncoding(ServletRequest request) {

        return (this.encoding);

    }


}
还需要在web.xml中配置相关信息
<!-- Example filter to set character encoding on each request -->
   <filter>
      <filter-name>Set Character Encoding</filter-name>
      <filter-class>dgut.ke.filter.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>/*</url-pattern>
    </filter-mapping>

 

Spring MVC 自带的字符编码过滤器以及Tomcat字符编码设置,彻底解决中文参数乱码问题

java Web项目添加Spring支持后,可使用Spring自带的字符编码过滤器。源码在spring-web-4.1.0.RELEASE.jar包下的org.springframework.web....
  • BeauXie
  • BeauXie
  • 2016年11月29日 10:41
  • 4713

JSP用过滤器解决request中文乱码问题

(1)客户端的数据一般是通过HTTP  GET/POST方式提交给服务器,在服务器端用request.getParameter() 读取参数时,很容易出现中文乱码现象。 (2)用过滤器解决requ...
  • u010870518
  • u010870518
  • 2014年09月10日 14:01
  • 2039

tomcat原理及实现-----tomcat的过滤器(第三篇)

对于tomcat的过滤器,
  • hao707822882
  • hao707822882
  • 2014年06月11日 20:21
  • 1972

Spring MVC 自带的字符编码过滤器以及Tomcat字符编码设置,彻底解决中文参数乱码问题

一、Spring MVC字符编码配置 java Web项目添加Spring支持后,可使用Spring自带的字符编码过滤器。源码在spring-web-4.1.0.RELEASE.jar包下的org....
  • weixin_38437243
  • weixin_38437243
  • 2018年01月11日 19:28
  • 18

Spring MVC 自带的字符编码过滤器以及Tomcat字符编码设置,彻底解决中文参数乱码问题

java Web项目添加Spring支持后,可使用Spring自带的字符编码过滤器。源码在spring-web-4.1.0.RELEASE.jar包下的org.springframework.web....
  • BeauXie
  • BeauXie
  • 2016年11月29日 10:41
  • 4713

润乾设计器修改自带的tomcat的端口和编码

最近经常遇见客户的设计器中润乾自带的tomcat无法启动或者编码不对,导致无法在浏览器中预览报表。          无法启动tomcat一般的问题就是系统的6001端口号被占用了,这时您需要修改端...
  • Lgghgg
  • Lgghgg
  • 2013年05月06日 10:34
  • 647

tomcat8 编写字符编码Filter过滤器无效问题

同事遇到编码问题时想做一个解决全站的字符编码过滤器,过滤器类和配置如下: 过滤器类: package com.chaoxing.newspaper.web.filter; import java.i...
  • fachunfc
  • fachunfc
  • 2017年06月02日 17:36
  • 507

Tomcat 5/6 GBK 编码下完美解决中文表单问题的过滤器

Tomcat 5/6 GBK 编码下完美解决中文表单问题的过滤器常见的 Tomcat 5 表单编码提交后乱码的解决方案有很多, 有改 Tomcat Connector 属性的, 有加过滤器的. 但是 ...
  • youge
  • youge
  • 2011年02月11日 11:00
  • 354

Tomcat5/6 GBK编码下完美解决中文表单问题的过滤器

常见的 Tomcat 5 表单编码提交后乱码的解决方案有很多, 有改 Tomcat Connector 属性的, 有加过滤器的. 但是 Tomcat 自带的那个过滤器只解决了 POST 方式的乱码却不...
  • mm2223
  • mm2223
  • 2011年11月23日 17:11
  • 739

vue自带过滤器

一 过滤器写法 {{ message | Filter}}   二 Vue自带的过滤器:capitalize 功能:首字母大写 DOCTYPE html> html>...
  • qq_19295069
  • qq_19295069
  • 2017年05月23日 17:14
  • 89
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:tomcat自带的编码过滤器
举报原因:
原因补充:

(最多只允许输入30个字)