一、创建MyFilter03
public class MyFilter03 implements Filter {
private String characterEncoding;
FilterConfig fc;
public void init(FilterConfig fConfig) throws ServletException {
// 获取FilterConfig对象
this.fc = fConfig;
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// 输出参数信息
characterEncoding=fc.getInitParameter("encoding");
System.out.println("encoding初始化参数的值为:"+characterEncoding);
chain.doFilter(request, response);
}
public void destroy() {
}
}
二、配置filter,并放在web.xml最前面
<filter>
<filter-name>MyFilter03</filter-name>
<filter-class>cn.itcast.chapter08.filter.MyFilter03</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>MyFilter03</filter-name>
<url-pattern>/MyServlet</url-pattern>
</filter-mapping>
注解开发的话,这么设置初始化参数
@WebFilter(value= "/MyServlet",initParams={@WebInitParam(name="encoding",value="GBk")})