【web后端(十四)】jsp、servlet_监听器
2023-09-11 14:20:37 时间
监听器有几种类型,分别在一些服务器组件创建和销毁等时间节点要调用的接口,最常用的是ServletContextListener 。
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--这里没有名字的原因是因为,只要服务器请求的点上。他就会自动执行,不需要服务器去请求它。-->
<listener>
<listener-class>edu.xalead.listenter.ContextListener</listener-class>
</listener>
</web-app>
ContextListener
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import javax.servlet.http.HttpSessionBindingEvent;
//服务器开始的时候,
// 执行ServletContextListener的contextInitialized初始化方法。
//销毁的时候,执行contextDestroyed方法。
//对于ServletContextListener,服务器开启的时候初始化,关闭的时候销毁。
//我们可以将调度程序写到里头。
//实际上,不只是ServletContext有监听器,
//request、session等也有。
public class ContextListener implements ServletContextListener,
HttpSessionListener, HttpSessionAttributeListener {
// Public constructor is required by servlet spec
public ContextListener() {
}
// -------------------------------------------------------
// ServletContextListener implementation
// -------------------------------------------------------
public void contextInitialized(ServletContextEvent sce) {
System.out.println("服务器启动。。。。。。");
}
public void contextDestroyed(ServletContextEvent sce) {
/* This method is invoked when the Servlet Context
(the Web application) is undeployed or
Application Server shuts down.
*/
}
// -------------------------------------------------------
// HttpSessionListener implementation
// -------------------------------------------------------
public void sessionCreated(HttpSessionEvent se) {
/* Session is created. */
}
public void sessionDestroyed(HttpSessionEvent se) {
/* Session is destroyed. */
}
// -------------------------------------------------------
// HttpSessionAttributeListener implementation
// -------------------------------------------------------
public void attributeAdded(HttpSessionBindingEvent sbe) {
/* This method is called when an attribute
is added to a session.
*/
}
public void attributeRemoved(HttpSessionBindingEvent sbe) {
/* This method is called when an attribute
is removed from a session.
*/
}
public void attributeReplaced(HttpSessionBindingEvent sbe) {
/* This method is invoked when an attibute
is replaced in a session.
*/
}
}
相关文章
- 开发架构系列1-什么是 Nacos、Zipkin、工单、ZooKeeper、xxl-job、jenkins、 JSP、Servlet、EFK、Grafana、RDP、eclipse、ProM
- List多个字段标识过滤 IIS发布.net core mvc web站点 ASP.NET Core 实战:构建带有版本控制的 API 接口 ASP.NET Core 实战:使用 ASP.NET Core Web API 和 Vue.js 搭建前后端分离项目 Using AutoFac
- 适用于app.config与web.config的ConfigUtil读写工具类 基于MongoDb官方C#驱动封装MongoDbCsharpHelper类(CRUD类) 基于ASP.NET WEB API实现分布式数据访问中间层(提供对数据库的CRUD) C# 实现AOP 的几种常见方式
- Jsp中无法使用EL表达式的解决方法错误Can not find the tag library descriptor for http://java.sun.com/jsp/jstl/core
- Java+Servlet+JSP+Mysql+Tomcat实现Web学生选课管理系统
- 《Servlet、JSP和Spring MVC初学指南》——1.2 Servlet
- 基于Java(jsp+servlet)+Mysql实现的(Web)宿舍管理系统【100010103】
- 基于Java(JSP+Servlet)+Mysql实现的(Web)简易的工资管理系统【100010062】
- springmvc 中controller与jsp传值
- jsp访问web-inf下的spring配置文件
- jsp头部报错:The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
- SpringMVC访问WEB-INF下的jsp的方法
- 【IDEA】IDEA新建maven的web项目有pom.xml无Index.jsp
- Jsp web.xml 标准配置测试版
- 【web后端(二十)】jsp、servlet_内置标签——用户自定义标签
- 【web后端(八)】jsp、servlet_会话管理_cookie
- 【web后端(二)】jsp、servlet_doPost_处理表单
- Jsp在Web.xml中的配置
- 在MyEclipse中改动jsp页面的默认打开方式
- jsp urlrewrite 中正則表達式不包括某个字符串写法
- Web 基础 之 Eclipse 中如何把新建的 jsp 文件默认的编码格式(ISO-8859-1)修改为 UTF-8,避免中文乱码
- Web 之 Eclipse 搭建 Gradle 环境/并且运行 Gradle 工程(spring web)简单整理
- 第一个JSP程序
- html(jsp)登录页面