<转载>springmvc(30):springmvc的字符编码过滤器CharacterEncodingFilter相关的几篇博文

浏览器Cookie迁移实战:从Chrome导出导入登录态全攻略 Cookie是浏览器保存的会话凭证,服务端通过Set-Cookie将登录态种在本地。迁移Cookie本质上是把服务端认可的会话标识搬到新环境,从而免去重复输入账号密码和二次验证的麻烦。在换电脑、切浏览器或重装系统时,掌握Cookie的导出导入技术能显著提升效率。Chrome与Edge同属Chromium内核,存储结构相似,但字段差异(如Domain、SameSite、Secure)常导致迁移失败。通过浏览器扩展或CDP脚本可实现跨浏览器登录态迁移,适用于内网后台、开发环境等场景。本文从原理到实践,梳理Coo 阅读详情

2018年2月22日

(blog 1 

--springmvc字符编码过滤器CharacterEncodingFilter浅析)

一、在web.xml中的配置
    <!-- characterEncodingFilter字符编码过滤器 -->
    <filter>
        <filter-name>characterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>characterEncodingFilter</filter-name>
        <!--这里的url根据你项目需求自己改-->
        <url-pattern>/</url-pattern>
    </filter-mapping>
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

二、CharacterEncodingFilter过滤器类浅析

打开该类源码,可以看到该类有三个类属性

private String encoding;

private boolean forceRequestEncoding = false;

private boolean forceResponseEncoding = false;
 
  • 1
  • 2
  • 3
  • 4
  • 5

主要方法只有一个,也就是下面这个

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

    String encoding = getEncoding();
    if (encoding != null) {
        if (isForceRequestEncoding() || request.getCharacterEncoding() == null) {
            request.setCharacterEncoding(encoding);
        }
        if (isForceResponseEncoding()) {
            response.setCharacterEncoding(encoding);
        }
    }
    filterChain.doFilter(request, response);
}
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

可以看到这个作用器的作用就是在request和response没有设置字符编码方式的时候设置一个编码方式。注意

if (isForceResponseEncoding()) {
    response.setCharacterEncoding(encoding);
}
 
  • 1
  • 2
  • 3

是在

filterChain.doFilter(request, response);
 
  • 1

之前执行的,也就是说设置的是response的默认编码方式,可以自己在代码里修改为其他的编码方式,网上有些文档说的是设置的是最终的编码方式,这是错的。


(blog 2 
--详解Spring中的CharacterEncodingFilter--forceEncoding为true在java代码中设置失效--html设置编码无效)

在项目中有很多让人头疼的问题,其中,编码问题位列其一,那么在Spring框架中是如何解决从页面传来的字符串的编码问题的呢?下面我们来看看Spring框架给我们提供过滤器CharacterEncodingFilter

1.看清结构:

&**

看到其继承GenericFilterBean和OncePerRequestFilter,也就是说,这个过滤器就是针对于每次浏览器请求进行过滤的,然后再其之上添加了父类没有的功能即处理字符编码。

 

2.官方解释:

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. (这句话就说你在html页面或表单中设置编码是没有用的)

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").(只要你设置了foreEncoding=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).

 

3.如何使用

下面来看看如何在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>
  <init-param>
   <param-name>forceEncoding</param-name>
   <param-value>true</param-value>
  </init-param>
 </filter>

 其中encoding用来设置编码格式,forceEncoding用来设置是否理会 request.getCharacterEncoding()方法,设置为true则强制覆盖之前的编码格式。

 

4.源码赏析

&**

Servlet容器启动的时候,会读取web.xml中对于过滤器的配置信息, 读取到<init-param>中的子标签<param-name>encoding和forceEncoding所对应的<param-value>的值,再通过调用该类setEncoding(String encoding)和setForceEncoding(boolean forceEncoding) 将值注入到这连个字段中。

&**

这里就能看到为什么设置foreEncoding为true会覆盖掉request.getCharacterEncoding()中的方法了吧,呵呵,还是那句话,源码之前了无秘密,只有深入到源代码之中才能看清本质。


(blog 3 

--Spring框架中CharacterEncodingFilter的作用真的没那么大)

之前看介绍Spring框架的时候,提到了Spring中内置了几种过滤器,其中有一种是CharacterEncodingFilter的过滤器,是将数据以某种格式编码的方式输出的。这个特性让我很兴奋,因为在WEB系统下,字符编码的格式是一定要面对的。所以我尝试了一下,结果很失望。

在web.xml中添加了如下内容

    <!-- 配置请求过滤器,编码格式设为UTF-8,避免中文乱码-->
    <filter>
       <filter-name>springUtf8Encoding</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>
       <init-param>
           <param-name>forceEncoding</param-name>
           <param-value>true</param-value>
       </init-param> 
    </filter>
    <filter-mapping>
       <filter-name>springUtf8Encoding</filter-name>
       <url-pattern>/*</url-pattern>
   </filter-mapping>
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

于是在浏览器中打开网页,在chrome中显示乱码,在opera中显示正常编码,查看了一下返回数据,发现HTTP报头中并没有指明编码类型,也就是说这时候浏览器并不知道消息体中是何种编码,于是它会按照自己默认编码去解析。这是不可忍受的。

于是我查看了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);
    }
}

 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91

这个filter可能与我们自己写的Filter不太相同,它继承的是OncePerRequestFilter,看看OncePerRequestFilter的源码,可以发现,OncePerRequestFilter的过滤器会负责调用doFilterInternal()方法,因此,我们只关注这个方法即可。 
这个方法很简单,就是看看是否设置了encoding,如果设置了encoding,且请求中没有设置characterEncoding的话,它会负责设置characterEncoding,并且如果forceEncoding设置为true的话,在responce中也会设置characterEncoding。

如何设置encoding和forceEncoding呢?这就是我们在配置文件中写的里面的数据,spring会负责将这些数据注入。我们可以不用管了。现在我们把这个方法简化一下,就是两个请求。

request.setCharacterEncoding(encoding);
response.setCharacterEncoding(encoding);
 
  • 1
  • 2

OK,现在我们来看一下setCharacterEncoding()干了点啥?我没找到setCharacterEncoding()的源码,只看看它的文档说明吧,

public void setCharacterEncoding(java.lang.String env)
                          throws java.io.UnsupportedEncodingException
Overrides the name of the character encoding used in the body of this request. This method must be called prior to reading request parameters or reading input using getReader().
Parameters:
env - a String containing the name of the character encoding.
Throws:
java.io.UnsupportedEncodingException - if this is not a valid encoding
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

看到这个说明的时候,我觉得这个作用就是更改HTTP报头中charset的内容,仅此而已,不会更改HTTPbody中的编码格式。所以我想到使用setContentType()来显式的设置输出的报文信息

setContentType("text/json";charst=utf-8);
 
  • 1

这样就OK了,在Chrome和Opera中已经可以正常显示了。

我们在看看setContentType的说明

public void setContentType(java.lang.String type)

Sets the content type of the response being sent to the client, if the response has not been committed yet. The given content type may include a character encoding specification, for example, text/html;charset=UTF-8. **The response's character encoding is only set from the given content type** if this method is called before getWriter is called.
This method may be called repeatedly to change content type and character encoding. This method has no effect if called after the response has been committed. It does not set the response's character encoding if it is called after getWriter has been called or after the response has been committed.

Containers must communicate the content type and the character encoding used for the servlet response's writer to the client if the protocol provides a way for doing so. In the case of HTTP, the Content-Type header is used.

Parameters:
type - a String specifying the MIME type of the content
See Also:
setLocale(java.util.Locale), setCharacterEncoding(java.lang.String), getOutputStream(), getWriter()
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

这里面很明显的说明了:输出的字符编码只有在给定的content type下面才有效。也即必须在有setContentType的前提下setCharacterEncoding()才有效,但是问题是setContentType()完全可以同时设置charset,那何必在setCharacterEncoding呢?

评论:

过滤器只负责向后台提交数据时编码,而不是页面上显示的编码。你的问题中的乱码是由于浏览器不知道后台是哪种编码只能用默认的编码显示才造成的乱码。一般都在html文件开头加上<meta http-equiv="content-type" content="text/html; charset=utf-8">类似的字样表示脚本编码。



【AI黑话日日新】什么是AI Infra? 是支撑AI模型开发、训练、推理、部署与规模化运营的,是连接底层算力与上层AI应用的“数字基建”。没有高效的AI Infra,大模型训练、推理与落地都无法高效实现。 阅读详情

相关推荐

从ENVI SARscape到SNAP:手把手教你迁移哨兵1 GRD数据预处理流程(含避坑指南)

本文详细介绍了从ENVI SARscape迁移到SNAP处理哨兵1 GRD数据的全流程,包括工具哲学差异、数据准备注意事项、核心处理流程等效实现及地形校正技巧。特别针对雷达数据预处理中的常见陷阱提供避坑指南,帮助用户高效完成工具迁移并确保结果质量。

weixin_28677005的博客 485

CharacterEncodingFilter 详解

一、什么是CharacterEncodingFilter 官方解释如下 是spring内置过滤器的一种,用来指定请求或者响应的编码格式。在web开发中经常被从来使用 二、CharacterEncodingFilter的用法 在web.xml中的配置 &amp;lt;filter&amp;gt; &amp;lt;filter-name&amp;gt;characterEncodingFilter&amp;lt;/fi...

NicoJ的博客 1万+

深度学习|卷积神经网络

介绍卷积神经网络基本理论,包括卷积层、池化层和全连接层,并阐述LeNet卷积神经网络的构建过程。

weixin_58243219的博客 5722

Spring MVC中的CharacterEncodingFilter介绍以及源码分析

一、场景 CharacterEncodingFilter是Spring MVC很简单很实用的一个过滤器,当前台JSP页面和JAVA代码中使用了不同的字符集进行编码的时候就会出现表单提交的数据或者上传/下载中文名称文件出现乱码的问题,那这个类就可以出场了。 二、配置 从名字就可以看出来它是个过滤器了,所以就要想配置普通过滤器那样配置到web.xml中去了,配置方式如下: &lt;...

HY—Chan 1700

三电平逆变器LC滤波设计避坑指南:从SVPWM谐波到Simulink仿真验证

本文详细解析了三电平逆变器LC滤波设计中的关键问题,从SVPWM谐波特性到Simulink仿真验证,提供了800V/10kW系统的参数计算方法和优化技巧。通过实际案例和仿真配置示例,帮助工程师避免常见设计误区,提升电能质量和系统稳定性。

weixin_29261711的博客 173

海康视觉平台VisionMaster实战入门:从通讯配置到工具应用

本文为海康VisionMaster视觉平台实战入门指南,详细介绍了从软件安装准备、与PLC等外部设备的通讯配置,到匹配定位、测量、畸变矫正等核心工具的应用方法。通过实战案例拆解,帮助工程师快速掌握如何搭建完整的机器视觉检测与引导定位系统,实现工业自动化项目落地。

weixin_29224309的博客 1008

CharacterEncodingFilter详解

在Web开发中,字符编码是一个至关重要的概念。CharacterEncodingFilter是Servlet规范中的一部分,它允许在请求进入Servlet时统一设置字符编码,从而确保在整个请求-响应周期中都使用相同的字符编码。通过本文的介绍,我们深入了解了在Web开发中非常重要的CharacterEncodingFilter。它为我们提供了一个简便的方式来处理字符编码,确保在Web应用中正常运行。

技术研究中心 846

强化学习Q值迭代的几何视角:从策略区域到快速收敛

在强化学习中,Q值迭代是求解最优策略的核心算法,其理论基础建立在贝尔曼不动点定理之上。该算法通过迭代更新Q函数,使其逐步逼近最优值函数。从几何视角看,整个Q函数空间可被视为一个高维流形,每个Q函数对应流形上的一个点,而贝尔曼最优算子则定义了流形上的变换轨迹。理解这一几何结构,特别是由策略超平面划分出的凸多面体区域,对于分析算法的收敛行为至关重要。在实际工程中,传统Q值迭代可能在高维或稀疏奖励环境下收敛缓慢,甚至陷入策略高原。通过引入策略区域缓存与自适应学习率等几何启发方法,可以更高效地导航策略空间,加速策略

weixin_30444111的博客 317

web.xml中配置CharacterEncodingFilter编码过滤器的原理

在web.xml文件中,我们经常可以看到配置的统一编码过滤器,他继承了OncePerRequestFilter,而OncePerRequestFilter继承自GenericFilterBean,GenericFilterBean实现了filter,因此可见CharacterEncodingFilter具有filter的所有特性;即实现拦截请求的功能,在HttpServletRequest到达 Se

uhgagnu的博客 2万+

第一讲:halcon模板匹配捆绑测量矩形检测芯片针脚

使用halcon模板匹配捆绑测量矩形,检测位置发生变换的芯片针脚缺陷。

uiopmn1的博客 2994

ArduPilot简介

一,什么是ArduPilot? ArduPilot是一个可信的,多用途的,开源的自动驾驶系统。该系统支持了非常多的交通工具类型,包括多旋翼无人机,传统直升机,固定翼飞行器,船,潜艇,巡逻车等。源码由大型开发者社区的专家和爱好者共同开发和维护。也欢迎更多的开发者加入源码的迭代演进,最好的“打开”方式就是加入开发者团队论坛。该论坛对所有人开放,在这里可以深入感受到开发者们对ArduPilot的热爱。 论坛链接:ArduPilot Discourse 开发者指南:Welcome to the ArduPil

从0到1,突破自己 5452

CharacterEncodingFilter 字符编码过滤器

<br />用法:web.xml<br /><filter><br />  <filter-name>encodingFilter</filter-name><br />  <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><br />  <init-param><br />   <param-name>encoding</param-name><br />   <param-value>UT

maco_liao的专栏 694

【C#程序员入门AI】AI应用的操作系统:Semantic Kernel 2026实战

用户ID用户信息

人工智能AI技术 1248

CharacterEncodingFilter的用法

CharacterEncoding是SpringMVC提供的一个一个过滤器,用于设置请求和响应的字符编码,解决乱码问题,他本身是一个过滤器。那么在SpringBoot中,CharacterEncoding就有一个很好的秒用。setForceEncoding(true) 设置请求和响应编码。setEncoding("UTF-8")设置编码。

tiantiantbtb的博客 413

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

CharacterEncodingFilter 过滤器的理解和配置

参考博客:http://blog.youkuaiyun.com/geloin/article/details/7441123      http://blog.youkuaiyun.com/heidan2006/article/details/3075730 在web项目中我们经常会遇到当前台JSP页面和JAVA代码中使用了不同的字符集进行编码的时候就会出现表单提交的数据或者上传/下载中文名称文件出现乱码的问题,这些

DoubleWeiWei001 8256

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

CharacterEncodingFilter 统一的字符编码应用案例说明

`CharacterEncodingFilter` 是Spring框架中的一个实用过滤器,用于统一设置请求和响应的字符编码。这确保了在整个请求处理过程中,数据能够以正确的编码进行传输和处理,从而避免了字符编码问题导致的乱码现象。

肖哥弹架构博客 582

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

CharacterEncodingFilter编码过滤器

楔子 Spring 编码过滤器 对post 提交没乱码,但是GET提交存在乱码,可以修改避免乱码问题 package org.springframework.web.filter; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.Map; import ...

zhuzi51的专栏 750

13.springboot中使用CharacterEncodingFilter的两种方式

1.只在application.properties文件中设置字符的编码方式,其他类中不需要声明字符的编码格式,如第一种方式中的WebSystemConfig类中就不需要再注册filter,只注册sevlet就可以了。3.在 application.properties , 禁用 Spring Boot 中默认启用的过滤器。设置步骤3的enabled的值为false后,运行项目,中文可以正常的显示出来。第一次运行没有关闭步骤3的enabled还是出现乱码。1.创建 MyServlet类,输出中文数据。

weixin_59334478的博客 2457

(6)SpringMVC中使用CharacterEncodingFilter编码过滤器处理请求和响应的乱码问题

(6)SpringMVC中使用CharacterEncodingFilter编码过滤器处理请求和响应的乱码问题

欢迎阅读我的博客 1115

报:类com.qin.servlet.HelloServlet不是Servlet

报错误:类com.qin.servlet.HelloServlet不是Servlet 类型 异常报告 消息 类com.qin.servlet.HelloServlet不是Servlet 描述 服务器遇到一个意外的情况,阻止它完成请求。 例外情况 jakarta.servlet.ServletException: 类com.qin.servlet.HelloServlet不是Servlet org.apache.catalina.authenticator.AuthenticatorBase.invoke

尘埃的博客 2200

'org.springframework.web.filter.CharacterEncodingFilter' is not assignable to 'javax.servlet.Filter'

记录一下… 今天在配置web.xml文件的过滤器时一直报这个错,之前貌似没碰到过,将处理方法记录一下: 解决办法: File–>Project Structer–>选中项目–>点击+号–>将Tomcat添加进来–>apply–>OK ...

米斯特尔.W 6401

maven org.springframework.web.filter.CharacterEncodingFilter cannot be cast to javax.servlet.Filter

使用maven将项目部署到tomcat容器,启动的时候报如下错误:严重: Exception starting filter charsetFilter java.lang.ClassCastException: org.springframework.web.filter.CharacterEncodingFilter cannot be cast to javax.servlet.Filter

wenguang_hz的博客 2万+

字符过滤器CharacterEncodingFilter和后端页面过滤器PageFilter

 CharacterEncodingFilter在web容器中的注册: &lt;!-- 处理请求编码过滤器 配置和servlet一样 --&gt; &lt;filter&gt; &lt;filter-name&gt;CharacterEncodingFilter&lt;/filter-name&gt; &lt;filter-class&gt;com.lin.linframe...

linry的博客 460

CharacterEncodingFilter类的学习

        我的第一篇正式BLOG就写点简单的东西吧,记录一下CharacterEncodingFilter这个类的使用和作用。        很简单很实用的一个过滤器,当前台JSP页面和JAVA代码中使用了不同的字符集进行编码的时候就会出现表单提交的数据或者上传/下载中文名称文件出现乱码的问题,那这个类就可以出场了。        从名字就可以看出来它是个过滤器了,所以就要想配置普通

『文 涛』 的专栏 2万+

【Spring进阶】Spring重写CharacterEncodingFilter完全解决编码问题

编码问题是在编程过程中经常遇到的问题之一,如果早期不指定统一的编码格式,开发过程中会遇到各种各样的乱码问题。还好spring为咱们提供了一个编码过滤器,可以帮助咱们解决大部分的乱码问题,本以为配置上它就可以高枕无忧,但是还是出现了问题。编码问题浮现// 获取前台传递过来的查询条件 String conditions = request.getParameter("extra_search");从r

陌上花开,可缓缓归矣 9238

springMVC开发配置全局乱码过滤器characterEncodingFilter的流程

文章目录前言web.xml文件 前言 在使用springMVC时,post请求可能会形成中文乱码问题,此时需要在web.xml中配置此filter,保证已正确的格式获取数据。 web.xml文件 <?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001

weixin_45118229的博客 716
上一篇: <转载>springmvc(29):spring默认欢迎页设置
下一篇: <转载>springmvc(31):Spring中的 ContextLoaderListener
后台技术汇
博客等级 码龄11年 670粉丝 531原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

后台技术汇

对你的帮助,是对我的最好鼓励。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值