导包
<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>
编写过滤器
- 导包
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(销毁,在服务器关闭的时候过滤器就销毁)
- 在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>