1.监听器分为3种:
application监听器
session监听器
request监听器
2.
1.application监听器
实现:ServletContextListener (常用)
重写:
//容器启动时调用
public void contextInitialized(ServletContextEvent event){
}
//容器消毁时调用
public void contextDestroyed(ServletContextEvent event){
}
2.2
Session监听器
实现:HttpSessionListener (偶尔用)
重写:
//session创建时调用
public void sessionCreated(HttpSessionEvent event){
}
//session销毁时调用
public void sessionDestroyed(HttpSessionEvent event){
}
2.3
request监听器
实现:ServletRequestListener (不用,性能差)
重写:
//请求开始时调用
public requestInitialized(ServletRequestEvent event){
}
//请求结束时调用
public requestDestroyed(ServletRequestEvent event){
}
3实例:监听在线人数
public class AppListrener implements ServletContextListener,HttpSessionListener {
ServletContext app=null;
//容器销毁时调用
public void contextDestroyed(ServletContextEvent sce) {
// TODO Auto-generated method stub
System.out.println("容器销毁了");
}
//容器初始化时调用
public void contextInitialized(ServletContextEvent sce) {
// TODO Auto-generated method stub
System.out.println("容器初始化了");
app=sce.getServletContext();
Integer count=0;
app.setAttribute("cut", count);
System.out.println(count+"___");
}
//session创建时调用
public void sessionCreated(HttpSessionEvent arg0) {
// TODO Auto-generated method stub
System.out.println("session创建时调用");
Integer count=(Integer)app.getAttribute("cut");
if(count==null){
count=0;
}
count++;
System.out.println("创建时:"+count);
app.setAttribute("cut", count);
}
//session销毁时调用
public void sessionDestroyed(HttpSessionEvent arg0) {
// TODO Auto-generated method stub
System.out.println("session销毁时调用");
Integer count=(Integer)app.getAttribute("cut");
if(count==null){
count=0;
}
count--;
System.out.println("注销时:"+count);
app.setAttribute("cut", count);
}
4.过滤器(filter)
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;
/**
* @author harrison
* @version v1.0
* @create 2022-04-22@星期五
* @Description 权限过滤器 [鉴权]
**/
@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);
}
}