博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CharacterEncodingFilter-Spring字符编码过滤器
阅读量:5090 次
发布时间:2019-06-13

本文共 5436 字,大约阅读时间需要 18 分钟。

  用于处理项目中的乱码问题。

  关系:

java.lang.Object  extended by org.springframework.web.filter.GenericFilterBean      extended by org.springframework.web.filter.OncePerRequestFilter          extended by org.springframework.web.filter.CharacterEncodingFilter

  源码:

1 /* 2  * Copyright 2002-2007 the original author or authors. 3  * 4  * Licensed under the Apache License, Version 2.0 (the "License"); 5  * you may not use this file except in compliance with the License. 6  * You may obtain a copy of the License at 7  * 8  *      http://www.apache.org/licenses/LICENSE-2.0 9  *10  * Unless required by applicable law or agreed to in writing, software11  * distributed under the License is distributed on an "AS IS" BASIS,12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13  * See the License for the specific language governing permissions and14  * limitations under the License.15  */16 17 package org.springframework.web.filter;18 19 import java.io.IOException;20 import javax.servlet.FilterChain;21 import javax.servlet.ServletException;22 import javax.servlet.http.HttpServletRequest;23 import javax.servlet.http.HttpServletResponse;24 25 /**26  * Servlet 2.3/2.4 Filter that allows one to specify a character encoding for27  * requests. This is useful because current browsers typically do not set a28  * character encoding even if specified in the HTML page or form.29  *30  * 

This filter can either apply its encoding if the request does not31 * already specify an encoding, or enforce this filter's encoding in any case32 * ("forceEncoding"="true"). In the latter case, the encoding will also be33 * applied as default response encoding on Servlet 2.4+ containers (although34 * this will usually be overridden by a full content type set in the view).35 *36 * @author Juergen Hoeller37 * @since 15.03.200438 * @see #setEncoding39 * @see #setForceEncoding40 * @see javax.servlet.http.HttpServletRequest#setCharacterEncoding41 * @see javax.servlet.http.HttpServletResponse#setCharacterEncoding42 */43 public class CharacterEncodingFilter extends OncePerRequestFilter {44 45 private String encoding;46 47 private boolean forceEncoding = false;48 49 50 /**51 * Set the encoding to use for requests. This encoding will be passed into a52 * {

@link javax.servlet.http.HttpServletRequest#setCharacterEncoding} call.53 *

Whether this encoding will override existing request encodings54 * (and whether it will be applied as default response encoding as well)55 * depends on the {

@link #setForceEncoding "forceEncoding"} flag.56 */57 public void setEncoding(String encoding) {58 this.encoding = encoding;59 }60 61 /**62 * Set whether the configured {
@link #setEncoding encoding} of this filter63 * is supposed to override existing request and response encodings.64 *

Default is "false", i.e. do not modify the encoding if65 * {

@link javax.servlet.http.HttpServletRequest#getCharacterEncoding()}66 * returns a non-null value. Switch this to "true" to enforce the specified67 * encoding in any case, applying it as default response encoding as well.68 *

Note that the response encoding will only be set on Servlet 2.4+69 * containers, since Servlet 2.3 did not provide a facility for setting70 * a default response encoding.71 */72 public void setForceEncoding(boolean forceEncoding) {73 this.forceEncoding = forceEncoding;74 }75 76 77 @Override78 protected void doFilterInternal(79 HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)80 throws ServletException, IOException {81 82 if (this.encoding != null && (this.forceEncoding || request.getCharacterEncoding() == null)) {83 request.setCharacterEncoding(this.encoding);84 if (this.forceEncoding) {85 response.setCharacterEncoding(this.encoding);86 }87 }88 filterChain.doFilter(request, response);89 }90 91 }

  官方解释: 

  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.

  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).

  通过源码可以看到在web.xml配置CharacterEncodingFilter 时,可以配置两个参数:encoding和forceEncoding ;

  encoding:编码格式;

  forceEncoding :是否允许设置的encoding 覆盖request和response中已经存在的encodings。

  如何使用:

1      
2
characterEncodingFilter
3
org.springframework.web.filter.CharacterEncodingFilter
4
5
encoding
6
UTF-8
7
8
9
forceEncoding
10
true
11
12
13
14
characterEncodingFilter
15
/*
16

 

转载于:https://www.cnblogs.com/jbelial/p/3934986.html

你可能感兴趣的文章
十六进制的ASCII码 "\u6cf0\u56fd" 解码成unicode
查看>>
Java语言概述
查看>>
关于BOM知识的整理
查看>>
android中自定义下拉框(转)
查看>>
Android设计模式源码解析之外观模式(Facade)
查看>>
并查集 (poj 1611 The Suspects)
查看>>
使用word发布博客
查看>>
冒泡排序算法的C++,Java和Python实现和冒泡排序算法三种语言效率的比较
查看>>
C9---include,编译
查看>>
Maven简介(六)——Dependency
查看>>
android106 C基本数据类型
查看>>
oc-25-id类型
查看>>
STL 案例分析
查看>>
[ActionScript 3.0] AS3 双A字模型
查看>>
后台管理项目简单小总结------彭记(021)
查看>>
死磕JDK源码之Thread
查看>>
jekyll 安装 ...
查看>>
微信页面关于点击按钮关注公众号放到链接里无关注按钮
查看>>
python 字典处理的一些坑
查看>>
构建oracle12c的Docker镜像
查看>>