Filter 笔记记录
- 1. Filter 过滤器
- 2. Filter介绍
- 3. FilterChain
- 3. Filter 过滤器的使用
- 4. 过滤器使用细节
- 5. Filter的声明周期
- 6. FilterConfig
- 7. 过滤器五种拦截行为
1. Filter 过滤器
2. Filter介绍
3. FilterChain
3. Filter 过滤器的使用
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/demo1")
public class ServletDemo01 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("demo1 执行了");
resp.getWriter().write("demo1执行了");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doGet(req, resp);
}
}
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
/**
* Filter基本使用
* @WebFilter("/*") 代表所有都会拦截
*/
@WebFilter("/*")
public class FilterDemo01 implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse resp, FilterChain filterChain) throws IOException, ServletException {
System.out.println("filter01 执行了");
//处理乱码
resp.setContentType("text/html;charset=UTF-8");
//放行
filterChain.doFilter(request,resp);
}
}
4. 过滤器使用细节
在web.xml中配置的话 是从上到下在Filterchain中执行,使用注解可以一一对应
5. Filter的声明周期
服务器加载时就创建了,当应用卸载时和服务器停止时执行销毁
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
/**
* Filter 生命周期
*/
@WebFilter("/*")
public class FilterDemo03 implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("filter 初始化成功");
}
@Override
public void doFilter(ServletRequest request, ServletResponse resp, FilterChain filterChain) throws IOException, ServletException {
System.out.println("filter03 执行了");
//处理乱码
resp.setContentType("text/html;charset=UTF-8");
//放行
filterChain.doFilter(request,resp);
}
@Override
public void destroy() {
System.out.println("filter 销毁了");
}
}
6. FilterConfig
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
/**
* Filter 过滤器配置对象的使用
*/
//@WebFilter("/*")
public class FilterDemo04 implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("filter 初始化成功");
//获取过滤器名字
String filterName = filterConfig.getFilterName();
System.out.println(filterName);
// 根据名称获取value
String filter = filterConfig.getInitParameter("filter");
System.out.println(filter);
}
@Override
public void doFilter(ServletRequest request, ServletResponse resp, FilterChain filterChain) throws IOException, ServletException {
System.out.println("filter03 执行了");
//处理乱码
resp.setContentType("text/html;charset=UTF-8");
//放行
filterChain.doFilter(request,resp);
}
@Override
public void destroy() {
System.out.println("filter 销毁了");
}
}
7. 过滤器五种拦截行为
<?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">
<filter>
<filter-name>filter05</filter-name>
<filter-class>com.example.Filter.FilterDemo05</filter-class>
<async-supported>true</async-supported>
<!-- <init-param>-->
<!-- <param-name>filter</param-name>-->
<!-- <param-value>filter param value</param-value>-->
<!-- </init-param>-->
<!-- 配置开启异步支持,当dispatcher配置ASYN时,需要配置此行-->
</filter>
<filter-mapping>
<filter-name>filter05</filter-name>
<url-pattern>/error.jsp</url-pattern>
<!-- 过滤请求:默认值-->
<dispatcher>REQUEST</dispatcher>
<!-- 过滤全局错误页面,当由服务器调用全局错误页面时,过滤器工作-->
<!-- 不配置全局配置 不会执行 filter05过滤器调用-->
<dispatcher>ERROR</dispatcher>
<!-- 过滤请求转发 当请求转发时 过滤器工作-->
<!-- 不配置全局配置 不会执行过滤器-->
<dispatcher>FORWARD</dispatcher>
<!-- 不配置全局 不会执行过滤器-->
<!-- 过滤请求包含 当请求包含时,过滤器工作。它只能过滤动态包含,jsp的include指令是静态包含,过滤器不会-->
<dispatcher>INCLUDE</dispatcher>
<!-- 过滤异步类型,它要求我们filter标签中配置开启异步支持-->
<dispatcher>ASYNC</dispatcher>
</filter-mapping>
<!-- 配置全局错误页面-->
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/error.jsp</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/error.jsp</location>
</error-page>
</web-app>