当前位置:网站首页>Servlet监听器&过滤器介绍
Servlet监听器&过滤器介绍
2022-04-23 12:49:00 【追梦梓辰】
1:监听器 Listener
需要看Servlet的可以看我的上一篇文章,里面有详细的介绍
Listener介绍:
监听器就是监听某个对象的状态变化
事件源:被监听的三个域对象request,session,servletcontext
监听器的编写步骤:
1:编写一个监听器类去实现监听器接口
使用注解来配置:@WebListener
还有一种配置方法就是用web.xml来配置,不过比较麻烦,个人喜欢用注解
package com.zking.lister;
import javax.servlet.Servlet;
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 JIE * * @date 2022年4月22日 下午8:21:42 */
@WebListener //注解方式
public class onLineListenr implements ServletContextListener,HttpSessionListener {
ServletContext applicaton;
@Override
public void contextDestroyed(ServletContextEvent sce) {
// application被销毁了
System.out.println("服务销毁了");
}
@Override
public void contextInitialized(ServletContextEvent sce) {
//application被创建了
System.out.println("服务创建了");
applicaton = sce.getServletContext();
applicaton.setAttribute("onlineCount", 0);
}
@Override
public void sessionCreated(HttpSessionEvent arg0) {
// 主要该项目的页面被访问了
//获取人数
Integer count = (Integer)applicaton.getAttribute("onlineCount");
//人数+1
//设置人数
applicaton.setAttribute("onlineCount", ++count);
System.out.println("有人进来了");
}
@Override
public void sessionDestroyed(HttpSessionEvent arg0) {
// 1.存活时间到期了
//2.手动销毁
//获取人数
Integer count = (Integer)applicaton.getAttribute("onlineCount");
//人数-1
//设置人数
applicaton.setAttribute("onlineCount", --count);
System.out.println("有人出去了");
}
}
2:过滤器
过滤器介绍:
在Servlet中,我们需要Filter来进行信息的对比过滤,比如在项目中,我们可以通过过滤器来限制游客的功能。
优点:
过滤器的作用描述:在做项目时,通常会分为几个包,游客的界面放在游客包里面,
用户的界面放在用户包里面,管理员的界面放在管理员包里面。
所以我们在写项目时就可以用过滤器的路径匹配来判定,这样的效率会大大增加
Filter的配置:
有两种方法:
1:通过web.xml来配置过滤器
2:通过注解来配置过滤器(个人推荐)
第一种方式比较麻烦,第二种方式比较简单,个人喜欢比较简单的
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 JIE * * @date 2022年4月22日 下午8:50:11 * */
@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 destroy() {
// TODO Auto-generated method stub
}
@Override
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
throws IOException, ServletException {
// 过滤器的所有操作全部在这里完成
HttpServletRequest request =(HttpServletRequest)req;
HttpServletResponse response =(HttpServletResponse)resp;
//获取当前请求的路径
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;
}
Object isLogin= request.getSession().getAttribute("isLogin");
if(isLogin==null) {
response.sendRedirect("index.jsp");
return;
}
}
@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}
Java开发手册(开发准则):https://pan.baidu.com/s/18cNu2W5p9EumiBZW_yxWIA
提取码:a4cb
版权声明
本文为[追梦梓辰]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_63719049/article/details/124357596
边栏推荐
- 网站首页文件被攻击篡改的形式有哪些
- C, calculation code of parameter points of two-dimensional Bezier curve
- 21 天学习MongoDB笔记
- STM32CubeProgrammer基础使用说明
- 有趣的IDEA插件推荐,给你的开发工作增添色彩
- Introduction to metalama 4 Use fabric to manipulate items or namespaces
- BUUCTF WEB [BUUCTF 2018]Online Tool
- Buuctf Web [gxyctf2019] no dolls
- 8 websites that should be known for product development to enhance work experience
- Plato Farm-以柏拉图为目标的农场元宇宙游戏
猜你喜欢
[vulnhub range] - DC2
Realize several "Postures" in which a box is horizontally and vertically centered in the parent box
QT draw image
如何防止网站被黑客入侵篡改
天梯赛赛前练习
Object. The disorder of key value array after keys
mysql中 innoDB执行过程分析
云原生KubeSphere部署Mysql
I changed to a programmer at the age of 31. Now I'm 34. Let me talk about my experience and some feelings
No idle servers? Import OVF image to quickly experience smartx super fusion community version
随机推荐
基于卷积神经网络的遥感影像分类识别系统
C, calculation code of parameter points of two-dimensional Bezier curve
Pre competition practice of TIANTI competition
SSM framework series - annotation development day2-2
Uni app native app cloud packaging integrated Aurora push (jg-jpush) detailed tutorial
Luogu p5540 [balkanoi2011] timeismoney | minimum product spanning tree problem solution
Qt绘制图像
Source code analysis of synchronousqueue
Realize several "Postures" in which a box is horizontally and vertically centered in the parent box
21 days learning mongodb notes
PHP generates JSON to process Chinese
mysql中 innoDB执行过程分析
Web17——EL与JSTL的使用
How to prevent the website from being hacked and tampered with
flask项目跨域拦截处理以及dbm数据库学习【包头文创网站开发】
软件测试周刊(第68期):解决棘手问题的最上乘方法是:静观其变,顺水推舟。
Recommended website for drawing result map
box-sizing
I changed to a programmer at the age of 31. Now I'm 34. Let me talk about my experience and some feelings
只是不断地建构平台,不断地收拢流量,并不能够做好产业互联网