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

怎么自己建立一个网站后台淮南服装网站建设费用

怎么自己建立一个网站后台,淮南服装网站建设费用,asp.net开发微网站开发,凡科建设网站一、什么是监听器 servlet监听器是一种特殊的接口,用于监听特定的事件(如请求创建和销毁、会话创建和销毁、上下文的初始化和销毁)。 当Web应用程序中反生特定事件时,Servlet容器就会自动调用监听器中相应的方法来处理这些事件。…

一、什么是监听器

servlet监听器是一种特殊的接口,用于监听特定的事件(如请求创建和销毁、会话创建和销毁、上下文的初始化和销毁)。

当Web应用程序中反生特定事件时,Servlet容器就会自动调用监听器中相应的方法来处理这些事件。

二、监听器的作用?

1、全局初始化和清理

通过实现ServletContextListener接口,开发者可以在Web应用程序启动时执行全局初始化操作(例如加载配置文件、初始化数据库连接池等),并在应用程序关闭时执行清理操作(例如关闭数据库连接池、释放资源等)。

public class MyServletContextListener implements ServletContextListener {@Overridepublic void contextInitialized(ServletContextEvent sce) {// 应用程序初始化时执行的代码System.out.println("Application is starting...");}@Overridepublic void contextDestroyed(ServletContextEvent sce) {// 应用程序销毁时执行的代码System.out.println("Application is shutting down...");}
}

2、会话管理

通过实现HttpSessionListener接口,开发者可以监控用户会话的创建和销毁。这可以用于统计当前在线用户数量、清理过期会话等。

public class MyHttpSessionListener implements HttpSessionListener {private int activeSessions = 0;@Overridepublic void sessionCreated(HttpSessionEvent se) {activeSessions++;System.out.println("Session created. Total active sessions: " + activeSessions);}@Overridepublic void sessionDestroyed(HttpSessionEvent se) {activeSessions--;System.out.println("Session destroyed. Total active sessions: " + activeSessions);}
}

3、请求和属性监听

通过实现ServletRequestListenerServletRequestAttributeListener接口,开发者可以监听请求的生命周期以及请求属性的变化。这可以用于日志记录、性能监控或动态修改请求属性等。

public class MyServletRequestListener implements ServletRequestListener {@Overridepublic void requestInitialized(ServletRequestEvent sre) {System.out.println("Request initialized...");}@Overridepublic void requestDestroyed(ServletRequestEvent sre) {System.out.println("Request destroyed...");}
}

三、监听器的种类?

1)ServletRequestListener

用途:监听 ServletRequest 的创建和销毁事件。

方法:

  • void requestInitialized(ServletRequestEvent sre):在请求初始化时被调用。

  • void requestDestroyed(ServletRequestEvent sre):在请求销毁时被调用。

代码:

public class MyServletRequestListener implements ServletRequestListener {@Overridepublic void requestInitialized(ServletRequestEvent sre) {System.out.println("Request initialized");}@Overridepublic void requestDestroyed(ServletRequestEvent sre) {System.out.println("Request destroyed");}
}

 

2) ServletRequestAttributeListener

用途监听 ServletRequest 中属性的添加、修改和删除事件。

方法:

  • void attributeAdded(ServletRequestAttributeEvent srae):当属性被添加到请求时调用。

  • void attributeRemoved(ServletRequestAttributeEvent srae):当属性从请求中移除时调用。

  • void attributeReplaced(ServletRequestAttributeEvent srae):当请求中的属性被替换时调用。

代码:

public class MyServletRequestAttributeListener implements ServletRequestAttributeListener {@Overridepublic void attributeAdded(ServletRequestAttributeEvent srae) {System.out.println("Attribute added: " + srae.getName());}@Overridepublic void attributeRemoved(ServletRequestAttributeEvent srae) {System.out.println("Attribute removed: " + srae.getName());}@Overridepublic void attributeReplaced(ServletRequestAttributeEvent srae) {System.out.println("Attribute replaced: " + srae.getName());}
}

3)HttpSessionListener

用途:监听 HttpSession 的创建和销毁事件。

方法:

  • void sessionCreated(HttpSessionEvent se):在会话创建时被调用。

  • void sessionDestroyed(HttpSessionEvent se):在会话销毁时被调用。

代码:

public class MyHttpSessionListener implements HttpSessionListener {@Overridepublic void sessionCreated(HttpSessionEvent se) {System.out.println("Session created: " + se.getSession().getId());}@Overridepublic void sessionDestroyed(HttpSessionEvent se) {System.out.println("Session destroyed: " + se.getSession().getId());}
}

4)HttpSessionAttributeListener

用途:监听 HttpSession 中属性的添加、修改和删除事件。

方法:

  • void attributeAdded(HttpSessionBindingEvent event):当属性被添加到会话时调用。

  • void attributeRemoved(HttpSessionBindingEvent event):当属性从会话中移除时调用。

  • void attributeReplaced(HttpSessionBindingEvent event):当会话中的属性被替换时调用。

代码:

public class MyHttpSessionAttributeListener implements HttpSessionAttributeListener {@Overridepublic void attributeAdded(HttpSessionBindingEvent event) {System.out.println("Session attribute added: " + event.getName());}@Overridepublic void attributeRemoved(HttpSessionBindingEvent event) {System.out.println("Session attribute removed: " + event.getName());}@Overridepublic void attributeReplaced(HttpSessionBindingEvent event) {System.out.println("Session attribute replaced: " + event.getName());}
}

5)ServletContextListener

用途:监听 ServletContext 的初始化和销毁事件。

方法:

  • void contextInitialized(ServletContextEvent sce):在上下文初始化时被调用。

  • void contextDestroyed(ServletContextEvent sce):在上下文销毁时被调用。

代码:

public class MyServletContextListener implements ServletContextListener {@Overridepublic void contextInitialized(ServletContextEvent sce) {System.out.println("Context initialized");}@Overridepublic void contextDestroyed(ServletContextEvent sce) {System.out.println("Context destroyed");}
}

6)ServletContextAttributeListener

用途:监听 ServletContext 中属性的添加、修改和删除事件。

方法:

  • void attributeAdded(ServletContextAttributeEvent scab):当属性被添加到上下文时调用。

  • void attributeRemoved(ServletContextAttributeEvent scab):当属性从上下文中移除时调用。

  • void attributeReplaced(ServletContextAttributeEvent scab):当上下文中的属性被替换时调用。

代码:

public class MyServletContextAttributeListener implements ServletContextAttributeListener {@Overridepublic void attributeAdded(ServletContextAttributeEvent scab) {System.out.println("Context attribute added: " + scab.getName());}@Overridepublic void attributeRemoved(ServletContextAttributeEvent scab) {System.out.println("Context attribute removed: " + scab.getName());}@Overridepublic void attributeReplaced(ServletContextAttributeEvent scab) {System.out.println("Context attribute replaced: " + scab.getName());}
}

http://www.yayakq.cn/news/154945/

相关文章:

  • 建一个设计网站要多少钱2核4g 1m做网站
  • 专门做自由行的网站如何制作官方网站
  • 官方网站平台下载返利网网站框架目录
  • 网站建设价位更换wordpress语言包
  • 四川建设行政主管部门官方网站5944免费空间上搭建网站
  • 山西太原网站建设做网站竞价是什么意思
  • 祥云平台技术支持双语网站页面设计风格的主要内容
  • 网站设计规划方案中建卓越建设有限公司网站首页
  • 手机对比网站自己建免费网站
  • 自学做网站要学什么某企业网络营销策略研究论文
  • 深圳网站制作公司讯息东莞网站建设多长时间
  • 学习电子商务网站建设与管理的感想网站的域名和密码
  • 南网站建设 首选搜点网络沈阳网站建设 熊掌号
  • ICP备案不停网站做网页的素材
  • 宽甸县建设局网站投票制作网站
  • 电商网站模板引擎浏览器下载免费安装
  • 哪些人可以做网站怎么给自己网站做搜索框
  • 多多卡起货网站建设昆明百度网站建设
  • 城市建设模拟游戏网站一般网站隐蔽点么么进
  • 网站的静态页面谁做宣传推广
  • 网站首页排名突然没了百度推广网站建设费
  • 网站开发知识视频dw软件可以做哪些网站
  • 做贷超网站 前期需要什么分析网站美工做的是什么
  • 试述网站建设的流程民宿网站开发dfd图
  • 微商手机网站制作公司哪家好企业商城网站开发
  • 网站建设 公司百度学术搜索入口
  • 广西钦州网站建设部署wordpress最应该用什么os
  • 湛江网站搜索引擎推广泸州市建设工程管理局网站
  • 常州网站公司怎么选慧聪网郭凡生现状
  • 金华网站制作推广网站开发教学大纲