JavaWeb中的Session、SessionListener、在线人数统计 JavaWeb中的Session
在JavaWeb中使用HttpSession(以下简称session)对象来表示一个会话。
正在装载数据……
Session的创建(代表会话周期的开始):第一次调用request.getSession()方法时,会创建一个session对象。
Session的销毁(代表会话周期的结束):在某个请求周期内调用了Session.invalidate()方法,此请求周期结束后,session被销毁;或者是session超时后自动销毁。
对于JSP,如果指定了,则在JSP中无法直接访问内置的session变量,同时也不会主动创建session,因为此时JSP未自动执行request.getSession()操作获取session。
在session对象刚创建的第一个请求周期内,调用session.isNew()方法将得到true。
可以在web.xml文件中配置session默认的超时时间(分钟):
<session-config>
<session-timeout>10session-timeout>
session-config> 也可以调用session. setMaxInactiveInterval()方法设置session超时时间(分钟)
SessionListener
通过SessionListenr可以监听session的创建和销毁,步骤:
1.写一个类MySessionListener,实现javax.servlet.http.HttpSessionListener接口及其sessionCreated()、sessionDestroyed()方法
2.在web.xml中配置SessionListener:
<listener>
<listener-class>MySessionListener类全名listener-class>
listener>
当 session被创建和销毁时,容器会分别调用SessionListener的sessionCreated()方法和 sessionDestroyed()方法,这两个方法中传入了一个参数对象HttpSessionEvent,可以通过此对象的 getSession()方法获取session对象。
应用:在线人数统计
import java.util.HashSet;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public class MySessionListener implements HttpSessionListener {
public void sessionCreated(HttpSessionEvent event) {
HttpSession session = event.getSession();
ServletContext application = session.getServletContext();
// 在application范围由一个HashSet集保存所有的session
HashSet sessions = (HashSet) application.getAttribute("sessions");
if (sessions == null) {
sessions = new HashSet();
application.setAttribute("sessions", sessions);
}
// 新创建的session均添加到HashSet集中
sessions.add(session);
// 可以在别处从application范围中取出sessions集合 // 然后使用sessions.size()获取当前活动的session数,即为“在线人数”
}
public void sessionDestroyed(HttpSessionEvent event) {
HttpSession session = event.getSession();
ServletContext application = session.getServletContext();
HashSet sessions = (HashSet) application.getAttribute("sessions");
// 销毁的session均从HashSet集中移除
sessions.remove(session);
}
}
package stat;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public class OnlineCounter extends HttpServlet implements HttpSessionListener,
HttpSessionAttributeListener,HttpSessionBindingListener {
public OnlineCounter(){
System.out.println("OnlineCounter initialized.");
}
private static final long serialVersionUID = 1L;
private static int sessionCounter = 0;
private static int attributeCounter = 0;
public void sessionCreated(HttpSessionEvent se) {
sessionCounter++;
System.out.println("session created:" + sessionCounter);
}
public void sessionDestroyed(HttpSessionEvent se) {
sessionCounter--;
System.out.println("session destroied");
}
public void attributeAdded(HttpSessionBindingEvent se) {
attributeCounter++;
System.out.println("attribute added :" + attributeCounter);
}
public void attributeRemoved(HttpSessionBindingEvent se) {
attributeCounter--;
System.out.println("attribute removed");
}
public void attributeReplaced(HttpSessionBindingEvent se) {
System.out.println(se.getName()+" replaced");
}
public void valueBound(HttpSessionBindingEvent event){
System.out.println(event.getName()+"_Bound_"+event.getValue());
}
public void valueUnbound(HttpSessionBindingEvent event) {
System.out.println(event.getName()+"_Unbound_"+event.getValue());
}
public static int getOnlineSession() {
return sessionCounter;
}
public static int getOnlineAttribute() {
return attributeCounter;
}
}
统计在线人数..sessionCreated中添加ip判断
package com.asiainfo.sale.common;
import javax.servlet.http.*;
import javax.servlet.ServletException;
import java.io.IOException;
import java.util.Date;
import java.util.ArrayList;
import java.sql.Timestamp;
import javax.servlet.ServletRequestListener;
import javax.servlet.ServletRequestEvent;
public class SessionCounter implements HttpSessionListener,ServletRequestListener {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
private static int activeSessions = 0;
private HttpServletRequest request;
private static ArrayList list = new ArrayList();
public void init() throws ServletException {
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
HttpSession session = request.getSession();
}
public void destroy() {
}
public void requestDestroyed(ServletRequestEvent event) {
//To change body of implemented methods use File | Settings | File Templates.
}
public void requestInitialized(ServletRequestEvent sre){
request=(HttpServletRequest)sre.getServletRequest();
}
public void sessionCreated(HttpSessionEvent httpSessionEvent) {
String sessionId = httpSessionEvent.getSession().getId();
Timestamp createTime = new Timestamp(System.currentTimeMillis());
String loginIp = request.getRemoteAddr();
boolean rs = true;
if(list.size() > 0){
for(int i = 0;i < list.size(); i ++){
if(loginIp.equals(list.get(i))){
rs = false;
}
}
}
if(rs){ //如果队列中存在相同的IP 则SESSION不增加
list.add(loginIp);
System.out.println("ipList队列新增ip: "+loginIp);
activeSessions++;
System.out.println("新增SESSION,sessionId = " + sessionId +"; createTime = " + createTime
+ "; loginIp = " + loginIp +"; 当前总SESSION值为 "+activeSessions);
}
}
public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
String sessionId = httpSessionEvent.getSession().getId();
Timestamp overTime = new Timestamp(System.currentTimeMillis());
String loginIp = request.getRemoteAddr();
if(activeSessions>0){
if(list.size() > 0){
for(int i = 0;i < list.size(); i ++){
if(loginIp.equals(list.get(i))){
list.remove(i);
System.out.println("ipList队列移除ip: "+loginIp);
}
}
}
activeSessions--; //在用户销毁的时候,从队列中踢出这个IP
System.out.println("销毁SESSION,sessionId = " + sessionId +"; overTime = " + overTime
+ "; loginIp = " + loginIp +"; 当前总SESSION值为 "+activeSessions);
}
}
public static int getActiveSessions() {
return activeSessions;
}
public void setActiveSessions(int i) {
activeSessions = i;
}
}
在JavaWeb中使用HttpSession(以下简称session)对象来表示一个会话。
正在装载数据……
Session的创建(代表会话周期的开始):第一次调用request.getSession()方法时,会创建一个session对象。
Session的销毁(代表会话周期的结束):在某个请求周期内调用了Session.invalidate()方法,此请求周期结束后,session被销毁;或者是session超时后自动销毁。
对于JSP,如果指定了,则在JSP中无法直接访问内置的session变量,同时也不会主动创建session,因为此时JSP未自动执行request.getSession()操作获取session。
在session对象刚创建的第一个请求周期内,调用session.isNew()方法将得到true。
可以在web.xml文件中配置session默认的超时时间(分钟):
<session-config>
<session-timeout>10session-timeout>
session-config> 也可以调用session. setMaxInactiveInterval()方法设置session超时时间(分钟)
SessionListener
通过SessionListenr可以监听session的创建和销毁,步骤:
1.写一个类MySessionListener,实现javax.servlet.http.HttpSessionListener接口及其sessionCreated()、sessionDestroyed()方法
2.在web.xml中配置SessionListener:
<listener>
<listener-class>MySessionListener类全名listener-class>
listener>
当 session被创建和销毁时,容器会分别调用SessionListener的sessionCreated()方法和 sessionDestroyed()方法,这两个方法中传入了一个参数对象HttpSessionEvent,可以通过此对象的 getSession()方法获取session对象。
应用:在线人数统计
import java.util.HashSet;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public class MySessionListener implements HttpSessionListener {
public void sessionCreated(HttpSessionEvent event) {
HttpSession session = event.getSession();
ServletContext application = session.getServletContext();
// 在application范围由一个HashSet集保存所有的session
HashSet sessions = (HashSet) application.getAttribute("sessions");
if (sessions == null) {
sessions = new HashSet();
application.setAttribute("sessions", sessions);
}
// 新创建的session均添加到HashSet集中
sessions.add(session);
// 可以在别处从application范围中取出sessions集合 // 然后使用sessions.size()获取当前活动的session数,即为“在线人数”
}
public void sessionDestroyed(HttpSessionEvent event) {
HttpSession session = event.getSession();
ServletContext application = session.getServletContext();
HashSet sessions = (HashSet) application.getAttribute("sessions");
// 销毁的session均从HashSet集中移除
sessions.remove(session);
}
}
package stat;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public class OnlineCounter extends HttpServlet implements HttpSessionListener,
HttpSessionAttributeListener,HttpSessionBindingListener {
public OnlineCounter(){
System.out.println("OnlineCounter initialized.");
}
private static final long serialVersionUID = 1L;
private static int sessionCounter = 0;
private static int attributeCounter = 0;
public void sessionCreated(HttpSessionEvent se) {
sessionCounter++;
System.out.println("session created:" + sessionCounter);
}
public void sessionDestroyed(HttpSessionEvent se) {
sessionCounter--;
System.out.println("session destroied");
}
public void attributeAdded(HttpSessionBindingEvent se) {
attributeCounter++;
System.out.println("attribute added :" + attributeCounter);
}
public void attributeRemoved(HttpSessionBindingEvent se) {
attributeCounter--;
System.out.println("attribute removed");
}
public void attributeReplaced(HttpSessionBindingEvent se) {
System.out.println(se.getName()+" replaced");
}
public void valueBound(HttpSessionBindingEvent event){
System.out.println(event.getName()+"_Bound_"+event.getValue());
}
public void valueUnbound(HttpSessionBindingEvent event) {
System.out.println(event.getName()+"_Unbound_"+event.getValue());
}
public static int getOnlineSession() {
return sessionCounter;
}
public static int getOnlineAttribute() {
return attributeCounter;
}
}
统计在线人数..sessionCreated中添加ip判断
package com.asiainfo.sale.common;
import javax.servlet.http.*;
import javax.servlet.ServletException;
import java.io.IOException;
import java.util.Date;
import java.util.ArrayList;
import java.sql.Timestamp;
import javax.servlet.ServletRequestListener;
import javax.servlet.ServletRequestEvent;
public class SessionCounter implements HttpSessionListener,ServletRequestListener {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
private static int activeSessions = 0;
private HttpServletRequest request;
private static ArrayList list = new ArrayList();
public void init() throws ServletException {
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
HttpSession session = request.getSession();
}
public void destroy() {
}
public void requestDestroyed(ServletRequestEvent event) {
//To change body of implemented methods use File | Settings | File Templates.
}
public void requestInitialized(ServletRequestEvent sre){
request=(HttpServletRequest)sre.getServletRequest();
}
public void sessionCreated(HttpSessionEvent httpSessionEvent) {
String sessionId = httpSessionEvent.getSession().getId();
Timestamp createTime = new Timestamp(System.currentTimeMillis());
String loginIp = request.getRemoteAddr();
boolean rs = true;
if(list.size() > 0){
for(int i = 0;i < list.size(); i ++){
if(loginIp.equals(list.get(i))){
rs = false;
}
}
}
if(rs){ //如果队列中存在相同的IP 则SESSION不增加
list.add(loginIp);
System.out.println("ipList队列新增ip: "+loginIp);
activeSessions++;
System.out.println("新增SESSION,sessionId = " + sessionId +"; createTime = " + createTime
+ "; loginIp = " + loginIp +"; 当前总SESSION值为 "+activeSessions);
}
}
public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
String sessionId = httpSessionEvent.getSession().getId();
Timestamp overTime = new Timestamp(System.currentTimeMillis());
String loginIp = request.getRemoteAddr();
if(activeSessions>0){
if(list.size() > 0){
for(int i = 0;i < list.size(); i ++){
if(loginIp.equals(list.get(i))){
list.remove(i);
System.out.println("ipList队列移除ip: "+loginIp);
}
}
}
activeSessions--; //在用户销毁的时候,从队列中踢出这个IP
System.out.println("销毁SESSION,sessionId = " + sessionId +"; overTime = " + overTime
+ "; loginIp = " + loginIp +"; 当前总SESSION值为 "+activeSessions);
}
}
public static int getActiveSessions() {
return activeSessions;
}
public void setActiveSessions(int i) {
activeSessions = i;
}
}