一、统计在线人数 1.监听器
- package com.logcd.listener;
-
- import javax.servlet.ServletContext;
- import javax.servlet.http.HttpSessionEvent;
- import javax.servlet.http.HttpSessionListener;
-
- public class SessionCounter implements HttpSessionListener {
- public void sessionCreated(HttpSessionEvent event) {
- ServletContext ctx = event.getSession( ).getServletContext( );
- Integer numSessions = (Integer) ctx.getAttribute("numSessions");
- if (numSessions == null) {
- numSessions = new Integer(1);
- }else {
- int count = numSessions.intValue( );
- numSessions = new Integer(count + 1);
- }
- ctx.setAttribute("numSessions", numSessions);
- }
- public void sessionDestroyed(HttpSessionEvent event) {
- ServletContext ctx = event.getSession( ).getServletContext( );
- Integer numSessions = (Integer) ctx.getAttribute("numSessions");
- if (numSessions == null) {
- numSessions = new Integer(0);
- }
- else {
- int count = numSessions.intValue( );
- numSessions = new Integer(count - 1);
- }
- ctx.setAttribute("numSessions", numSessions);
- }
- }
package com.logcd.listener;import javax.servlet.ServletContext; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; public class SessionCounter implements HttpSessionListener { public void sessionCreated(HttpSessionEvent event) { ServletContext ctx = event.getSession( ).getServletContext( ); Integer numSessions = (Integer) ctx.getAttribute("numSessions"); if (numSessions == null) { numSessions = new Integer(1); }else { int count = numSessions.intValue( ); numSessions = new Integer(count + 1); } ctx.setAttribute("numSessions", numSessions); } public void sessionDestroyed(HttpSessionEvent event) { ServletContext ctx = event.getSession( ).getServletContext( ); Integer numSessions = (Integer) ctx.getAttribute("numSessions"); if (numSessions == null) { numSessions = new Integer(0); } else { int count = numSessions.intValue( ); numSessions = new Integer(count - 1); } ctx.setAttribute("numSessions", numSessions); } }
说明:实现了HttpSessionListener定义的两个方法:sessionCreated()和sessionDestroyed()。这两个方法可以监听到当前应用中session的创建和销毁情况。
以下两种情况下就会发生sessionDestoryed(会话销毁)事件:
(1)执行session.invalidate()方法时。
(2)如果用户长时间没有访问服务器,超过了会话最大超时时间,服务器就会自动销毁超时的session。
- <session-config>
- <session-timeout>1</session-timeout>
- </session-config>
<session-config> <session-timeout>1</session-timeout></session-config>
时间单位是一分钟,并且只能是整数,如果是零或负数,那么会话就永远不会超时。
2.web.xml中配置
- <listener>
- <listener-class>com.logcd.listener.SessionCounter</listener-class>
- </listener>
<listener> <listener-class>com.logcd.listener.SessionCounter</listener-class> </listener>
3.jsp调用
- 目前共有<span class="salient"> <%=application.getAttribute("numSessions")%> </span>人在线
目前共有<span class="salient"> <%=application.getAttribute("numSessions")%> </span>人在线
二、统计注册人数 1.监听器
- package com.logcd.listener;
-
- import java.util.HashMap;
- import java.util.Map;
-
- import javax.servlet.ServletContext;
- import javax.servlet.http.HttpSession;
- import javax.servlet.http.HttpSessionBindingEvent;
- import javax.servlet.http.HttpSessionBindingListener;
-
- public class OnlineUserBindingListener implements HttpSessionBindingListener{
-
- private String userId;
- private String userName;
-
- public OnlineUserBindingListener(String userId,String username){
- this.setUserId(userId);
- this.setUserName(userName);
- }
-
- @SuppressWarnings("unchecked")
- public void valueBound(HttpSessionBindingEvent event) {
- HttpSession session = event.getSession();
- ServletContext application = session.getServletContext();
-
-
- Map onlineUserList = (Map) application.getAttribute("onlineUserList");
-
- if (onlineUserList == null) {
- onlineUserList = new HashMap();
- application.setAttribute("onlineUserList", onlineUserList);
- }
- onlineUserList.put(this.userId, this.userName);
-
- }
-
- public void valueUnbound(HttpSessionBindingEvent event) {
- HttpSession session = event.getSession();
- ServletContext application = session.getServletContext();
-
-
- Map onlineUserList = (Map) application.getAttribute("onlineUserList");
- onlineUserList.remove(this.userId);
-
- }
-
-
- public String getUserId() {
- return userId;
- }
-
- public void setUserId(String userId) {
- this.userId = userId;
- }
-
- public String getUserName() {
- return userName;
- }
-
- public void setUserName(String userName) {
- this.userName = userName;
- }
-
- }
package com.logcd.listener;import java.util.HashMap;import java.util.Map;import javax.servlet.ServletContext;import javax.servlet.http.HttpSession;import javax.servlet.http.HttpSessionBindingEvent;import javax.servlet.http.HttpSessionBindingListener;public class OnlineUserBindingListener implements HttpSessionBindingListener{private String userId;private String userName;public OnlineUserBindingListener(String userId,String username){this.setUserId(userId);this.setUserName(userName);}@SuppressWarnings("unchecked")public void valueBound(HttpSessionBindingEvent event) {HttpSession session = event.getSession(); ServletContext application = session.getServletContext(); // 把用户放入在线列表 Map onlineUserList = (Map) application.getAttribute("onlineUserList"); // 第一次使用前,需要初始化 if (onlineUserList == null) { onlineUserList = new HashMap(); application.setAttribute("onlineUserList", onlineUserList); } onlineUserList.put(this.userId, this.userName);}public void valueUnbound(HttpSessionBindingEvent event) {HttpSession session = event.getSession(); ServletContext application = session.getServletContext(); // 从在线列表中删除用户 Map onlineUserList = (Map) application.getAttribute("onlineUserList"); onlineUserList.remove(this.userId);}public String getUserId() {return userId;}public void setUserId(String userId) {this.userId = userId;}public String getUserName() {return userName;}public void setUserName(String userName) {this.userName = userName;}}
说明:实现了HttpSessionBindingListener接口,接口中共定义了两个方法:valueBound()和valueUnbound(),分别对应数据绑定,和取消绑定两个事件。
所谓对session进行数据绑定,就是调用session.setAttribute()把HttpSessionBindingListener保存进session中。
HttpSessionListener只需要设置到web.xml中就可以监听整个应用中的所有session。HttpSessionBindingListener必须实例化后放入某一个session中,才可以进行监听。
从监听范围上比较,HttpSessionListener设置一次就可以监听所有session,HttpSessionBindingListener通常都是一对一的。
valueUnbound的触发条件是以下三种情况:
(1)执行session.invalidate()时。
(2)session超时,自动销毁时。
(3)执行session.setAttribute("onlineUserListener", "其他对象");或session.removeAttribute("onlineUserListener");将listener从session中删除时。
因此,只要不将listener从session中删除,就可以监听到session的销毁。
2.jsp调用
(1)login.jsp
- <%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
- <%@ page import="com.logcd.listener.OnlineUserBindingListener"%>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme() + "://"
- + request.getServerName() + ":" + request.getServerPort()
- + path + "/";
- String userId = "2000090603";
- String userName = "LogCN";
- session.setAttribute(userId,userName);
- session.setAttribute("onlineUserBindingListener", new OnlineUserBindingListener(userId,userName));
- response.sendRedirect("counter.jsp");
- %>
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%><%@ page import="com.logcd.listener.OnlineUserBindingListener"%><%String path = request.getContextPath();String basePath = request.getScheme() + "://"+ request.getServerName() + ":" + request.getServerPort()+ path + "/";String userId = "2000090603";String userName = "LogCN";session.setAttribute(userId,userName);session.setAttribute("onlineUserBindingListener", new OnlineUserBindingListener(userId,userName));response.sendRedirect("counter.jsp");%>
(2)counter.jsp
- <%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
- 注册用户<span class="salient"><%=((Map)application.getAttribute("onlineUserList")).size()%> </span> 人
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>注册用户<span class="salient"><%=((Map)application.getAttribute("onlineUserList")).size()%> </span> 人
三、RequestDispatcher RequestDispatcher是一个Web资源的包装器,可以用来把当前request传递到该资源,或者把新的资源包括到当前响应中。RequestDispatcher接口中定义了两个方法:include/forward。
由于<jsp:include>只能指定固定的jsp文件名,不能动态指定jsp文件名。需要把<jsp:include>翻译为Java code – RequestDispatcher.include();
用法:
<% request.getRequestDispatcher(filename).include(request, response); />
RequestDispatcher.forward()方法将当前的request和response重定向到该RequestDispacher指定的资源。只有在尚未向客户端输出响应时才可以调用forward()方法,如果页面缓存不为空,在重定向前将自动清除缓存。否则将抛出一个IllegalStateException异常。
JSP中的三种页面跳转方式:
1. response.sendRedirect();
2. response.setHeader("location","");
3. <jsp:forward page="" />