web.xml中--使用监听器Servlet
2012-03-28 11:33
323 查看
1.Listener是Servlet的监听器
2.可以监听客户端的请求、服务端的操作等。
3.通过监听器,可以自动激发一些操作,如监听在线用户数量,当增加一个HttpSession时,给在线人数加1。
4.编写监听器需要实现相应的接口
5.编写完成后在web.xml文件中配置一下,就可以起作用了
6.可以在不修改现有系统基础上,增加web应用程序生命周期事件的跟踪
常用的监听接口
1.ServletContextAttributeListener
监听对ServletContext属性的操作,比如增加/删除/修改
2.ServletContextListener
监听ServletContext,当创建ServletContext时,激发contextInitialized(ServletContextEvent sce)方法;当销毁ServletContext时,激发contextDestroyed(ServletContextEvent sce)方法。
3.HttpSessionListener
监听HttpSession的操作。当创建一个Session时,激发session Created(SessionEvent se)方法;当销毁一个Session时,激发sessionDestroyed (HttpSessionEvent se)方法。
4.HttpSessionAttributeListener
监听HttpSession中的属性的操作。当在Session增加一个属性时,激发attributeAdded(HttpSessionBindingEvent se) 方法;当在Session删除一个属性时,激发attributeRemoved(HttpSessionBindingEvent se)方法;当在Session属性被重新设置时,激发attributeReplaced(HttpSessionBindingEvent se) 方法。
使用范例:
由监听器管理共享数据库连接
生命周期事件的一个实际应用由context监听器管理共享数据库连接。在web.xml中如下定义监听器:
<listener>
<listener-class>XXX.MyConnectionManager</listener-class>
</listener> ?server创建监听器的实例,接受事件并自动判断实现监听器接口的类型。要记住的是由于监听器是配置在部署描述符web.xml中,所以不需要改变任何代码就可以添加新的监听器。
public class MyConnectionManager implements ServletContextListener{
public void contextInitialized(ServletContextEvent e) {
Connection con = // create connection
e.getServletContext().setAttribute("con", con);
}
public void contextDestroyed(ServletContextEvent e) {
Connection con = (Connection) e.getServletContext().getAttribute("con");
try {
con.close();
}
catch (SQLException ignored) { } // close connection
}
}
监听器保证每新生成一个servlet context都会有一个可用的数据库连接,并且所有的连接对会在context关闭的时候随之关闭。
计算在线用户数量的Linstener
(1)
Package xxx;
public class OnlineCounter {
private static long online = 0;
public static long getOnline(){
return online;
}
public static void raise(){
online++;
}
public static void reduce(){
online--;
}
}
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public class OnlineCounterListener implements HttpSessionListener{
public void sessionCreated(HttpSessionEvent hse) {
OnlineCounter.raise();
}
public void sessionDestroyed(HttpSessionEvent hse){
OnlineCounter.reduce();
}
}
在需要显示在线人数的JSP中可是使用
目前在线人数:
<%@ page import=“xxx.OnlineCounter" %>
<%=OnlineCounter.getOnline()%>
退出会话(可以给用户提供一个注销按钮):
<form action="exit.jsp" method=post>
<input type=submit value="exit">
</form>
exit.jsp: <%session.invalidate() ;%>
在web.xml中加入:
<listener>
<listener-class>servletlistener111111.SecondListener</listener-class> </listener>
怎么样,就是这么简单,不用对现有代码做任何的修改。
相关文章推荐
- web.xml中--使用监听器Servlet
- web.xml中--使用监听器Servlet
- web.xml中--使用监听器Servlet
- web.xml中--使用监听器Servlet
- 使用spring web.xml里的配置 - 请求处理器DispatcherServlet
- 一个基于servlet 3.0的不使用web.xml配置文件的建议web项目demo
- 备忘:在web.xml中使用org.springframework.js.resource.ResourceServlet的配置对静态资源的访问
- web.xml与注解配置servlet使用介绍
- 在web.xml定义,在网页中使用变量实例 使用所有Servlet都可以访问的<context-param>方式
- 快速创建一个servlet并且在web.xml配置和使用它
- 使用spring web.xml里的配置 - 请求处理器DispatcherServlet
- Java EE使用Servlet获取web.xml中的参数
- servlet 3.0无需配置web.xml,使用注入方式配置servlet实现登陆功能(服务器需要支持servlet3.0)
- Servlet中使用web.xml标签元素自定义错误页面备忘
- 使用ServletConfig获得web.xml资源中的参数
- tomcat7.0使用servlet处理jsp表单(form)的web.xml配置
- 使用UI-Tag的注意点: 请确定/WEB-INF/web.xml文件中的ServletDispatcher 设定为自动加载
- tomcat7.0使用servlet处理jsp表单(form)的web.xml配置
- 谷歌验证码非web.xml非servlet拦截 使用配置文件自定义加载,防止多次点击(防js攻击)
- Spring之WebContext不使用web.xml启动 初始化重要的类源码分析(Servlet3.0以上的)