编写过滤器

导包

<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet.jsp.jstl/jstl-api -->
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl-api</artifactId>
<version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/taglibs/standard -->
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!--连接数据库-->
<dependency>
<groupId>com.github.spt-oss</groupId>
<artifactId>mysql-connector-java-plus</artifactId>
<version>0.1.0</version>
</dependency>
</dependencies>

编写过滤器

  1. 导包
    image
    2.重写三个方法
  • init(初始化,在服务器启动时过滤器就开始初始化)
  • doFilter(主要写这个)
servletRequest.setCharacterEncoding("utf-8");
servletResponse.setCharacterEncoding("utf-8");
servletResponse.setContentType("text/html;charset=UTF-8");
System.out.println("过滤之前");
filterChain.doFilter(servletRequest,servletResponse);
System.out.println("过滤之后");
  • destroy(销毁,在服务器关闭的时候过滤器就销毁)
  1. 在xml中配置filter文件
<filter>
<filter-name>filterDemo</filter-name>
<filter-class>com.Google.FilterDemo01</filter-class>
</filter>
<filter-mapping>
<filter-name>filterDemo</filter-name>
<url-pattern>/servlet/*</url-pattern>
</filter-mapping>
posted @   小罗要有出息  阅读(54)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
点击右上角即可分享
微信分享提示