关于监听器,下面说法正确的是 () A 启动 Web 应用会触发 ServletContextAttibuteListener 工作 B 关闭Web引用会触发 ServletContextListener 工作 C service0方法执行完毕会触发 ServletRequestListener 工作 D 执行 session.invalidate0会触发 HttpSessionListener 工作
时间: 2023-06-10 08:08:12 浏览: 89
答案是 B。关闭 Web 应用时会触发 ServletContextListener 工作,ServletContextAttributeListener 监听器是用来监听 ServletContext 对象属性的变化,ServletRequestListener 监听器是用来监听 HttpServletRequest 对象的创建和销毁的,HttpSessionListener 监听器是用来监听 HttpSession 对象的创建和销毁的。执行 session.invalidate() 方法不会触发 HttpSessionListener 工作,只有在 HttpSession 对象被销毁时才会触发。
相关问题
Web应用程序启动时将通知应用程序的那个事件监听器
Web应用程序启动时,将通知应用程序的ServletContextListener事件监听器。
ServletContextListener是一个Web应用程序中的事件监听器,它可以在Web应用程序的生命周期内监听ServletContext对象的创建和销毁事件,并在这些事件发生时执行一些操作。当Web应用程序启动时,容器会自动创建ServletContext对象,并将其作为参数传递给所有已注册的ServletContextListener实现类的contextInitialized()方法。在这个方法中,开发人员可以执行一些初始化操作,例如读取配置文件、初始化数据库连接池、启动定时任务等。当Web应用程序被销毁时,容器会调用所有已注册的ServletContextListener实现类的contextDestroyed()方法,开发人员可以在这个方法中执行一些清理操作,例如关闭数据库连接池、停止定时任务等。
要注册ServletContextListener事件监听器,可以在web.xml配置文件中使用listener元素进行配置,例如:
```xml
<listener>
<listener-class>com.example.MyServletContextListener</listener-class>
</listener>
```
在这个示例中,我们注册了一个名为MyServletContextListener的ServletContextListener实现类,当Web应用程序启动时,容器会自动创建该类的实例,并调用其contextInitialized()方法。
下面选项中,用于在web.xml中配置监听器的元素是( )
在web.xml中配置监听器的元素是C选项<listener>。Servlet规范中的监听器(Listener)是一种特殊的Java类,用于监听Web应用程序中的事件,并在事件发生时执行一些特殊的逻辑。例如,可以通过监听器来完成Web应用程序的初始化、销毁、会话创建和销毁、Servlet上下文属性修改等操作。
在web.xml文件中,<listener>元素用于配置一个监听器,其中包含<listener-class>子元素,用于指定该监听器的实现类。例如,下面是一个web.xml文件中Listener的定义示例:
```
<listener>
<listener-class>com.example.MyServletContextListener</listener-class>
</listener>
```
上述示例中,定义了一个ServletContextListener的实现类MyServletContextListener,并将其配置为监听器。在Web应用程序启动和关闭时,MyServletContextListener会分别执行其contextInitialized()和contextDestroyed()方法,完成Web应用程序的初始化和销毁操作。需要注意的是,<listener>元素必须放在<web-app>元素的顶层位置,同时要在其他元素之前定义。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)