session总结

登录人数的统计:
ServletContext session1=getServletConfig().getServletContext();//取得ServletContext对象实例
if((SessionListener)session1.getAttribute("listener1")==null)
{
SessionListener sessionListener1=new SessionListener("count");
//只设置一次,不同于上面日志文件的记录每次会话均设置。即当第一个客户连接到服务器时启动一个全局变量,此后所有的客户将使用相同的上下文。
session1.setAttribute("listener1",sessionListener1);//将监听器对象设置成ServletContext的属性,具有全局范围有效性,即所有的客户均可以取得它的实例。
}
session.setAttribute("listener1",(SessionListener)session1.getAttribute("listener1"));
//取出此全局对象,并且将此对象绑定到某个会话中,此举将促使监听器调用valueBound,计数器加一。
在此后的程序中随时可以用以下代码取得当前的登录人数:
((SessionListener)session.getAttribute("listener1")).getCount()
getCount()是监听器的一个方法,即取得当前计数器的值也就是登录人数了。
from-javaresearch.org

 

 

 

 

从作用域范围来说,Servlet的作用域有ServletContext,HttpSession,ServletRequest.

Context范围:

ServletContextListener:
对一个应用进行全局监听.随应用启动而启动,随应用消失而消失主要有两个方法:

contextDestroyed(ServletContextEvent event)

在应用关闭的时候调用

contextInitialized(ServletContextEvent event)

在应用启动的时候调用

这个监听器主要用于一些随着应用启动而要完成的工作,也就是很多人说的我想在容器
启动的时候干..........
一般来说对"全局变量"初始化,如

public void contextInitialized(ServletContextEvent event){
ServletContex sc = event.getServletContext();
sc.setAttribute(name,value);
}

以后你就可以在任何servlet中getServletContext().getAttribute(name);
我最喜欢用它来做守护性工作,就是在contextInitialized(ServletContextEvent event)
方法中实现一个Timer,然后就让应用在每次启动的时候让这个Timer工作:
程序代码:
public void contextInitialized(ServletContextEvent event){
timer = new Timer();
timer.schedule(new TimerTask(){
public void run(){
//do any things
}
},0,时间间隔);
}


有人说Timer只能规定从现在开始的多长时间后,每隔多久做一次事或在什么时间做
一次事,那我想在每月1号或每天12点做一项工作如何做呢?
你只要设一个间隔,然后每次判断一下当时是不是那个时间段就行了啊,比如每月一号做,那你
时间间隔设为天,即24小时一个循环,然后在run方法中判断当时日期new Date().getDate()==1
就行了啊.如果是每天的12点,那你时间间隔设为小时,然后在run中判断new Date().getHour()
==12,再做某事就行了.

ServletContextAttributeListener:

这个监听器主要监听ServletContex对象在setAttribute()和removeAttribute()的事件,注意
也就是一个"全局变量"在被Add(第一次set),replace(对已有的变量重新赋值)和remove的时候.
分别调用下面三个方法:
public void attributeAdded(ServletContextAttributeEvent scab)这个方法不仅可以知道
哪些全局变量被加进来,而且可获取容器在启动时自动设置了哪些context变量:
程序代码:
public void attributeAdded(ServletContextAttributeEvent scab){
System.out.println(scab.getName());
}
public void attributeRemoved(ServletContextAttributeEvent scab)

public void attributeReplaced(ServletContextAttributeEvent scab)


Session范围:
HttpSessionListener:
这个监听器主要监听一个Session对象被生成和销毁时发生的事件.对应有两个方法:
程序代码:
public void sessionCreated(HttpSessionEvent se)

public void sessionDestroyed(HttpSessionEvent se)


一般来说,一个session对象被create时,可以说明有一个新客端进入.可以用来粗略统计在线人
数,注意这不是精确的,因为这个客户端可能立即就关闭了,但sessionDestroyed方法却会按一定
的策略很久以后才会发生.

HttpSessionAttributeListener:
和ServletContextAttributeListener一样,它监听一个session对象的Attribut被Add(一个特定
名称的Attribute每一次被设置),replace(已有名称的Attribute的值被重设)和remove时的事件.
对就的有三个方法.
程序代码:
public void attributeAdded(HttpSessionBindingEvent se)

public void attributeRemoved(HttpSessionBindingEvent se)

public void attributeReplaced(HttpSessionBindingEvent se)


上面的几个监听器的方法,都是在监听应用逻辑中servlet逻辑中发生了什么事,一般的来说.
我们只要完成逻辑功能,比如session.setAttribute("aaa","111");我只要把一个名为aaa的变量
放在session中以便以后我能获取它,我并不关心当session.setAttribute("aaa","111");发生时
我还要干什么.(当然有些时候要利用的),但对于下面这个监听器,你应该好好发解一下:

HttpSessionBindingListener:
上面的监听器都是作为一个独立的Listener在容器中控制事件的.而HttpSessionBindingListener
对在一对象中监听该对象的状态,实现了该接口的对象如果被作为value被add到一个session中或从
session中remove,它就会知道自己已经作为一个session对象或已经从session删除,这对于一些非
纯JAVA对象,生命周期长于session的对象,以及其它需要释放资源或改变状态的对象非常重要.
比如:
session.setAttribute("abcd","1111");
以后session.removeAttribute("abcd");因为abcd是一个字符中,你从session中remove后,它就会
自动被垃圾回收器回收,而如果是一个connection:(只是举例,你千万不要加connection往session
中加入)
程序代码:
session.setAttribute("abcd",conn);

以后session.removeAttribute("abcd");这时这个conn被从session中remove了,你已经无法获取它
的句柄,所以你根本没法关闭它.而在没有remove之前你根本不知道什么时候要被remove,你又无法
close(),那么这个connection对象就死了.另外还有一些对象可以在被加入一个session时要锁定
还要被remove时要解锁,应因你在程序中无法判断什么时候被remove(),add还好操作,我可以先加锁
再add,但remove就后你就找不到它的句柄了,根本没法解锁,所以这些操作只能在对象自身中实现.
也就是在对象被add时或remove时通知对象自己回调相应的方法:
程序代码:
MyConn extends Connection implements HttpSessionBindingListener{
public void valueBound(HttpSessionBindingEvent se){
this.initXXX();
}
public void valueUnbound(HttpSessionBindingEvent se){

this.close();
}
}


session.setAttribute("aaa",new MyConn());
这时如果调用session.removeAttribute("aaa"),则触发valueUnbound方法,就会自动关闭自己.
而其它的需要改变状态的对象了是一样.

 

使用Servlet监听器可以统计在线用户,具体实现方法如下:
1.通过ServletContext监听初始化一个application对象,保存在线用户列表;
2.通过Session监听当用户登录成功设置Session属性时将用户名保存在列表中;
3.通过Session监听当用户注销登录时将用户名从列表中删除。

实例:
OnlineListener.java:

 程序代码

package mgc.listener.test;

import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class OnlineListener implements ServletContextListener,ServletContextAttributeListener,HttpSessionListener,HttpSessionAttributeListener {  
    private ServletContext application = null ;
    private HttpSession session = null ;  
    public void contextInitialized(ServletContextEvent sce) {       
        //初始化一个application对象
        this.application=sce.getServletContext() ;
        //设置一个列表属性,用于保存在线用户名
        this.application.setAttribute("online", new ArrayList()) ;
    }   
    public void contextDestroyed(ServletContextEvent sce) {       
    }   
    public void attributeAdded(ServletContextAttributeEvent scab) {      
    }  
    public void attributeRemoved(ServletContextAttributeEvent scab) {      
    }   
    public void attributeReplaced(ServletContextAttributeEvent scab) {
       
    }   
    public void sessionCreated(HttpSessionEvent se) {       
    }  
    public void sessionDestroyed(HttpSessionEvent se) {
   
        //取得用户名列表
        List online=(List)this.application.getAttribute("online") ;
        //取得当前用户名
        String username=(String)se.getSession().getAttribute("username") ;
        //将此用户名从列表中删除
        online.remove(username) ;
        //将删除后的列表重新设置到application属性中
        this.application.setAttribute("online", online) ;
    }  
    public void attributeAdded(HttpSessionBindingEvent se) {
       
        //取得用户名列表
        List online=(List)this.application.getAttribute("online") ;
        //将当前用户名添加到列表中
        online.add(se.getValue()) ;
        //将添加后的列表重新设置到application属性中
        this.application.setAttribute("online", online) ;
    }
   
    public void attributeRemoved(HttpSessionBindingEvent se) {
       
    }  
    public void attributeReplaced(HttpSessionBindingEvent se) {      
    }
}
web.xml:

 程序代码

  <listener>
      <listener-class>mgc.listener.test.OnlineListener</listener-class>
  </listener>
sessionlistener.jsp:

 程序代码

<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="java.util.*" %>
<html>
  <head>
    <title>sessionlistener</title>
  </head>
 
  <body>
      <form action="onlinelistener.jsp" method="post" >
          用户名:<input type="text" name="username"/>
          <input type="submit" value="登录"/>
          <a href="logout.jsp">注销</a>
      </form>
<%
    String username=request.getParameter("username") ;
    if(username != null) {
   
        session.setAttribute("username",username) ;   
    }   
%>
    <p>
    <h3>在线用户:</h3>
    <hr>
<%
    List online = (List)getServletContext().getAttribute("online") ;
    Iterator iter = online.iterator();
    while(iter.hasNext()) {
   
%>
    <li><%=iter.next() %></li>
<%
    }
%>
  </body>
</html>
logout.jsp:

 程序代码

<%@ page contentType="text/html;charset=GB2312" %>
<html>
  <head>
    <title>logout</title>
  </head>
 
  <body>
<%
    session.invalidate() ;
    response.setHeader("refresh","3;URL=onlinelistener.jsp") ;
%>
    <h3>注销成功!</h3>
    3秒后自动返回登录页面<br>
    如果没有跳转,请点<a href="onlinelistener.jsp">这里</a>
  </body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值