码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • javaWeb监听器Listener -自定义监听器类实现(二)


    javaWeb监听器Listener

    • 一、创建监听器步骤说明
    • 二、具体代码

    一、创建监听器步骤说明

    1. 自定义监听器类
    2. 实现监听器接口 重写接口中的方法
    3. 在web.xml文件中,对监听器进行注册

    二、具体代码

    1. 自定义监听器类并实现监听器接口 重写接口中的方法
    import javax.servlet.ServletContextEvent;
    import javax.servlet.ServletContextListener;
    
    public class MyServletContextListener implements ServletContextListener {
    
    	@Override
    	public void contextDestroyed(ServletContextEvent arg0) {
    		System.out.println("****************ServletContext 对象销毁了****************");
    		
    	}
    
    	@Override
    	public void contextInitialized(ServletContextEvent arg0) {
    		System.out.println("****************ServletContext 对象创建了****************");
    		
    	}
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    1. 在web.xml文件中,对监听器进行注册
    
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    	id="WebApp_ID" version="3.1">
    	<display-name>day_20220909display-name>
    	
    	<listener>
    		<listener-class>com.heima.MyServletContextListenerlistener-class>
    	listener>
    web-app>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    1. 启动服务,即会创建servletContext对象
    信息: Initializing ProtocolHandler ["http-apr-8080"]
    九月 09, 2022 4:02:11 下午 org.apache.coyote.AbstractProtocol init
    信息: Initializing ProtocolHandler ["ajp-apr-8009"]
    九月 09, 2022 4:02:11 下午 org.apache.catalina.startup.Catalina load
    信息: Initialization processed in 1533 ms
    九月 09, 2022 4:02:11 下午 org.apache.catalina.core.StandardService startInternal
    信息: Starting service Catalina
    九月 09, 2022 4:02:11 下午 org.apache.catalina.core.StandardEngine startInternal
    信息: Starting Servlet Engine: Apache Tomcat/8.0.53
    九月 09, 2022 4:02:12 下午 org.apache.catalina.util.SessionIdGeneratorBase createSecureRandom
    警告: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [405] milliseconds.
    ****************ServletContext 对象创建了****************
    九月 09, 2022 4:02:12 下午 org.apache.coyote.AbstractProtocol start
    信息: Starting ProtocolHandler ["http-apr-8080"]
    九月 09, 2022 4:02:12 下午 org.apache.coyote.AbstractProtocol start
    信息: Starting ProtocolHandler ["ajp-apr-8009"]
    九月 09, 2022 4:02:12 下午 org.apache.catalina.startup.Catalina start
    信息: Server startup in 812 ms
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
  • 相关阅读:
    Android通知怎么实现?Android开发如何操作相机和相册?
    【C++进阶之路】第三篇:二叉搜索树 & kv模型
    Axure 9 实战案例,动态面板的应用 6,导航菜单栏的手风琴效果
    Javaweb之Vue指令的详细解析
    生成数字图像基本过程
    QT+OSG/osgEarth编译之二十七:Pixman+Qt编译(一套代码、一套框架,跨平台编译,版本:Pixman-0.42.2)
    DevOps jenkins实现持续集成、持续部署
    【编程题】【Scratch四级】2020.09 数字之和
    使用html+css+js实现一个静态页面(含源码)
    【测试沉思录】8. 测试计划应该怎么做?
  • 原文地址:https://blog.csdn.net/weixin_43860634/article/details/126784582
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号