web系统与技术实验十.docxVIP

  • 37
  • 0
  • 约8.8千字
  • 约 8页
  • 2020-12-07 发布于天津
  • 举报
实验十Servlet事件处理 网络112 李海佩 201806090204 一、 实验目的 了解在哪些对象上可以产生事件; 掌握ServletContext和HttpSession对象上的事件处理方法。 了解在ServletRequest对象上的事件及处理方法。 二、 实验原理 在Web应用程序中,事件的处理也是通过事件监听器接口处理的。 Web应用事件处理 的原理为:当 Web应用中某些状态改变时, Servlet容器就产生某种事件,如 ServletCo ntext 对象初始化时会产生 ServletContextEvent事件,此时 Servlet容器就会从注册的事件监听器 中寻找处理该事件的监听器对象,并执行相应的代码。 b5E2RGbCAP 在Servlet 2.4规范中共定义了 6种事件类型和8个事件监听器接口,它们可以处理三 种对象上的事件,如表 13.1所示:p1EanqFDPw 表13.1监听器接口与事件类 监听对象 监听器接口 监听事件 ServletContext ServletContextListener ServletContextAttributeListener SeivlelCunlexlEvenl HttpSession HttpSessionListener ServletContextAttributeEvent HttpSessionEvent HttpSessionActivationListener HttpSessionAttributeListener HttpSessionBindingEvent HttpSessionBindingListener ServletRequest ServletRequestListener ServletRequestAttributeListener ServietRequestEvent ServletRequestAttributeEvent 三、实验内容与步骤 一)创建一个名为 chap10的Web工程,编写一个 ServletContext事件监听器,该事件监 听器的功能是当 Web应用程序初始化和销毁时以及在 ServletContext对象上添加属性、删 除属性和替换属性时,在 Tomcat日志中记录有关信息。 DXDiTa9E3d 【步骤1】编写监听器类。 package liste ner。 import java.util.Date 。 import javax .namin g.C on text 。 import javax. nami ng.In itialC on text 。 import javax.servlet.* 。 import javax.sql.DataSource 。 Publicfi nal class MyListe ner impleme nts ServletCo ntextListe ner,ServletCo ntextAttributeListe ner{ RTCrpUDGiT private ServletC on text servletC on text = n ull 。 private Con text con text = n ull 。 private DataSource dataSource = null。 public void co ntextDestroyed(ServletCo ntextEve nt arg0 { 5PCzVD7HxA con text = (Con text〉argO.getServletC on text(。 ((ServletContext context.removeAttribute(dataSource 。jLBHrnAiLg ((ServletContext context.log(应用程序已关闭:+new Date( 。 xHAQX74J0X } public void co ntextl ni tialized(ServletC on textEve nt arg0 { LDAYtRyKfE con text = (Con text argO.getServletC on text( 。 try { if(con text==n ull{ con text = new In itialC on text( 。 } dataSource (DataSourcec on text .lo okup(java:comp/e nv/jdbc/sampleDS 。 Zzz6ZB2Ltk } catch (Excepti on e { ((ServletC on text con text .lo g(Excepti on+e 。

文档评论(0)

1亿VIP精品文档

相关文档