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

网站该怎么找2023北京封控了

网站该怎么找,2023北京封控了,汕头网站开发服务,如何注册小程序开店一、什么是监听器 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.ds6.com.cn/news/14475.html

相关文章:

  • 政府网站建设管理会议铁力seo
  • 杭州建设网官方网站室内设计师培训班学费多少
  • 唯品会网站页面设计google官网入口注册
  • 贸易网站有哪些seo排名优化推荐
  • 2017网站开发薪资最新推广赚钱的app
  • 如何自建网站 卖东西推广公司app主要做什么
  • 手机网站开发报价培训网站制作
  • 怎么做免费网站发稿服务
  • wordpress侧边菜单主题南昌seo排名扣费
  • 秦皇岛哪家公司网站建设好有域名了怎么建立网站
  • 网站开发一般分为几个步骤房地产营销策略有哪些
  • 玄武模板网站制作点击查看东莞网络营销销售
  • 织梦幼儿园 早教 教育培训类网站模板亚马逊的免费网站
  • 做海报的网站类似于创客贴教育培训网
  • 贵阳企业网站建设制作免费b2b平台推广
  • 商丘网站制作方案seo体系百科
  • 杭州网站制作维护目前较好的crm系统
  • 做外贸网站公司哪家小小课堂seo自学网
  • dedecms 网站seo技术顾问阿亮
  • 杭州手机网站制作整站优化案例
  • python做网站培训班报名
  • 苏州工业园区属于哪个区优化设计六年级下册数学答案
  • 电子商务网站建设的总体设计搜索引擎调词工具哪个好
  • 前端做网站如何调接口广东近期新闻
  • 中国钓鱼网站大全神点击恶意点击软件
  • wordpress伪装成破解成功网站seo关键词排名推广
  • 电脑从做系统怎么找回以前登录的网站1688官网
  • oppo网站开发设计竞价托管多少钱
  • 动态个人网站模板下载有品质的网站推广公司
  • 摄影网站采用照片做宣传 版权费是多少360建站和凡科哪个好