jsp之监听器

来源:互联网 发布:avmask永久域名 编辑:程序博客网 时间:2024/05/17 12:55
(1)什么是监听器?
是servlet规范当中定义的一种特殊的组件,用来监听容器产生的事件并且进行相应的处理。
容器会产生两大类事件: a,生命周期相关的事件:容器创建或者销毁request,session,ServletContext(servlet上下文)这些对象时产生的事件。b,绑订相关的事件:
对request,session,ServletContext(servlet上下文)调用了setAttribute,removeAttribute产生的事件。
(2)如何写一个监听器?
step1, 写一个java类,实现相应的监听器接口(要看监听的事件类型: HttpSessionListener)。
step2, 在监听器接口对应的方法里面,实现监听的处理逻辑。
step3,注册(web.xml)
(3)ServletContext
1)什么是Servlet上下文?
容器在启动的时候,会为每一个应用创建唯一的一个
符合ServletContext接口的一个对象。该对象会一直存在,
除非关闭容器。
2)如何获得servlet上下文?
a,GenericServlet提供了getServletContext方法。
b,ServletConfig提供了getServletContext方法。
c, FilterConfig提供了getServletContext方法。
d, HttpSession提供了getServletContext方法。
3)servlet上下文的作用?
a,绑订相关的方法
setAttribute,removeAttribute,getAttribute
request,session,ServletContext这三个对象都有绑订
相关的方法,应该优先使用生命周期短(request<session
<servlet上下文)的。
b, 访问全局的初始化参数
step1, 使用<context-param>来配置,该参数可以被
所有的servlet,filter访问。
step2, 使用ServletContext提供的getInitParameter方法
来获取参数值。
c, 获得实际部署时的物理路径。

String getRealPath(String path);

通过代码先了解一下servletContext,即servlet上下文。   

[java] view plaincopy
  1. //获得ServletContext  
  2.       ServletContext sctx=getServletContext();  
  3.       sctx.setAttribute("username","user1");  
  4.       out.println("已经将username绑定"+"到了ServletContext上面");  
输出通过servletContext得到的内容:
[java] view plaincopy
  1. ServletContext sctx=getServletContext();  
  2.            String username=(String)sctx.getAttribute("username");  

配置web.xml:
[html] view plaincopy
  1.  <!-- 全局初始化参数 -->  
  2. <context-param>  
  3.     <param-name>version</param-name>  
  4.     <param-value>1.0</param-value>  
  5. </context-param>  
  6. <listener>  
  7.     <listener-class>web.CountListener</listener-class>  
  8. </listener>  

在servlet中得到该数据:
[java] view plaincopy
  1. String version=sctx.getInitParameter("version");  
  2.           out.println("version:"+version);  

最后写一个统计在线人数的程序,利用到监听器知识,即触发某个事件时,对应的方法自动触发执行:

step1

编写servlet

[java] view plaincopy
  1. import javax.servlet.ServletContext;  
  2. import javax.servlet.http.HttpServlet;  
  3. import javax.servlet.http.HttpSession;  
  4. import javax.servlet.http.HttpSessionEvent;  
  5. import javax.servlet.http.HttpSessionListener;  
  6.   
  7. public class CountListener implements HttpSessionListener {  
  8.      //session对象一旦创建成功,会执行该方法  
  9.     private int count=0;  
  10.     public void sessionCreated(HttpSessionEvent arg0) {  
  11.         // TODO Auto-generated method stub  
  12.         count++;  
  13.         HttpSession session=arg0.getSession();  
  14.         ServletContext sctx=session.getServletContext();  
  15.         sctx.setAttribute("count", count);  
  16.     }  
  17.      //session对象被销毁会执行该方法  
  18.     public void sessionDestroyed(HttpSessionEvent arg0) {  
  19.         // TODO Auto-generated method stub  
  20.         count--;  
  21.         HttpSession session=arg0.getSession();  
  22.         ServletContext sctx=session.getServletContext();  
  23.         sctx.setAttribute("count", count);  
  24.     }  
  25.        
  26. }  
step2

配置全局化参数:

[html] view plaincopy
  1. <listener>  
  2.         <listener-class>web.CountListener</listener-class>  
  3.     </listener>  


step3,编写jsp文件

[html] view plaincopy
  1. <body style="font-size:30px;">  
  2.       当前系统在线人数是:  
  3.       <%=application.getAttribute("count") %>       
  4.       <%=application.getAttribute("username") %>  
  5.     </br>  
  6.     <a href="logout.jsp">退出系统</a>  
  7.   </body>  

step4,编写logout.jsp文件
[html] view plaincopy
  1. <body>  
  2.    <%  
  3.       session.invalidate();  
  4.     %>  
  5.  </body> 
原创粉丝点击