过滤器Filter
Filter:过滤器,用来过滤网站的数据。
- 处理中文乱码
- 登陆验证
路径
依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>JavaWeb</artifactId>
<groupId>com.huangdekai</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>0008_filter</artifactId>
<dependencies>
<!-- Servlet的依赖 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<!-- JSP依赖 -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
</dependency>
<!-- JSTL表达式的依赖 -->
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl-api</artifactId>
<version>1.2</version>
</dependency>
<!-- standard标签库 -->
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!-- 连接数据库 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
</dependencies>
</project>
代码
- CharacterEncodingFilter
package com.huangdekai.JavaWeb.filter;
import javax.servlet.*;
import java.io.IOException;
/**
* @Autord: HuangDekai
* @Date: 2020/4/25 1:34
* @Version: 1.0
* @since: jdk11
*/
public class CharacterEncodingFilter implements Filter {
// 初始化 Web服务器启动,就已经初始化了,等待过滤对象出现
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("CharaterEncodingFilter已经初始化了");
}
// chain:链
/*
* 1.过滤器中的所有代码,在过滤特定请求的时候都会执行
* 2.必须要让过滤器继续进行
* */
public 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("CharacterEncodingFilter执行前...");
filterChain.doFilter(servletRequest, servletResponse);//让请求继续走,如果不写,请求在这里就被拦截停止了
System.out.println("CharacterEncdoingFilter执行后...");
}
// 销毁 web服务器关闭,filter销毁
public void destroy() {
System.out.println("CharaterEncodingFilter已经销毁了");
}
}
CharacterEncdoingFilter是过滤器,很显然,功能就是处理中文乱码的问题。注意实现的是javax.servlet.Filter:
- ShowServlet:
package com.huangdekai.JavaWeb.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @Autord: HuangDekai
* @Date: 2020/4/25 1:48
* @Version: 1.0
* @since: jdk11
*/
public class ShowServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().write("你好啊,世界");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>ShowServlet</servlet-name>
<servlet-class>com.huangdekai.JavaWeb.servlet.ShowServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ShowServlet</servlet-name>
<url-pattern>/servlet/show</url-pattern>
<url-pattern>/show</url-pattern>
</servlet-mapping>
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>com.huangdekai.JavaWeb.filter.CharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<!-- /servlet/* 的意思是只要是/servlet的任何请求,都会经过这个过滤器 -->
<url-pattern>/servlet/*</url-pattern>
</filter-mapping>
</web-app>
配置好之后,启动Tomcat,可以在IDEA中发现这段:
可以修改为英文 查看是否是init的内容。这就是过滤器在web服务器的启动时就初始化的证明。
访问页面
在上面的Web.xml中设置了两个地址,都可以访问到同一个Servlet:
而过滤器设置过滤的是/servlet/地址下的所有页面(指的是servlet设置的地址):
因此分别访问/servlet/show和/show一个能经过过滤器,一个不经过过滤器: