Filter学习
作者:互联网
Filter: 过滤器,用来过滤网站的数据;(减少重复代码)
- 处理中文乱码
- 登录验证
Filter开发步骤
1.导包,实现filter接口,重写方法
2.编写过滤器
void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
servletRequest.setCharacterEncoding("utf-8");
servletResponse.setCharacterEncoding("utf-8");
servletResponse.setContentType("text/html;charset=UTF-8");
System.out.println("过滤器执行前...");
filterChain.doFilter(servletRequest,servletResponse);//让我们的请求继续走,如果不写servlet就被拦截
System.out.println("过滤器执行后...");
}3.在web.xml中配置Filter
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>com.ultraBlast.filter.CharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<!--只要是/servelt的任何请求,都会经过这个过滤器-->
<url-pattern>/servlet/*</url-pattern>
</filter-mapping>
标签:学习,System,Filter,servletResponse,过滤器,servletRequest,CharacterEncodingFilter 来源: https://www.cnblogs.com/UltraBlast/p/14399696.html