栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

监听器 Listener&&过滤器 Filter

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

监听器 Listener&&过滤器 Filter

监听器 Listener
ServletContextListener  上下文监听器 HttpSessionListener       会话监听器 ServletRequestListener    请求监听器 一定要在监听器的类上加上 WebListener ,否则该监听器不会被启用
1.ServletContextListener
@Override public void contextInitialized(ServletContextEvent sce) { } @Override public void contextDestroyed(ServletContextEvent sce) { }
2.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;



@WebListener  //配置监听器
public class OnLineListener implements ServletContextListener,HttpSessionListener{

	ServletContext application;//全局变量
	
	@Override
	public void contextDestroyed(ServletContextEvent sce) {
		//application 被销毁了
		System.out.println("服务关闭了");
		 
	}

	@Override
	public void contextInitialized(ServletContextEvent sce) {
		
		//application 被创建了
		System.out.println("服务启动了");
		application= sce.getServletContext();
		//服务启动的时候 把人数设置为0
		application.setAttribute("onLineCount",0);
	}

	@Override
	public void sessionCreated(HttpSessionEvent arg0) {
		//只要该项目被访问了
		//获取人数
		Integer count=(Integer)application.getAttribute("onLineCount");
		//人数+1,设置人数
		application.setAttribute("onLineCount", ++count);
		System.out.println("进来了,人数:"+count);
	}

	@Override
	public void sessionDestroyed(HttpSessionEvent arg0) {
		//1.存活事件 ttl 到期了
		//2.手动销毁 req.getSession().invalidate();
		//获取人数											
		Integer count =(Integer)application.getAttribute("onLineCount");
		//人数-1,设置人数
		application.setAttribute("onLineCount",--count);
		System.out.println("出去了,人数:"+count);
	}

}
过滤器 Filter
精准匹配 /a.jsp 扩展名匹配 *.jsp 路径匹配 /manager @WebFilter("/*") //设置过滤的规则 public class RoleFilter implements Filter{ List paths=new ArrayList<>(); { paths.add("/index.jsp"); paths.add("/tourists.jsp"); paths.add("/login.do"); } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { //过滤器的所有操作全部在这里完成 HttpServletRequest req=(HttpServletRequest)request; HttpServletResponse resp=(HttpServletResponse)response; //获取当前请求的路径 //拿的是路径最后一个/后面的路径 String path=req.getServletPath(); //判断当前访问的路劲是否需要过滤 boolean f=false; for (String p : paths) { if(p.equals(path)) { f=true; break; } } if(f==true) {//如果当前访问路劲在列表中 就不需要过滤 //让过滤器放行 chain.doFilter(request, response); return;//终止代码运行 } Object isLogin = req.getSession().getAttribute("isLogin"); if(isLogin==null) { //回首页 resp.sendRedirect("index.jsp"); return; } //让过滤器放行 chain.doFilter(request, response); } @Override public void init(FilterConfig arg0) throws ServletException { } @Override public void destroy() { } }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/830659.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号