Javaweb开发核心知识之Listener监听器
Javaweb开发核心之初识Listener监听器
讲解Servlet里面的监听器Listener
什么是监听器
- 监听器
- 监听器是一个实现了特定接口的普通ava类,用于监听其他对象的创建和销毁,监听其他对象的方法执行和属性改变
javaweb里面的监听器:
- 作用
- 监听域对象的创建和销毁,比如request/session/context
- 分类
- ServletContextLitener
- HttpSessionListener
- ServletRequestListener
监听器的实现步骤
- 创建一个普通Java类
- 让该类实现监听器的接口
- 在该类中实现监听器接口的所有方法
- 旧版的在web.xml文件中通过标签来配置监听器,新版使用@WebListener
监听器实战ServletContextListener全局配置加载
简介:实战自定义ServletContext监听器
使用场景
web.xm配置
<context-param>
<param-name>ur1</param-name>
<param-value>https://xdclass.net</param-value>
</context-param>
<context-param>
<param-name>topic</param-name>
<param-value>你好世界</param-value>
</context-param>
监听器开发
@WebListener
public class ContextListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("ContextListener contextInitialized");
ServletContext servletContext = sce.getServletContext();
String url = servletContext.getInitParameter("url");
String topic = servletContext.getInitParameter("topic");
Config config = new Config();
config.setTopic(topic);
config.setUrl(url);
servletContext.setAttribute("config",config);
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("ContextListener contextDestroyed");
}
}
监听器实战HttpSessionListener统计当前在线人数
简介:java监听器实战之HttpSessionListener统计当前在线人数
session使用场景
- 和session相关操作,比如统计网站在线人数,当前服务器的负载情况等
ContextLisener配置
ServletContext sc = sce.getServletcontext();
sc.setAttribute("onlineNum",0);
HttpSessionListener开发
@WebListener
public class SessionListener implements HttpSessionListener {
@Override
public void sessionCreated(HttpSessionEvent se) {
System.out.println("SessionListener sessionCreated");
ServletContext servletContext =
se.getSession().getServletContext();
Integer onlineNum =
(Integer) servletContext.getAttribute("onlineNum");
servletContext.setAttribute("onlineNum", ++onlineNum);
}
@Override
public void sessionDestroyed(HttpSessionEvent se) {
System.out.println("SessionListener sessionDestroyed");
ServletContext servletContext =
se.getSession().getServletContext();
Integer onlineNum =
(Integer) servletContext.getAttribute("onlineNum");
servletContext.setAttribute("onlineNum", --onlineNum);
}
}
add.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>javaweb统计在线人数</title>
</head>
<body>
<hr>
近30分钟在线⼈人数: ${applicationScope.onlineNum}
</body>
</html>
delete.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>javaweb统计在线人数</title>
</head>
<body>
销毁session
<hr>
<% request.getSession().invalidate(); %>
</body>
</html>
注意
- 关闭启动tomcat自动打开浏览器,因为会触发会触发多个session
- 使用多个浏览器测试粗略统计,如果是多机器分布式情况,需要用到分布式缓存
监听器实战ServletRequestListener统计网站请求量
ServletRequestListener统计网站请求量
ContextLisener配置
ServletContext sc = sce.getServletContext();
sc.setAttribute("onlineNum",0);
sc.setAttribute("totalVisit",0);
RequestListener开发
@WebListener
public class SessionListener implements HttpSessionListener {
@Override
public void sessionCreated(HttpSessionEvent se) {
System.out.println("SessionListener sessionCreated");
ServletContext servletContext =
se.getSession().getServletContext();
Integer onlineNum =
(Integer) servletContext.getAttribute("onlineNum");
servletContext.setAttribute("onlineNum", ++onlineNum);
}
@Override
public void sessionDestroyed(HttpSessionEvent se) {
System.out.println("SessionListener sessionDestroyed");
ServletContext servletContext =
se.getSession().getServletContext();
Integer onlineNum =
(Integer) servletContext.getAttribute("onlineNum");
servletContext.setAttribute("onlineNum", --onlineNum);
}
}
add.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>javaweb统计在线人数</title>
</head>
<body>
<hr>
近30分钟在线人数: ${applicationScope.onlineNum}
<hr>
应⽤服务器启动后总访问次数: ${totalVisit}
</body>
</html>