web.xml 字符过滤器

本文介绍了一个Spring框架中的字符编码过滤器,该过滤器允许开发者为请求指定字符编码,以解决浏览器通常不设置字符编码的问题。文章详细展示了如何配置web.xml中的CharacterEncodingFilter,并解释了其工作原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

org.springframework.web.filter.CharacterEncodingFilter
web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
 version="2.4">

    <filter>
      <filter-name>CharacterEncodingFilter</filter-name>
      <filter-class>
           org.springframework.web.filter.CharacterEncodingFilter
      </filter-class>
      init-param>
       <param-name>encoding</param-name>
       <param-value>GB2312</param-value>
      </init-param>
      <init-param>
       <param-name>forceEncoding</param-name>
       <param-value>true</param-value>
      </init-param>
     </filter>
<!-- 要过滤得类型 -->
  <filter-mapping>
  <filter-name>SetCharacterEncoding</filter-name>
  <url-pattern>*.jsp</url-pattern>
  </filter-mapping>

</web-app>
public class CharacterEncodingFilter
extends OncePerRequestFilter
 
 

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值