`
jiagou
  • 浏览: 2534667 次
文章分类
社区版块
存档分类
最新评论

CharacterEncodingFilter (spring中的filter设置编码)

 
阅读更多
(源码包)这是spring的package org.springframework.web.filter包中CharacterEncodingFilter 类
/*
 * Copyright 2002-2007 the original author or authors.
 *
 * Licensed 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 org.springframework.web.filter;


import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * Servlet 2.3/2.4 Filter that allows one to specify a character encoding for
 * requests. This is useful because current browsers typically do not set a
 * character encoding even if specified in the HTML page or form.
 *
 * <p>This filter can either apply its encoding if the request does not
 * already specify an encoding, or enforce this filter's encoding in any case
 * ("forceEncoding"="true"). In the latter case, the encoding will also be
 * applied as default response encoding on Servlet 2.4+ containers (although
 * this will usually be overridden by a full content type set in the view).
 *
 * @author Juergen Hoeller
 * @since 15.03.2004
 * @see #setEncoding
 * @see #setForceEncoding
 * @see javax.servlet.http.HttpServletRequest#setCharacterEncoding
 * @see javax.servlet.http.HttpServletResponse#setCharacterEncoding
 */
public class CharacterEncodingFilter extends OncePerRequestFilter {


	private String encoding;


	private boolean forceEncoding = false;




	/**
	 * Set the encoding to use for requests. This encoding will be passed into a
	 * {@link javax.servlet.http.HttpServletRequest#setCharacterEncoding} call.
	 * <p>Whether this encoding will override existing request encodings
	 * (and whether it will be applied as default response encoding as well)
	 * depends on the {@link #setForceEncoding "forceEncoding"} flag.
	 */
	public void setEncoding(String encoding) {
		this.encoding = encoding;
	}


	/**
	 * Set whether the configured {@link #setEncoding encoding} of this filter
	 * is supposed to override existing request and response encodings.
	 * <p>Default is "false", i.e. do not modify the encoding if
	 * {@link javax.servlet.http.HttpServletRequest#getCharacterEncoding()}
	 * returns a non-null value. Switch this to "true" to enforce the specified
	 * encoding in any case, applying it as default response encoding as well.
	 * <p>Note that the response encoding will only be set on Servlet 2.4+
	 * containers, since Servlet 2.3 did not provide a facility for setting
	 * a default response encoding.
	 */
	public void setForceEncoding(boolean forceEncoding) {
		this.forceEncoding = forceEncoding;
	}




	@Override
	protected void doFilterInternal(
			HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
			throws ServletException, IOException {


		if (this.encoding != null && (this.forceEncoding || request.getCharacterEncoding() == null)) {
			request.setCharacterEncoding(this.encoding);
			if (this.forceEncoding) {
				response.setCharacterEncoding(this.encoding);
			}
		}
		filterChain.doFilter(request, response);
	}


}


web.xml加

<filter>
  		<filter-name>CharacterEncodingFilter</filter-name>
  		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  		<init-param>
  			<param-name>encoding</param-name>
  			<param-value>UTF-8</param-value>
  		</init-param>
  	</filter>
  	
  	<filter-mapping>
  		<filter-name>CharacterEncodingFilter</filter-name>
  		<url-pattern>/*</url-pattern>
  	</filter-mapping>



分享到:
评论

相关推荐

    spring配置编码格式

    org.springframework.web.filter.CharacterEncodingFilter &lt;/filter-class&gt; &lt;param-name&gt;encoding &lt;param-value&gt;UTF-8 &lt;/filter&gt; &lt;filter-mapping&gt; &lt;filter-name&gt;setCharacterEncoding&lt;/filter-name&gt; ...

    字符编码工具类CharacterEncodingFilter.java

    字符编码工具类CharacterEncodingFilter.java 字符编码工具类CharacterEncodingFilter.java 字符编码工具类CharacterEncodingFilter.java

    解决struts2.1.6+spring+hibernate 中文乱码

    &lt;filter-class&gt;org.springframework.web.filter.CharacterEncodingFilter&lt;/filter-class&gt; &lt;init-param&gt; &lt;param-name&gt;encoding&lt;/param-name&gt; &lt;param-value&gt;GBK&lt;/param-value&gt; &lt;/init-param&gt; ...

    spring-web-2.5.jar

    org.springframework.web.filter.CharacterEncodingFilter.class org.springframework.web.filter.CommonsRequestLoggingFilter.class org.springframework.web.filter.DelegatingFilterProxy.class org.spring...

    Spring MVC 入门实例

    22 &lt;filter-class&gt;org.springframework.web.filter.CharacterEncodingFilter&lt;/filter-class&gt; 23 24 &lt;param-name&gt;encoding 25 &lt;param-value&gt;UTF-8 26 27 &lt;/filter&gt; 28 29 &lt;filter-mapping&gt; 30 &lt;filter-name&gt;...

    JSP存到数据库乱码解决办法

    &lt;filter-class&gt;org.springframework.web.filter.CharacterEncodingFilter&lt;/filter-class&gt; &lt;param-name&gt;encoding &lt;param-value&gt;UTF-8 &lt;param-name&gt;forceEncoding &lt;param-value&gt;true &lt;/...

    CharacterEncodingFilter类的学习 .doc

    CharacterEncodingFilter类的学习CharacterEncodingFilter类的学习

    字符过滤器

    该过滤器能解决除ckeditor之外所有的的字符过滤,使用方法:在src下创建一个filter文件夹,将这个字符过滤器放在filter文件夹下,web.xml中代码如下 ... &lt;filter&gt; &lt;display-name&gt;CharacterEncodingFilter &lt;filter...

    Spring MVC 框架应用实例

    org.springframework.web.filter.CharacterEncodingFilter &lt;/filter-class&gt; &lt;param-name&gt;encoding &lt;param-value&gt;UTF-8 &lt;/filter&gt; &lt;filter-mapping&gt; &lt;filter-name&gt;encodingFilter&lt;/filter-name...

    ssh(structs,spring,hibernate)框架中的上传下载

     文件数据存储在Blob类型的FILE_CONTENT表字段上,在Spring中采用OracleLobHandler来处理Lob字段(包括Clob和Blob),由于在程序中不需要引用到oracle数据驱动程序的具体类且屏蔽了不同数据库处理Lob字段方法上的...

    DOS命令使用方法(超全).

    -- 利用spring监听 编码设置 --&gt; &lt;filter&gt; &lt;filter-name&gt;SpringCharacterEncodingFilter&lt;/filter-name&gt; &lt;filter-class&gt;org.springframework.web.filter.CharacterEncodingFilter&lt;/filter-class&gt; ...

    sshz中文乱码解决方法

    org.springframework.web.filter.CharacterEncodingFilter&lt;/filter-class&gt; &lt;init-param&gt; &lt;param-name&gt;encoding&lt;/param-name&gt; &lt;param-value&gt;GBK&lt;/param-value&gt; &lt;/init-param&gt; &lt;init-param&gt; &lt;param-name&gt;...

    web配置文件.txt

    &lt;filter-class&gt;org.springframework.web.filter.CharacterEncodingFilter&lt;/filter-class&gt; &lt;param-name&gt;encoding &lt;param-value&gt;UTF-8 &lt;param-name&gt;forceEncoding &lt;param-value&gt;true &lt;/filter&gt; &lt;filter...

    struts2+kindEditor4.1.7

    &lt;filter-class&gt;org.springframework.web.filter.CharacterEncodingFilter&lt;/filter-class&gt; &lt;param-name&gt;encoding &lt;param-value&gt;UTF-8 &lt;param-name&gt;forceEncoding &lt;param-value&gt;true &lt;/filter&gt; &lt;filter...

    CharacterEncodingFilter.java

    CharacterEncodingFilter.java

    生活轨迹SSH服务端

    &lt;filter-class&gt;org.springframework.web.filter.CharacterEncodingFilter&lt;/filter-class&gt; &lt;param-name&gt;encoding &lt;param-value&gt;UTF-8 &lt;/filter&gt; &lt;filter-mapping&gt; &lt;filter-name&gt;encodingFilter&lt;/filter-...

    myeclipse中文处理

    -- 中文过滤器start --&gt; &lt;filter&gt; &lt;filter-name&gt;Encoding&lt;/filter-name&gt; &lt;filter-class&gt; org.springframework.web.filter.CharacterEncodingFilter &lt;/filter-class&gt; &lt;init-param&gt;&lt;br&gt;...

    SPRING API 2.0.CHM

    ReflectionUtils.MethodFilter ReflectiveAspectJAdvisorFactory ReflectiveAspectJAdvisorFactory.SyntheticInstantiationAdvisor ReflectiveLoadTimeWeaver ReflectiveMethodInvocation ...

    JavaWeb乱码过滤器.zip

     &lt;filter-class&gt;com.github.zhanhb.filter.CharacterEncodingFilter&lt;/filter-class&gt;    &lt;description&gt;Encoding for content and query string, default UTF-8.  &lt;param-name&gt;characterEncoding  ...

    解决cannot be cast to javax.servlet.Filter 报错的问题

    cannot be cast to javax.servlet.Filter 报错, 原因servlet-api.jar冲突 使用maven开发web应用程序, 启动的时候报错: jar not loaded....java.lang.ClassCastException: org.springframework.web.f

Global site tag (gtag.js) - Google Analytics