HttpSessionListener是个session监听器,它有两个方法:publicvoidsessionCreated(HttpSessionEventevent){}和publicvoidsessionDestroyed(HttpSessionEventevent){},前者是在session被创建的时候执行,后者是在session被销毁的时候执行,通过对当前session的监听,达到统计在线人数的效果。
代码如下:
首先建一个监听类CountLineListener,实现HttpSessionListener接口,并添加未实现的方法sessionCreated(){}和sessionDestroyed(){}:
[java]
packagecom.test.listener;
importjavax.servlet.ServletContext;
importjavax.servlet.http.HttpSessionEvent;
importjavax.servlet.http.HttpSessionListener;
publicclassCountLineListenerimplementsHttpSessionListener{
/***********
*创建session时调用
*/
publicvoidsessionCreated(HttpSessionEventevent){
System.out.println("创建session……");
ServletContextcontext=event.getSession()。getServletContext();
Integercount=(Integer)context.getAttribute("count");
if(count==null){
count=newInteger(1);
}else{
intco=count.intValue();
count=newInteger(co+1);
}
System.out.println("当前用户人数:"+count);
context.setAttribute("count",count);//保存人数
}
/************
*销毁session时调用
*/
publicvoidsessionDestroyed(HttpSessionEventevent){
System.out.println("销毁session……");
ServletContextcontext=event.getSession()。getServletContext();
Integercount=(Integer)context.getAttribute("count");
intco=count.intValue();
count=newInteger(co-1);
context.setAttribute("count",count);
System.out.println("当前用户人数:"+count);
}
}
监听类写好了,接下来就要在web.xml里配置此监听类,添加代码:
[java]
<listener>
<listener-class>com.test.listener.CountLineListener</listener-class>
</listener>
针对以上可以结合servlet写个小例子:
(1)login.jsp
[html]
<%@pagelanguage="java"import="java.util.*"pageEncoding="UTF-8"%>
<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
<html>
<head>
<title>MyJSP'index.jsp'startingpage</title>
<metahttp-equiv="pragma"content="no-cache">
<metahttp-equiv="cache-control"content="no-cache">
<metahttp-equiv="expires"content="0">
<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
<metahttp-equiv="description"content="Thisismypage">
</head>
<body>
<formmethod="POST"action="<%=request.getContextPath()%>/MyServlet">
<inputtype="text"name="username"/>
<br/><inputtype="submit"value="登录"/>
</form>
</body>
</html>
点击登录---->MyServlet
(2)MyServlet.java
[java]
packagecom.test.servlet;
importjava.io.IOException;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
publicclassMyServletextendsHttpServlet{
publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
this.doPost(request,response);
}
publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
Stringuser=request.getParameter("username");
request.getSession()。setAttribute("user",user);
request.getRequestDispatcher("/index.jsp")。forward(request,response);
}
}
登录就跳到首页index.jsp,显示在线人数:
[html]
<%@pagelanguage="java"import="java.util.*"pageEncoding="UTF-8"%>
<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
<html>
<head>
<title>MyJSP'index.jsp'startingpage</title>
<metahttp-equiv="pragma"content="no-cache">
<metahttp-equiv="cache-control"content="no-cache">
<metahttp-equiv="expires"content="0">
<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
<metahttp-equiv="description"content="Thisismypage">
</head>
<body>
这里是首页,当前访问量:
<%
ServletContextcontext=session.getServletContext();
Integercount=(Integer)context.getAttribute("count");
%>
<%=count%>
<br/>
当前用户:${sessionScope.user}
</body>
</html>
这样就简单实现统计当前在线人数的效果了。如果在页面有一个"退出系统"的链接,可以调用session.invalidate()执行清除session,这样在线人数就会-1,那如果用户没有点击"退出系统",而是直接关闭浏览器呢?我自己做了个测试,在关闭浏览器一会儿的话,服务器端会自动执行sessionDestroyed()方法进行销毁session,此时用户人数-1,但是有时候又不会执行,不知道为什么,自己还是慢慢研究吧……