HttpSessionListener是个session监听器它有两个方法publicvoidsessionCreated(HttpSessionEventevent){}和publicvoidsessionDestroyed(HttpSessionEventevent){}前者是在session被创建的时候执行后者是在session被销毁的时候执行通过对当前session的监听达到统计在线人数的效果
代码如下
首先建一个监听类CountLineListener实现HttpSessionListener接口并添加未实现的方法sessionCreated(){}和sessionDestroyed(){}:
[java]
packagecomtestlistener;
importjavaxservletServletContext;
importjavaxservlethttpHttpSessionEvent;
importjavaxservlethttpHttpSessionListener;
publicclassCountLineListenerimplementsHttpSessionListener{
/***********
*创建session时调用
*/
publicvoidsessionCreated(HttpSessionEventevent){
Systemoutprintln(创建session……)
ServletContextcontext=eventgetSession()getServletContext()
Integercount=(Integer)contextgetAttribute(count)
if(count==null){
count=newInteger()
}else{
intco=countintValue()
count=newInteger(co+)
}
Systemoutprintln(当前用户人数+count)
contextsetAttribute(countcount)//保存人数
}
/************
*销毁session时调用
*/
publicvoidsessionDestroyed(HttpSessionEventevent){
Systemoutprintln(销毁session……)
ServletContextcontext=eventgetSession()getServletContext()
Integercount=(Integer)contextgetAttribute(count)
intco=countintValue()
count=newInteger(co)
contextsetAttribute(countcount)
Systemoutprintln(当前用户人数+count)
}
}
监听类写好了接下来就要在webxml里配置此监听类添加代码
[java]
<listener>
<listenerclass>comtestlistenerCountLineListener</listenerclass>
</listener>
针对以上可以结合servlet写个小例子
()loginjsp
[html]
<%@pagelanguage=javaimport=javautil*pageEncoding=UTF%>
<!DOCTYPEHTMLPUBLIC//WC//DTDHTMLTransitional//EN>
<html>
<head>
<title>MyJSPindexjspstartingpage</title>
<metahttpequiv=pragmacontent=nocache>
<metahttpequiv=cachecontrolcontent=nocache>
<metahttpequiv=expirescontent=>
<metahttpequiv=keywordscontent=keywordkeywordkeyword>
<metahttpequiv=descriptioncontent=Thisismypage>
</head>
<body>
<formmethod=POSTaction=<%=requestgetContextPath()%>/MyServlet>
<inputtype=textname=username/>
<br/><inputtype=submitvalue=登录/>
</form>
</body>
</html>
点击登录>MyServlet
()MyServletjava
[java]
packagecomtestservlet;
importjavaioIOException;
importjavaxservletServletException;
importjavaxservlethttpHttpServlet;
importjavaxservlethttpHttpServletRequest;
importjavaxservlethttpHttpServletResponse;
publicclassMyServletextendsHttpServlet{
publicvoiddoGet(HttpServletRequestrequestHttpServletResponseresponse)
throwsServletExceptionIOException{
thisdoPost(requestresponse)
}
publicvoiddoPost(HttpServletRequestrequestHttpServletResponseresponse)
throwsServletExceptionIOException{
Stringuser=requestgetParameter(username)
requestgetSession()setAttribute(useruser)
requestgetRequestDispatcher(/indexjsp)forward(requestresponse)
}
}
登录就跳到首页indexjsp显示在线人数
[html]
<%@pagelanguage=javaimport=javautil*pageEncoding=UTF%>
<!DOCTYPEHTMLPUBLIC//WC//DTDHTMLTransitional//EN>
<html>
<head>
<title>MyJSPindexjspstartingpage</title>
<metahttpequiv=pragmacontent=nocache>
<metahttpequiv=cachecontrolcontent=nocache>
<metahttpequiv=expirescontent=>
<metahttpequiv=keywordscontent=keywordkeywordkeyword>
<metahttpequiv=descriptioncontent=Thisismypage>
</head>
<body>
这里是首页当前访问量
<%
ServletContextcontext=sessiongetServletContext()
Integercount=(Integer)contextgetAttribute(count)
%>
<%=count%>
<br/>
当前用户${sessionScopeuser}
</body>
</html>
这样就简单实现统计当前在线人数的效果了如果在页面有一个退出系统的链接可以调用sessioninvalidate()执行清除session这样在线人数就会那如果用户没有点击退出系统而是直接关闭浏览器呢?我自己做了个测试在关闭浏览器一会儿的话服务器端会自动执行sessionDestroyed()方法进行销毁session此时用户人数但是有时候又不会执行不知道为什么自己还是慢慢研究吧……