Servlet过滤器1(精品)

上传人:cn****1 文档编号:487318650 上传时间:2023-12-26 格式:DOC 页数:13 大小:75KB
返回 下载 相关 举报
Servlet过滤器1(精品)_第1页
第1页 / 共13页
Servlet过滤器1(精品)_第2页
第2页 / 共13页
Servlet过滤器1(精品)_第3页
第3页 / 共13页
Servlet过滤器1(精品)_第4页
第4页 / 共13页
Servlet过滤器1(精品)_第5页
第5页 / 共13页
点击查看更多>>
资源描述

《Servlet过滤器1(精品)》由会员分享,可在线阅读,更多相关《Servlet过滤器1(精品)(13页珍藏版)》请在金锄头文库上搜索。

1、Servlet监听器详解Servlet监听器用于监听一些重要事件的发生,监听器对象可以在事情发生前、发生后可以做一些必要的处理。接口:目前Servlet2.4和JSP2.0总共有8个监听器接口和6个Event类,其中HttpSessionAttributeListener与HttpSessionBindingListener皆使用HttpSessionBindingEvent;HttpSessionListener和HttpSessionActivationListener则都使用HttpSessionEvent;其余Listener对应的Event如下所示:Listener接口Event类S

2、ervletContextListenerServletContextEventServletContextAttributeListenerServletContextAttributeEventHttpSessionListenerHttpSessionEventHttpSessionActivationListenerHttpSessionAttributeListenerHttpSessionBindingEventHttpSessionBindingListenerServletRequestListenerServletRequestEventServletRequestAttri

3、buteListenerServletRequestAttributeEvent一 ,ServletContext相关监听接口补充知识:通过ServletContext 的实例可以存取应用程序的全局对象以及初始化阶段的变量。在JSP文件中,application 是 ServletContext 的实例,由JSP容器默认创建。Servlet 中调用 getServletContext()方法得到 ServletContext 的实例。注意:全局对象即Application范围对象,初始化阶段的变量指在web.xml中,经由元素所设定的变量,它的范围也是Application范围,例如: Nam

4、e browser当容器启动时,会建立一个Application范围的对象,若要在JSP网页中取得此变量时:String name = (String)application.getInitParameter(Name);或者使用EL时:$initPara.name若是在Servlet中,取得Name的值方法:String name = (String) ServletContext.getInitParameter (Name);1.ServletContextListener:用于监听WEB 应用启动和销毁的事件,监听器类需要实现javax.servlet.ServletContextLi

5、stener 接口。ServletContextListener 是 ServletContext 的监听者,如果 ServletContext 发生变化,如服务器启动时 ServletContext 被创建,服务器关闭时 ServletContext 将要被销毁。ServletContextListener接口的方法:void contextInitialized(ServletContextEvent sce)通知正在接受的对象,应用程序已经被加载及初始化。void contextDestroyed(ServletContextEvent sce)通知正在接受的对象,应用程序已经被载出。S

6、ervletContextEvent中的方法:ServletContext getServletContext()取得ServletContext对象2.ServletContextAttributeListener:用于监听WEB应用属性改变的事件,包括:增加属性、删除属性、修改属性,监听器类需要实现javax.servlet.ServletContextAttributeListener接口。ServletContextAttributeListener接口方法:void attributeAdded(ServletContextAttributeEvent scab)若有对象加入Appl

7、ication的范围,通知正在收听的对象void attributeRemoved(ServletContextAttributeEvent scab)若有对象从Application的范围移除,通知正在收听的对象void attributeReplaced(ServletContextAttributeEvent scab)若在Application的范围中,有对象取代另一个对象(重新给赋值保存)时,通知正在收听的对象ServletContextAttributeEvent中的方法:java.lang.String getName()回传属性的名称java.lang.Object getVa

8、lue()回传属性的值现在来说说Servlet的监听器Listener,它是实现了javax.servlet.ServletContextListener 接口的服务器端程序,它也是随web应用的启动而启动,只初始化一次,随web应用的停止而销毁。主要作用是: 做一些初始化的内容添加工作、设置一些基本的内容、比如一些参数或者是一些固定的对象等等。下面利用监听器对数据库连接池DataSource的初始化演示它的使用: 1: MyServletContextListener.java 2: package dc.gz.listeners; 3: import javax.servlet.Servl

9、etContext; 4: import javax.servlet.ServletContextEvent; 5: import javax.servlet.ServletContextListener; 6: import mons.dbcp.BasicDataSource; 7: 8: /* 9: * Web应用监听器10: */11: public class MyServletContextListener implements ServletContextListener 12: / 应用监听器的销毁方法14: public void contextDestroyed(Servle

10、tContextEvent event) 15: ServletContext sc = event.getServletContext();16: / 在整个web应用销毁之前调用,将所有应用空间所设置的内容清空17: sc.removeAttribute(dataSource);18: System.out.println(销毁工作完成.);19: 20: 21: / 应用监听器的初始化方法22: public void contextInitialized(ServletContextEvent event) 23: / 通过这个事件可以获取整个应用的空间24: / 在整个web应用下面

11、启动的时候做一些初始化的内容添加工作25: ServletContext sc = event.getServletContext();26: / 设置一些基本的内容;比如一些参数或者是一些固定的对象27: / 创建DataSource对象,连接池技术 dbcp28: BasicDataSource bds = new BasicDataSource();29: bds.setDriverClassName(com.mysql.jdbc.Driver);30: bds.setUrl(jdbc:mysql:/localhost:3306/hibernate);31: bds.setUsernam

12、e(root);32: bds.setPassword(root);33: bds.setMaxActive(10);/最大连接数34: bds.setMaxIdle(5);/最大管理数35: /bds.setMaxWait(maxWait); 最大等待时间36: / 把 DataSource 放入ServletContext空间中,37: / 供整个web应用的使用(获取数据库连接)38: sc.setAttribute(dataSource, bds);39: System.out.println(应用监听器初始化工作完成.);40: System.out.println(已经创建Data

13、Source.);41: 42: web.xml中配置如下,很简单: 1: 2: 3: dc.gz.listeners.MyServletContextListener 4: 这样配置好了之后,以后在web应用中就可以通过ServletContext取得BasicDataSource对象,从而获取与数据库的连接,提高性能,方便使用。上面通过两个示例演示了Filter和Listener的基本使用,对于其它的应用则需要我们在项目开发中根据具体的场景选择。二、HttpSession相关监听接口1.HttpSessionBindingListener接口注意:HttpSessionBindingLis

14、tener接口是唯一不需要在web.xml中设定的Listener当我们使用的类实现了HttpSessionBindingListener接口后,只要该类的对象加入Session范围(即调用HttpSession对象的setAttribute方法的时候)或从Session范围中移出(即调用HttpSession对象的removeAttribute方法的时候或Session Time out的时候)时,容器分别会自动调用下列两个方法:void valueBound(HttpSessionBindingEvent event)void valueUnbound(HttpSessionBindingEvent event)public class UserObject impleme

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 建筑/环境 > 施工组织

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号