当前位置: 首页 > news >正文

JavaWeb05

一、Listener监听器

1、简介

Listener是Servlet规范中的一员

在Servlet中,所有的监听器接口都是以Listener结尾

监听器实际上是Servlet规范留给JavaWeb程序员的一些特殊时机

当在某些时机需要执行一段Java代码时,可以用对应的监听器

2、常用的监听器接口

(1)jakarta.servlet 包下:

ServletContextListener、ServletContextAttributeListener

ServletRequestListener、ServletRequestAttributeListener

(2)jakarta.servlet.http 包下:

HttpSessionListener

HttpSessionAttributeListener、HttpSessionBindingListener

HttpSessionIdListener、HttpSessionActivationListener

3、实现一个监听器的步骤

(1)以ServletContextListener为例

编写一个类实现ServletContextListener接口

这个监听器监听的是ServletContext对象的创建和销毁

监听器中的方法不需要程序员调用,在特定事件发生时由服务器调用

@WebListener
public class MyServletContextListener implements ServletContextListener {@overridepublic void contextInitialized(ServletContextEvent sce) {// 这个方法在ServletContext对象被创建时调用    }@overridepublic void contextDestroyed(ServletContextEvent sce) {// 这个方法在ServletContext对象被销毁时调用    }
}

(2)在web.xml文件中配置这个监听器

也可以使用 @WebListener 注解

<listener><listener-class>自己实现的监听器类的全类名</listener-class>
</listener>

4、其他监听器

(1)XxxxAttributeListener

监听的是某个域中的attribute被增加、修改、删除

只要域中的数据发生变化,就执行相应的方法

(2)XxxxBindingListener

例如,一个JavaBean实体类实现了HttpSessionBindingListener接口

那么当这个实体类的对象被放入session的attribute中触发bind事件,移除触发unbind事件

这个实体类不需要使用 @WebListener注解

(3)HttpSessionIdListener

监听Session对象的Id,当Id改变时调用类中的唯一的方法

(4)HttpSessionActivationListener

监听Session对象的钝化和活化

钝化:session对象从内存中存储到硬盘文件

活化:session对象从硬盘文件中恢复到内存

二、MVC架构模式

1、简介以及示意图

2、JDBC工具类的封装

public class DBUtil {private static ResourceBundle bundle = ResourceBundle.getBundle("resources/jdbc");private static String driver = bundle.getString("driver");private static String url = bundle.getString("url");private static String user = bundle.getString("user");private static String password = bundle.getString("password");// 工具类的所有方法都是静态的// 将构造方法私有化,防止创建对象private DBUtil() {}static {try {Class.forName(driver);                    } catch (ClassNotFoundException e) {e.printStackTrace();        }}private static ThreadLocal<Connection> local = new ThreadLocal<>();// 没有使用数据库连接池,直接创建连接对象public static Connection getConnection() throws SQLException {Connection conn = local.get();if (conn == null) {conn = DriverManager.getConnection(url, user, password);        local.set(conn);}return conn;}public static void close(Connection conn, Statement stmt, ResultSet rs) {if (conn != null) {try {conn.close(); local.remove();           } catch (SQLException e) {throw new RuntimeException(e);        }}if (stmt != null) {try {stmt.close();            } catch (SQLException e) {throw new RuntimeException(e);        }}if (rs != null) {try {rs.close();            } catch (SQLException e) {throw new RuntimeException(e);        }}    }}

3、MVC架构模式与三层架构的关系

三层架构:表现层、业务逻辑层、持久化层

表现层对应V和C

M包括了业务逻辑层和持久化层


文章转载自:

http://B7acCTGN.cpgdy.cn
http://z944T80A.cpgdy.cn
http://biIlJuT2.cpgdy.cn
http://vEhCxo5D.cpgdy.cn
http://2R9aIe6E.cpgdy.cn
http://q2y7vdZx.cpgdy.cn
http://2SlhxveR.cpgdy.cn
http://Pke4sNzZ.cpgdy.cn
http://6EXxunP4.cpgdy.cn
http://ItdhoK5o.cpgdy.cn
http://fyz0SJAm.cpgdy.cn
http://Sz4QvZ3w.cpgdy.cn
http://O7wSyCp9.cpgdy.cn
http://3cTJgAIG.cpgdy.cn
http://mk7Zu9YP.cpgdy.cn
http://bYpeUwZO.cpgdy.cn
http://LgcDw1L4.cpgdy.cn
http://4SHZvKV0.cpgdy.cn
http://uOtqVFmj.cpgdy.cn
http://xQ1D8vhA.cpgdy.cn
http://J70OeNuy.cpgdy.cn
http://89QVv1E8.cpgdy.cn
http://dAHz7gGk.cpgdy.cn
http://igER27oS.cpgdy.cn
http://QHgs6NYc.cpgdy.cn
http://bYKqfAMg.cpgdy.cn
http://cSJbBiJU.cpgdy.cn
http://cWUT04kP.cpgdy.cn
http://yUPLe7J2.cpgdy.cn
http://Atnn9hoi.cpgdy.cn
http://www.dtcms.com/a/378776.html

相关文章:

  • 浅聊一下ThreadLocal
  • 部署大模型的极简笔记
  • linux面试题记录
  • 深度解码OpenAI的2025野心:Codex重生与GPT-5 APIKey获取调用示例
  • 文献阅读笔记:脉冲神经网络最新文献合集-IV
  • STM32学习路线开启篇:芯片简介与课程简介
  • 第七章 ELK Stack高级应用与集成
  • 认识跨平台UI框架Flutter和MAUI区别,如何选。
  • 9.11-QT-QT的基本使用
  • 线程安全相关的注解
  • [超表面论文快讯-242] PR-微波超四元数涡旋阵列洛书加权锁定成像加密-江南大学王继成、上海科技大学王雄团队
  • 质量特性工程
  • 性能测试-jmeter10-分布式测试
  • Java中方法重写与重载的区别
  • 【代码随想录算法训练营——Day9】字符串——151.翻转字符串里的单词、卡码网:55.右旋转字符串、28.实现 strStr()、459.重复的子字符串
  • 少儿舞蹈小程序(13)作品播放量累加及点赞
  • 【AI知识点】模型训练优化之——混合精度训练
  • 华为考试:HCIE数通考试难度分析
  • OpenHarmony app通过U盘升级配置
  • mobx-miniprogram小程序的数据传输
  • rdKafka驾驭手册:从青铜到王者的异步消息屠龙术
  • Ubuntu\Linux环境中驱动版本配置cudaToolKit
  • 法规变更后研发文档更新不及时该怎么办
  • linux 时间相关的命令
  • ThreadLocal 线程本地变量源码深度解析
  • 虚拟化技术(1):虚拟化技术的演进、挑战与突破
  • AWS strands agents 当智能体作为独立服务/容器部署时,它们无法共享进程内状态
  • 云手机与云游戏之间有什么关系?
  • 数据库学习MySQL系列3、Windows11系统安装MySQL方法二.zip压缩包详细教程
  • 淘宝/天猫按图搜索(拍立淘)item_search_img API接口全解析