当前位置: 技术问答>java相关
高手给讲讲servlet listener!
来源: 互联网 发布时间:2015-01-25
本文导语: 我在看j2ee 1.3 tutorial第172页,关于servlet listener的内容。 这里ServletContextListener是什么时候触发事件的? 还有这里初始化方法中得到数据库连接(BookDB),是为每个session初始化吗? 但这里教程上说这个Listener是web c...
我在看j2ee 1.3 tutorial第172页,关于servlet listener的内容。
这里ServletContextListener是什么时候触发事件的?
还有这里初始化方法中得到数据库连接(BookDB),是为每个session初始化吗?
但这里教程上说这个Listener是web context对象的Listener,
session对象的listener是HttpSessionListener。
高手能给讲讲?
多谢!多谢!
public final class ContextListener
implements ServletContextListener {
private ServletContext context = null;
public void contextInitialized(ServletContextEvent event) {
context = event.getServletContext();
try {
BookDB bookDB = new BookDB();
context.setAttribute("bookDB", bookDB);
} catch (Exception ex) {
context.log("Couldn't create bookstore database bean: " + ex.getMessage());
}
Counter counter = new Counter();
context.setAttribute("hitCounter", counter);
counter = new Counter();
context.setAttribute("orderCounter", counter);
}
public void contextDestroyed(ServletContextEvent event) {
context = event.getServletContext();
BookDB bookDB = (BookDB)context.getAttribute("bookDB");
bookDB.remove();
context.removeAttribute("bookDB");
context.removeAttribute("hitCounter");
context.removeAttribute("orderCounter");
}
}
这里ServletContextListener是什么时候触发事件的?
还有这里初始化方法中得到数据库连接(BookDB),是为每个session初始化吗?
但这里教程上说这个Listener是web context对象的Listener,
session对象的listener是HttpSessionListener。
高手能给讲讲?
多谢!多谢!
public final class ContextListener
implements ServletContextListener {
private ServletContext context = null;
public void contextInitialized(ServletContextEvent event) {
context = event.getServletContext();
try {
BookDB bookDB = new BookDB();
context.setAttribute("bookDB", bookDB);
} catch (Exception ex) {
context.log("Couldn't create bookstore database bean: " + ex.getMessage());
}
Counter counter = new Counter();
context.setAttribute("hitCounter", counter);
counter = new Counter();
context.setAttribute("orderCounter", counter);
}
public void contextDestroyed(ServletContextEvent event) {
context = event.getServletContext();
BookDB bookDB = (BookDB)context.getAttribute("bookDB");
bookDB.remove();
context.removeAttribute("bookDB");
context.removeAttribute("hitCounter");
context.removeAttribute("orderCounter");
}
}
|
在JSP网页中,将Bean对象保存至session对象时,我们可以定义Bean响应HttpSessionBindingEvent事件。当Bean对象加入session、Bean从session中删除,以及session对象中止时,将会触发此事件。如下例子:
SessionEvent.jsp
HttpSessionBindingEvent事件的响应
HttpSessionBindingEvent事件的响应
Bean对象:SEvent.java
package com.bitc.ch12;
//定义Bean所属的套件
import java.io.*;
import javax.servlet.http.*;
//定义SEvent实做HttpSessionBingingListener
public class SEvent implements HttpSessionBindingListener
{
//当Bean物件加入session时, 将自动呼叫此方法
public void valueBound(HttpSessionBindingEvent event)
{
System.out.println("valueBound is called!");
}
//当Bean物件从session中移除时, 将自动呼叫此方法
public void valueUnbound(HttpSessionBindingEvent event)
{
System.out.println("valueUnbound is called!");
}
}
SessionEvent.jsp
HttpSessionBindingEvent事件的响应
HttpSessionBindingEvent事件的响应
Bean对象:SEvent.java
package com.bitc.ch12;
//定义Bean所属的套件
import java.io.*;
import javax.servlet.http.*;
//定义SEvent实做HttpSessionBingingListener
public class SEvent implements HttpSessionBindingListener
{
//当Bean物件加入session时, 将自动呼叫此方法
public void valueBound(HttpSessionBindingEvent event)
{
System.out.println("valueBound is called!");
}
//当Bean物件从session中移除时, 将自动呼叫此方法
public void valueUnbound(HttpSessionBindingEvent event)
{
System.out.println("valueUnbound is called!");
}
}