监听器 :Listener
ServletContextListener
HttpSessionListener
ServletRequestListener
一定要在监听器的类上加上 WebListener ,否则该监听器不会被启用:
@WebListener //配置监听器
ServletContextListener
@Override
public void contextInitialized(ServletContextEvent sce) {
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
}
HttpSessionListener:
@Override
public void sessionCreated(HttpSessionEvent se) {
}
@Override
public void sessionDestroyed(HttpSessionEvent se) {
}
代码如下:
package com.zking.Listener;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import javafx.application.Application;
/**
* 监听在线的人数变化
* @author Janet
*
* @date 2022年4月23日下午4:07:18
*/
@SuppressWarnings("all")
@WebListener //配置监听器
public class OnLineListener implements ServletContextListener, HttpSessionListener {
ServletContext application;//全局对象
@Override
public void contextInitialized(ServletContextEvent sce) {
//application被创建了
System.out.println("服务启动了");
//将程序的上下文赋值给了全局对象
application = sce.getServletContext();
//项目启动的时候 把人数设置为0
application.setAttribute("onLineCount",0);
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
//application被销毁了
System.out.println("服务关闭了");
}
@Override
public void sessionCreated(HttpSessionEvent se) {
//只要该项目的页面被访问了
//获取application中存放的人数
Integer count = (Integer)application.getAttribute("onLineCount");
//人数+1,设置人数
application.setAttribute("onLineCount",++count);
System.out.println("有人进来了,人数:"+count);
}
@Override
public void sessionDestroyed(HttpSessionEvent se) {
//1.存活时间 ttl到期了
//2.手动销毁 req.getSession().invalidate();
//获取application中存放的人数
Integer count = (Integer)application.getAttribute("onLineCount");
//人数+1,设置人数
application.setAttribute("onLineCount",--count);
System.out.println("有人出去了,人数:"+count);
}
}
过滤器:Filter
精准匹配 /a.jsp
扩展名匹配 *.jsp
路径匹配 /manager/*
匹配所有 /*
@WebFilter("/*") //设置过滤的规则
//让过滤器放行
chain.doFilter(req,resp);
代码如下:
package com.zking.filter;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@SuppressWarnings("all")
@WebFilter("/*") //设置过滤的规则
public class RoleFilter implements Filter {
//放我不需要过滤的路径
List<String> paths=new ArrayList<String>();
//将路径放到列表中
{
paths.add("/index.jsp");
paths.add("/tourists.jsp");
paths.add("/login.do");
paths.add("/exit.do");
}
@Override
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
//过滤器的所有操作全部在这里完成
HttpServletRequest request=(HttpServletRequest)req;
HttpServletResponse response=(HttpServletResponse)resp;
//获取当前请求的路径
// http://localhost:8080/web21/index.jsp -> /index.jsp
// http://localhost:8080/web21/exit.do -> /exit.do
String path = request.getServletPath();
//判断你当前访问的路径是否需要过滤 /index.jsp
boolean f=false;
for (String p : paths) {
if(p.equals(path)){
f=true;
break;
}
}
if(f){//当你的访问路径在列表中 我是不需要过滤的
//让过滤器放行
chain.doFilter(req,resp);
return;//终止代码运行
}
//isLogin是在登录之后被放到session里面去
Object isLogin = request.getSession().getAttribute("isLogin");
if(isLogin == null){//没有登录
//回去首页
response.sendRedirect("index.jsp");
return;
}
//让过滤器放行
chain.doFilter(req,resp);
}
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}