javaWeb_14(过滤器&监听器)

一、什么是过滤器:

过滤器实际上就是对Web资源进行拦截,做一些处理后再交给下一个过滤器或Servlet处理

二、过滤器  Filter

Filter配置有两种方式:

1、在Web.xml中配置过滤器

2、使用注解来配置过滤器   @WebFilter("/*")   建议使用这个

三、过滤器的作用:

统计人数、加载初始化信息、统计网站访问量....

package com.zking.filter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 权限过滤器 [鉴权]
 * @author zjjt
 *
 * @date 2022年4月23日 下午1:30:00
 */
@WebFilter("/*") //设置过滤的规则
public class RoleFilter implements Filter{
	
	//放我不需要过滤的路径
	List<String> paths=new ArrayList<String>();
	
	//将路径放到列表中
	{
		paths.add("/index.jsp");
		paths.add("/tourists.jsp");
		paths.add("/login.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/servlet02/index.jsp -->index.jsp
		String path = request.getServletPath();
		//判断你当前访问的路径是否需要过滤
		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
		
	}
	

	
	

}

四、什么是监听器:

Web监听器是一种能监听某个对象状态变化的组件,重点在于监听域对象

五、监听器 Listener

Listener配置有两种方式:

1、在Web.xml中配置监听器

2、使用注解来配置监听器   @WebListener   建议使用这个

 六、监听器的作用:

能够针对创建和销毁定义不同的行为 

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;

/**
 * 监听在线的人数变化
 * @author zjjt
 *
 * @date 2022年4月22日 下午9:09:26
 */

@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);
	}

}

注意:一定要在监听器的类上加上 WebListener ,否则该监听器不会被启动

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值