JavaWeb11监听器汇编Word文档下载推荐.docx

上传人:b****1 文档编号:15344676 上传时间:2022-10-29 格式:DOCX 页数:11 大小:69.32KB
下载 相关 举报
JavaWeb11监听器汇编Word文档下载推荐.docx_第1页
第1页 / 共11页
JavaWeb11监听器汇编Word文档下载推荐.docx_第2页
第2页 / 共11页
JavaWeb11监听器汇编Word文档下载推荐.docx_第3页
第3页 / 共11页
JavaWeb11监听器汇编Word文档下载推荐.docx_第4页
第4页 / 共11页
JavaWeb11监听器汇编Word文档下载推荐.docx_第5页
第5页 / 共11页
点击查看更多>>
下载资源
资源描述

JavaWeb11监听器汇编Word文档下载推荐.docx

《JavaWeb11监听器汇编Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《JavaWeb11监听器汇编Word文档下载推荐.docx(11页珍藏版)》请在冰豆网上搜索。

JavaWeb11监听器汇编Word文档下载推荐.docx

监听器接口

事件源类型

ServletContextListener

ServletContextEvent

ServletContextAttributeListener

ServletContextAttributeEvent

HttpSessionListener

HttpSessionEvent

HttpSessionAttributeListener

HttpSessionBindingEvent

ServletRequestListener

ServletRequestEvent

ServletRequestAttributeListener

ServletRequestAttributeEvent

2.1.1监听器使用步骤

监听器的使用分为两步,分别是监听器实现类的创建、监听器的启动配置。

1、类创建

监听器类创建方式是实现J2EE定义的相关接口、并实现规定方法,如需要监听请求对象的操作就必须实现ServletAPI中的ServletRequestListenner接口,并实现其中的监听方法。

2、启动配置

当监听器类创建好之后,需要在web.xml中配置监听器类,让系统启动时将监听器类也启动起来。

web.xml中使用listener及其子标签配置监听器类。

2.2Servlet上下文监听

上下文监听器——主要用于监听web应用程序中ServletContext对象的创建、销毁,以及属性的添加、替换、删除。

2.2.1Servlet上下文监听

ServletContext监听器,由javax.servlet包中的ServletContextListener接口提供,在该接口中分别定义了ServletContext初始化的监听方法和销毁监听方法,接口的源代码如下所示:

packagejavax.servlet;

importjava.util.EventListener;

publicinterfaceServletContextListenerextendsEventListener{

publicabstractvoidcontextInitialized(ServletContextEventservletcontextevent);

publicabstractvoidcontextDestroyed(ServletContextEventservletcontextevent);

}

其中的contextInitialized()方法用于监听上下文的初始化、contextDestroyed()方法用于监听上下文的销毁。

如例2-1在上下文创建、销毁时分别打印系统启动、销毁信息。

例2-1:

Servlet上下文监听

ApplicationListener.java

packagecom.create.javaWeb.ch11;

importjavax.servlet.ServletContext;

importjavax.servlet.ServletContextEvent;

importjavax.servlet.ServletContextListener;

/**

*Servlet上下文创建、销毁监听

*/

publicclassApplicationListenerimplementsServletContextListener{

/**

*Servlet上下文销毁

*@paramevent事件源

*/

publicvoidcontextDestroyed(ServletContextEventevent){

System.out.println("

-----WEB应用程序停止、上下文销毁-----"

);

}

*Servlet上下文创建

publicvoidcontextInitialized(ServletContextEventevent){

//得到上下文对象

ServletContextapp=event.getServletContext();

//读取应用程序初始化配置信息

Stringimgpath=app.getInitParameter("

img_config"

//向上下文保存图片目录

app.setAttribute("

imgPath"

imgpath);

-----WEB应用程序启动、上下文创建-----"

在ApplicationListener类中,在创建对象的监听方法中通过实践源对象event得到了Servlet上下文对象,通过上下文对象读取到web.xml中配置的初始化信息,并向上下文对象中保存了一个图片目录的数据。

这样就可以在jsp页面中使用EL表达式访问该路径。

在web.xml中间中添加配置信息如下:

<

context-param>

<

param-name>

img_config<

/param-name>

param-value>

/images<

/param-value>

/context-param>

listener>

listener-class>

com.create.javaWeb.ch2.ApplicationListener

/listener-class>

/listener>

其中context-param标签用于为web应用程序配置参数,可以通过上下文ServletContext的getInitParameter(Stringname)方法得到该参数;

listener标签用于配置监听器类。

启动web服务器将看到如图2-1所示的提示信息。

图2-1Servlet上下文监听

当重新启动web应用程序是将看到图2-2所示的内容,即销毁上下文对象、然后再创建上下文对象。

图2-2重启web应用程序

2.2.2Servlet上下文属性监听

除了可以监听Servlet上下文的创建和销毁之外、J2EE还提供了对上下文属性的监听。

由ServletAPI中的ServletContextAttributeListener接口提供,该接口的源代码如下所示:

publicinterfaceServletContextAttributeListenerextendsEventListener{

publicabstractvoidattributeAdded(ServletContextAttributeEventservletcontextattributeevent);

publicabstractvoidattributeRemoved(ServletContextAttributeEventservletcontextattributeevent);

publicabstractvoidattributeReplaced(ServletContextAttributeEventservletcontextattributeevent);

在该接口中定义了上下文添加、移除、替换属性的监听方法。

如例2-2修改例2-1中ApplicationListener类,实现Servlet上下文属性的监听。

例2-2:

上下文属性监听

importjavax.servlet.ServletContextAttributeEvent;

importjavax.servlet.ServletContextAttributeListener;

*@authorcreate

publicclassApplicationListenerimplementsServletContextListener,ServletContextAttributeListener{

//……

*监听添加属性

publicvoidattributeAdded(ServletContextAttributeEventscae){

******Servlet上下文【添加】属性******"

//从事件源取出属性名、属性值

--属性名:

"

+scae.getName());

--属性值:

+scae.getValue());

*监听移除属性

publicvoidattributeRemoved(ServletContextAttributeEventscae){

******Servlet上下文【移除】属性******"

*监听替换属性

publicvoidattributeReplaced(ServletContextAttributeEventscae){

******Servlet上下文【替换】属性******"

在修改后的ApplicationListener中实现

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 求职职场 > 面试

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1