过滤类
  1
 /** *//**
/** *//**
  2 *
 *
  3 *
 *
  4 * apache 出的 struts hibernate 字符编码处理过滤器
 * apache 出的 struts hibernate 字符编码处理过滤器
  5 *
 *
  6 *
 *
  7 */
 */
  8 package com.zeroidea.common;
package com.zeroidea.common;
  9
 /**//*
/**//*
 10 * Copyright 2004 The Apache Software Foundation
* Copyright 2004 The Apache Software Foundation
 11 *
*
 12 * Licensed under the Apache License, Version 2.0 (the "License");
* Licensed under the Apache License, Version 2.0 (the "License");
 13 * you may naot use this file except in compliance with the License.
* you may naot use this file except in compliance with the License.
 14 * You may obtain a copy of the License at
* You may obtain a copy of the License at
 15 *
*
 16 *     http://www.apache.org/licenses/LICENSE-2.0
*     http://www.apache.org/licenses/LICENSE-2.0
 17 *
*
 18 * Unless required by applicable law or agreed to in writing, software
* Unless required by applicable law or agreed to in writing, software
 19 * distributed under the License is distributed on an "AS IS" BASIS,
* distributed under the License is distributed on an "AS IS" BASIS,
 20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 21 * See the License for the specific language governing permissions and
* See the License for the specific language governing permissions and
 22 * limitations under the License.
* limitations under the License.
 23 */
*/
 24
 25 import java.io.IOException;
import java.io.IOException;
 26
 27 import javax.servlet.Filter;
import javax.servlet.Filter;
 28 import javax.servlet.FilterChain;
import javax.servlet.FilterChain;
 29 import javax.servlet.FilterConfig;
import javax.servlet.FilterConfig;
 30 import javax.servlet.ServletException;
import javax.servlet.ServletException;
 31 import javax.servlet.ServletRequest;
import javax.servlet.ServletRequest;
 32 import javax.servlet.ServletResponse;
import javax.servlet.ServletResponse;
 33
 34
 35
 /** *//**
/** *//**
 36 * <p>Example filter that sets the character encoding to be used in parsing the
 * <p>Example filter that sets the character encoding to be used in parsing the
 37 * incoming request, either unconditionally or only if the client did not
 * incoming request, either unconditionally or only if the client did not
 38 * specify a character encoding.  Configuration of this filter is based on
 * specify a character encoding.  Configuration of this filter is based on
 39 * the following initialization parameters:</p>
 * the following initialization parameters:</p>
 40 * <ul>
 * <ul>
 41 * <li><strong>encoding</strong> - The character encoding to be configured
 * <li><strong>encoding</strong> - The character encoding to be configured
 42 *     for this request, either conditionally or unconditionally based on
 *     for this request, either conditionally or unconditionally based on
 43 *     the <code>ignore</code> initialization parameter.  This parameter
 *     the <code>ignore</code> initialization parameter.  This parameter
 44 *     is required, so there is no default.</li>
 *     is required, so there is no default.</li>
 45 * <li><strong>ignore</strong> - If set to "true", any character encoding
 * <li><strong>ignore</strong> - If set to "true", any character encoding
 46 *     specified by the client is ignored, and the value returned by the
 *     specified by the client is ignored, and the value returned by the
 47 *     <code>selectEncoding()</code> method is set.  If set to "false,
 *     <code>selectEncoding()</code> method is set.  If set to "false,
 48 *     <code>selectEncoding()</code> is called <strong>only</strong> if the
 *     <code>selectEncoding()</code> is called <strong>only</strong> if the
 49 *     client has not already specified an encoding.  By default, this
 *     client has not already specified an encoding.  By default, this
 50 *     parameter is set to "true".</li>
 *     parameter is set to "true".</li>
 51 * </ul>
 * </ul>
 52 *
 *
 53 * <p>Although this filter can be used unchanged, it is also easy to
 * <p>Although this filter can be used unchanged, it is also easy to
 54 * subclass it and make the <code>selectEncoding()</code> method more
 * subclass it and make the <code>selectEncoding()</code> method more
 55 * intelligent about what encoding to choose, based on characteristics of
 * intelligent about what encoding to choose, based on characteristics of
 56 * the incoming request (such as the values of the <code>Accept-Language</code>
 * the incoming request (such as the values of the <code>Accept-Language</code>
 57 * and <code>User-Agent</code> headers, or a value stashed in the current
 * and <code>User-Agent</code> headers, or a value stashed in the current
 58 * user's session.</p>
 * user's session.</p>
 59 *
 *
 60 * @author Craig McClanahan
 * @author Craig McClanahan
 61 * @version $Revision: 1.2 $ $Date: 2004/03/18 16:40:28 $
 * @version $Revision: 1.2 $ $Date: 2004/03/18 16:40:28 $
 62 */
 */
 63
 64
 public class SetCharacterEncodingFilter implements Filter
public class SetCharacterEncodingFilter implements Filter  {
{
 65
 66
 67 // ----------------------------------------------------- Instance Variables
    // ----------------------------------------------------- Instance Variables
 68
 69
 70
 /** *//**
    /** *//**
 71 * The default character encoding to set for requests that pass through
     * The default character encoding to set for requests that pass through
 72 * this filter.
     * this filter.
 73 */
     */
 74 protected String encoding = null;
    protected String encoding = null;
 75
 76
 /** *//**
    /** *//**
 77 * The filter configuration object we are associated with.  If this value
     * The filter configuration object we are associated with.  If this value
 78 * is null, this filter instance is not currently configured.
     * is null, this filter instance is not currently configured.
 79 *
     * 
 80 * @uml.property name="filterConfig"
     * @uml.property name="filterConfig"
 81 * @uml.associationEnd multiplicity="(0 1)"
     * @uml.associationEnd multiplicity="(0 1)"
 82 */
     */
 83 protected FilterConfig filterConfig = null;
    protected FilterConfig filterConfig = null;
 84
 85
 86
 87
 /** *//**
    /** *//**
 88 * Should a character encoding specified by the client be ignored?
     * Should a character encoding specified by the client be ignored?
 89 */
     */
 90 protected boolean ignore = true;
    protected boolean ignore = true;
 91
 92
 93 // --------------------------------------------------------- Public Methods
    // --------------------------------------------------------- Public Methods
 94
 95
 96
 /** *//**
    /** *//**
 97 * Take this filter out of service.
     * Take this filter out of service.
 98 */
     */
 99
 public void destroy()
    public void destroy()  {
{
100
101 this.encoding = null;
        this.encoding = null;
102 this.filterConfig = null;
        this.filterConfig = null;
103
104 }
    }
105
106
107
 /** *//**
    /** *//**
108 * Select and set (if specified) the character encoding to be used to
     * Select and set (if specified) the character encoding to be used to
109 * interpret request parameters for this request.
     * interpret request parameters for this request.
110 *
     *
111 * @param request The servlet request we are processing
     * @param request The servlet request we are processing
112 * @param result The servlet response we are creating
     * @param result The servlet response we are creating
113 * @param chain The filter chain we are processing
     * @param chain The filter chain we are processing
114 *
     *
115 * @exception IOException if an input/output error occurs
     * @exception IOException if an input/output error occurs
116 * @exception ServletException if a servlet error occurs
     * @exception ServletException if a servlet error occurs
117 */
     */
118 public void doFilter(ServletRequest request, ServletResponse response,
    public void doFilter(ServletRequest request, ServletResponse response,
119 FilterChain chain)
                         FilterChain chain)
120
 throws IOException, ServletException
    throws IOException, ServletException  {
{
121
122 // Conditionally select and set the character encoding to be used
        // Conditionally select and set the character encoding to be used
123
 if (ignore || (request.getCharacterEncoding() == null))
        if (ignore || (request.getCharacterEncoding() == null))  {
{
124 String encoding = selectEncoding(request);
            String encoding = selectEncoding(request);
125 if (encoding != null)
            if (encoding != null)
126 request.setCharacterEncoding(encoding);
                request.setCharacterEncoding(encoding);
127 }
        }
128
129 // Pass control on to the next filter
    // Pass control on to the next filter
130 chain.doFilter(request, response);
        chain.doFilter(request, response);
131
132 }
    }
133
134
 /** *//**
    /** *//**
135 * Place this filter into service.
     * Place this filter into service.
136 *
     * 
137 * @param filterConfig The filter configuration object
     * @param filterConfig The filter configuration object
138 *
     * 
139 * @uml.property name="filterConfig"
     * @uml.property name="filterConfig"
140 */
     */
141
 public void init(FilterConfig filterConfig) throws ServletException
    public void init(FilterConfig filterConfig) throws ServletException  {
{
142
143 this.filterConfig = filterConfig;
        this.filterConfig = filterConfig;
144 this.encoding = filterConfig.getInitParameter("encoding");
        this.encoding = filterConfig.getInitParameter("encoding");
145 String value = filterConfig.getInitParameter("ignore");
        String value = filterConfig.getInitParameter("ignore");
146 if (value == null)
        if (value == null)
147 this.ignore = true;
            this.ignore = true;
148 else if (value.equalsIgnoreCase("true"))
        else if (value.equalsIgnoreCase("true"))
149 this.ignore = true;
            this.ignore = true;
150 else if (value.equalsIgnoreCase("yes"))
        else if (value.equalsIgnoreCase("yes"))
151 this.ignore = true;
            this.ignore = true;
152 else
        else
153 this.ignore = false;
            this.ignore = false;
154
155 }
    }
156
157 // ------------------------------------------------------ Protected Methods
    // ------------------------------------------------------ Protected Methods
158
159
160
 /** *//**
    /** *//**
161 * Select an appropriate character encoding to be used, based on the
     * Select an appropriate character encoding to be used, based on the
162 * characteristics of the current request and/or filter initialization
     * characteristics of the current request and/or filter initialization
163 * parameters.  If no character encoding should be set, return
     * parameters.  If no character encoding should be set, return
164 * <code>null</code>.
     * <code>null</code>.
165 * <p>
     * <p>
166 * The default implementation unconditionally returns the value configured
     * The default implementation unconditionally returns the value configured
167 * by the <strong>encoding</strong> initialization parameter for this
     * by the <strong>encoding</strong> initialization parameter for this
168 * filter.
     * filter.
169 *
     *
170 * @param request The servlet request we are processing
     * @param request The servlet request we are processing
171 */
     */
172
 protected String selectEncoding(ServletRequest request)
    protected String selectEncoding(ServletRequest request)  {
{
173
174 return (this.encoding);
        return (this.encoding);
175
176 }
    }
177
178
179 }
}
180
 web.xml 中的设置
 1 <!-- 编码转换过滤器  -->
 <!-- 编码转换过滤器  -->
 2 <filter>
 <filter>
 3 <filter-name>Encoding</filter-name>
  <filter-name>Encoding</filter-name>
 4 <filter-class>com.zeroidea.common.SetCharacterEncodingFilter</filter-class>
  <filter-class>com.zeroidea.common.SetCharacterEncodingFilter</filter-class>
 5 <init-param>
  <init-param>
 6 <param-name>encoding</param-name>
   <param-name>encoding</param-name>
 7 <param-value>GBK</param-value>
   <param-value>GBK</param-value>
 8 </init-param>
  </init-param>
 9 </filter>
 </filter>
10 
 
11 <!-- 映射过滤器-->
 <!-- 映射过滤器-->
12 <filter-mapping>
 <filter-mapping>
13 <filter-name>Encoding</filter-name>
  <filter-name>Encoding</filter-name>
14 <url-pattern>*.jsp</url-pattern>
  <url-pattern>*.jsp</url-pattern>
15 </filter-mapping>
 </filter-mapping>
16 <filter-mapping>
 <filter-mapping>
17 <filter-name>Encoding</filter-name>
  <filter-name>Encoding</filter-name>
18 <url-pattern>*.do</url-pattern>
  <url-pattern>*.do</url-pattern>
19 </filter-mapping>
 </filter-mapping> 
	posted on 2006-11-30 16:32 
一手的小窝窝 阅读(1922) 
评论(1)  编辑  收藏  所属分类: 
JAVA