1 创建一个包 com.aaa.filter
2 在包中创建一个类 实现 Filter接口 和 三个实现方法
3 在 doFilter中
@Override
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain filterChain) throws IOException, ServletException {
//0 设置编码方式
req.setCharacterEncoding("UTF-8");
filterChain.doFilter(req,resp);//放行
}
4 在 web.xml配置
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>com.aaa.filter.CharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<!-- filter过滤器 url-pattern 对哪些请求进行过滤
/* 所有请求都过滤
/emp/* 过滤 emp下所有请求 http://localhost:8080/shop/login/userLogin http://localhost:8080/shop/emp/oreder http://localhost:8080/shop/emp/xxx
*.do 过滤所有以.do结尾的请求
-->
<url-pattern>/*</url-pattern>
</filter-mapping>