目录
1、Filter概述
1.1、什么是Filter
1.2、Filter原理
1.3、过滤器快速入门
定义一个类来实现Filter接口,并重写init、doFilter、destroy三个方法。
public class MyFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("init");
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("doFilter");
}
@Override
public void destroy() {
System.out.println("destroy");
}
}
在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">
<!--配置过滤器-->
<filter>
<filter-name>MyFilter</filter-name>
<!--filter-class书写过滤器类路径-->
<filter-class>cn.itssl.filter.MyFilter</filter-class>
</filter>
<!--映射-->
<filter-mapping>
<filter-name>MyFilter</filter-name>
<!--拦截页面-->
<url-pattern>/hello.html</url-pattern>
</filter-mapping>
</web-app>
1.4、Filter的生命周期
init方法
doFilter方法
destroy方法
Filter生命周期和Servlet生命周期对比:
1.5、拦截路径
精准匹配
目录匹配
后缀匹配
匹配所有
<filter>
<filter-name>MyFilter</filter-name>
<filter-class>cn.itssl.web.MyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>MyFilter</filter-name>
<!--指定拦截-->
<url-pattern>/hello.html</url-pattern>
<url-pattern>/UserServlet</url-pattern>
<url-pattern>/show.jsp</url-pattern>
<!--匹配后缀-->
<url-pattern>*.html</url-pattern>
<!--拦截所有请求路径-->
<!--<url-pattern>/*</url-pattern>-->
</filter-mapping>
1.6、doFilter方法
chain.doFilter(request,response) 放行、执行后续组件。
package cn.itssl.web;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @Auther: itssl
* @Date: 12:07
* @Desc
*/
public class MyFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("--init--");
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("--doFilter--");
//默认不放行
//我不仅要拦截还要放行
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
//获取资源的请求路径
String path = request.getRequestURI().substring(request.getContextPath().length());
//判断请求路径是否满足
if("/hello.html".equals(path) || "/userServlet".equals(path)){
//放行实现
filterChain.doFilter(request,response);
}
}
@Override
public void destroy() {
System.out.println("--destroy--");
}
}
1.7、使用注解形式
使用注解形式,可以省去配置web.xml的过程,直接在类上面写@WebFilter注解,里面书写拦截的路径。
@WebFilter(urlPatterns = "/*")
public class MyFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
}
@Override
public void destroy() {
}
}
2、Listener
2.1、什么是Listener
使用场景:
2.2、监听器的作用
2.3、Servlet监听器
2.4、Listener快速入门
首先需要定义一个类来实现ServletContextListener监听器
public class MyServletContextListener implements ServletContextListener{
// Public constructor is required by servlet spec
public MyServletContextListener() {
}
public void contextInitialized(ServletContextEvent sce) {
System.out.println("ServletContextListener创建了...........");
}
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("ServletContextListener销毁了...........");
}
}
配置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_3_1.xsd"
version="3.1">
<listener>
<listener-class>com.itssl.listener.MyServletContextListener</listener-class>
</listener>
</web-app>
2.5、案例——统计网站在线人数
有用户使用网站,在线人数就+1;用户退出网站,在线人数就-1
定义一个类用来监听整个项目,也是整个域,实现ServletContextListener接口
@WebListener
public class InitNumberListener implements ServletContextListener {
public InitNumberListener() {
}
public void contextInitialized(ServletContextEvent sce) {
ServletContext servletContext = sce.getServletContext();
servletContext.setAttribute("number", 0);
}
public void contextDestroyed(ServletContextEvent sce) {
}
}
定义一个类实现HttpSeesionListener接口,用来监听session域的创建和销毁
@WebListener
public class NumberChangeListener implements HttpSessionListener {
@Override
public void sessionCreated(HttpSessionEvent httpSessionEvent) {
ServletContext servletContext = httpSessionEvent.getSession().getServletContext();
int number = (int) servletContext.getAttribute("number");
servletContext.setAttribute("number",number+1);
}
@Override
public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
ServletContext servletContext = httpSessionEvent.getSession().getServletContext();
int number = (int) servletContext.getAttribute("number");
servletContext.setAttribute("number",number-1);
}
}
定义一个类,用来销毁session域,退出网页功能,让人数-1
@WebServlet("/logoutServlet")
public class LogoutServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 处理post请求乱码问题
request.setCharacterEncoding("utf-8");
// 设置响应数据的编码
response.setContentType("text/html;charset=utf-8");
// 销毁
request.getSession().invalidate();
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
在线人数: ${number}
<br>
<a href="/logoutServlet">退出系统</a>>
</body>
</html>