Spring-web源码解析之Filter-CharacterEncodingFilter

本文涉及的产品
全局流量管理 GTM,标准版 1个月
云解析 DNS,旗舰版 1个月
云解析DNS,个人版 1个月
简介: 基于4.1.7.RELEASE在web.xml我们经常看见这么一段 encodingFilter org.

基于4.1.7.RELEASE


在web.xml我们经常看见这么一段

<filter>
   <filter-name>encodingFilter</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,下面我们就来看看CharacterEncodingFilter的源码,首先看看类图


CharacterEncodingFilter并不是直接实现Filter接口,而是继承了OncePerRequestFilter,其作用稍后再讲。

CharacterEncodingFilter含有两个属性,一个是在web.xml中经常出现的encoding,它指明了用于设置request的encoding的值

另一个是foreceEncoding,默认为false,如果设置为true它就会使encoding属性强制替换掉request中的encoding值,而不管request获取的encoding是否为空。

下面看其主要的方法

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);
}

上面说过encoding是设置request的编码,而这个方法就是具体实现过程。如果设置了forceEncoding,还会同时将response的默认编码设置为encoding。

目录
相关文章
|
15天前
|
存储 NoSQL Redis
redis 6源码解析之 object
redis 6源码解析之 object
43 6
|
4天前
|
XML Java 数据格式
Spring Cloud全解析:注册中心之zookeeper注册中心
使用ZooKeeper作为Spring Cloud的注册中心无需单独部署服务器,直接利用ZooKeeper服务端功能。项目通过`spring-cloud-starter-zookeeper-discovery`依赖实现服务注册与发现。配置文件指定连接地址,如`localhost:2181`。启动应用后,服务自动注册到ZooKeeper的`/services`路径下,形成临时节点,包含服务实例信息。
|
8天前
|
开发者 Python
深入解析Python `httpx`源码,探索现代HTTP客户端的秘密!
深入解析Python `httpx`源码,探索现代HTTP客户端的秘密!
32 1
|
8天前
|
开发者 Python
深入解析Python `requests`库源码,揭开HTTP请求的神秘面纱!
深入解析Python `requests`库源码,揭开HTTP请求的神秘面纱!
21 1
|
15天前
|
NoSQL Redis
redis 6源码解析之 ziplist
redis 6源码解析之 ziplist
16 5
|
4天前
|
算法 安全 Java
深入解析Java多线程:源码级别的分析与实践
深入解析Java多线程:源码级别的分析与实践
|
2月前
|
XML Java 数据格式
深度解析 Spring 源码:从 BeanDefinition 源码探索 Bean 的本质
深度解析 Spring 源码:从 BeanDefinition 源码探索 Bean 的本质
67 3
|
22天前
|
负载均衡 Java Spring
@EnableFeignClients注解源码解析
@EnableFeignClients注解源码解析
48 14
|
22天前
|
负载均衡 Java API
Feign 进行rpc 调用时使用ribbon负载均衡源码解析
Feign 进行rpc 调用时使用ribbon负载均衡源码解析
38 11
|
22天前
|
Java Spring 容器
Spring Boot 启动源码解析结合Spring Bean生命周期分析
Spring Boot 启动源码解析结合Spring Bean生命周期分析
60 11

热门文章

最新文章

推荐镜像

更多