Filter过滤器
过滤器
概念
过滤器(Filter)是处于客户端与服务器⽬标资源之间的⼀道过滤技术。
- ⽣活中的过滤器:净⽔器,空⽓净化器
- web中的过滤器:当访问服务器的资源时,过滤器可以将请求拦截下来,完成⼀些特殊的功能。
过滤器作⽤
- 执⾏地位在Servlet之前,客户端发送请求时,会先经过Filter,再到达⽬标Servlet中;响应时,会根据执⾏流程再次反向执⾏Filter
- ⼀般⽤于完成通⽤的操作。如:登录验证、统⼀编码处理、敏感字符过滤…
编写过滤器
Servlet API中提供了⼀个Filter接⼝,开发⼈员编写⼀个Java类实现了这个接⼝即可,这个Java类称之为过滤器(Filter)
实现过程
- 编写Java类实现Filter接⼝
- 在doFilter⽅法中编写拦截逻辑
- 设置拦截路径
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
@WebFilter("/myservlet1")//过滤路径
public class MyFilter1 implements Filter {
//初始化过滤器
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("过滤器初始化了........init... "+filterConfig);
}
//执⾏过滤
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
System.out.println("过滤前........doFilter ");
//放⾏
chain.doFilter(request, response);
System.out.println("过滤后.......doFilter");
}
//销毁
@Override
public void destroy() {
System.out.println("销毁了.....destroy");
}
}
过滤器⽣命周期⽅法
- init:在服务器启动后,会创建Filter对象,然后调⽤init⽅法。只执⾏⼀次。⽤于加载资源
- doFilter:每⼀次请求被拦截资源时,会执⾏。执⾏多次
- destroy:在服务器关闭后,Filter对象被销毁。如果服务器是正常关闭,则会执⾏destroy⽅法。只执⾏⼀次。⽤于释放资源
过滤器配置
注解配置
在⾃定义的Filter类上使⽤注解@WebFilter(value=“/过滤⽬标资源”)
xml配置
<!--过滤器的xml配置 -->
<filter>
<!--名称-->
<filter-name>sf</filter-name>
<!--过滤器类全称-->
<filter-class>com.qf.web.filter.SecondFilter</filter-class>
</filter>
<!--映射路径配置-->
<filter-mapping>
<!--名称-->
<filter-name>sf</filter-name>
<!--过滤的url匹配规则和Servlet类似-->
<url-pattern>/*</url-pattern>
</filter-mapping>
过滤器路径
过滤器的过滤路径通常有三种形式:
精确过滤匹配 ,⽐如/index.jsp /myservlet1
后缀过滤匹配,⽐如*.jsp、*.html、*.jpg
通配符过滤匹配/*,表示拦截所有。注意过滤器不能使⽤/匹配。
/aaa/bbb/* 允许
//@WebFilter("/index.jsp")
//@WebFilter("/user/*")
//@WebFilter("*.jsp")
@WebFilter("/*")
public class FilterDemo4 implements Filter {
public void destroy() {
}
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws ServletException, IOException {
System.out.println("filterDemo4.....");
chain.doFilter(req, resp);
}
public void init(FilterConfig config) throws ServletException {
}
}
过滤器典型应⽤
过滤器解决编码
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
@WebFilter(value = "/*")
public class EncodingFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse
servletResponse, FilterChain filterChain) throws IOException,
ServletException {
//统⼀处理请求和响应的乱码
servletRequest.setCharacterEncoding("UTF-8");
servletResponse.setContentType("text/html;charset=utf-8");
filterChain.doFilter(servletRequest,servletResponse);
}
@Override
public void destroy() {
}
}
Listener:监听器
- 概念:web的三⼤组件之⼀。
- 事件监听机制
- 事件 :⼀件事情
- 事件源 :事件发⽣的地⽅
- 监听器 :⼀个对象
- 注册监听:将事件、事件源、监听器绑定在⼀起。 当事件源上发⽣某个事件后,执⾏监听器代码
- ServletContextListener:监听ServletContext对象的创建和销毁
- void contextDestroyed(ServletContextEvent sce) :ServletContext对象被销毁之前会调⽤该⽅法
- void contextInitialized(ServletContextEvent sce) :ServletContext对象创建后会调⽤该⽅法
- 步骤:
- 定义⼀个类,实现ServletContextListener接⼝
- 复写⽅法
- 配置
- web.xml
<!--配置监听器
-->
<listener>
<listener-class>listener.ContextLoaderListener</listener-class>
</listener>
<!--指定初始化的参数-->
<context-param>
<param-name>contextLoadConfig</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-
value>
</context-param>
- 注解:
@WebListener
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
/**
* 监听器
*/
public class ContextLoaderListener implements ServletContextListener {
/**
* 监听ServletContext对象的创建。ServletContext对象服务器启动后⾃动创建
* 在服务器启动后,⾃动调⽤
* @param servletContextEvent
*/
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
//加载资源
//1,获取ServletContext对象
ServletContext servletContext =
servletContextEvent.getServletContext();
//2,加载资源⽂件
String contextLoadConfig =
servletContext.getInitParameter("contextLoadConfig");
System.out.println("contextLoadConfig:"+contextLoadConfig);
System.out.println("ServletContext对象呗创建了。。。");
}
/**
* 在服务器关闭后,ServletContext对象被销毁。当服务器正常关闭后,该⽅法被调⽤
* @param servletContextEvent
*/
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
System.out.println("ServletContext对象呗销毁了。。。");
}
}