Session应用问题-统计在线人数
2009-09-27 09:25
381 查看
在学习Java EE 的Session这一部分时看到一个统计在线人数的例子:
SLDemo.java
/*01*/ package servlet;
/*02*/ import java.util.*;
/*03*/ import javax.servlet.*;
/*04*/ import javax.servlet.http.*;
/*05*/ public class SLDemo extends HttpServlet
/*06*/ implements HttpSessionListener
/*07*/ {
/*08*/ private ServletContext application =null;
/*09*/ public void sessionCreated(HttpSessionEvent se)
/*10*/ {
/*11*/ application=se.getSession().getServletContext();
/*12*/ application.log("Create Session="+se.getSession().getId());
/*14*/ this.increateCounter(1);
/*15*/ }
/*16*/ public void sessionDestroyed(HttpSessionEvent se)
/*17*/ {
/*18*/ application.log("Des Session="+se.getSession().getId());
/*20*/ this.increateCounter(-1);
/*21*/ }
/*22*/ private void increateCounter(int i)
/*23*/ {
/*24*/ if(application.getAttribute("count")==null)
/*25*/ {
/*26*/ application.setAttribute("count",1);
/*27*/ }
/*28*/ else
/*29*/ {
/*30*/ String strTemp=application.getAttribute("count").toString();
/*31*/ int nCount=Integer.valueOf(strTemp).intValue()+i;
/*32*/ application.setAttribute("count",nCount);
/*33*/ }
/*34*/ }
/*35*/}
OnLineCount.jsp
<%@page language="java" contentType="text/html;charset=GBK"%>
<html>
<head>
<title>比特塞威斯培训</title>
</head>
<body>
<%
Object o=application.getAttribute("count");
out.print("当前在线人数:"+o);
%>
<form action="servlet/InvalidSession" method="post">
<input type="submit" value="退出">
</form>
</body>
</html>
在web.xml中添加
<listener>
<listener-class>servlet.SLDemo</listener-class>
</listener>
访问OnLineCount.jsp即可以看到当前网站应用的Session数,但如果使用下面的servlet就实现不了这个功能:
ShowCountInLine.java
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
import javax.servlet.http.*;
public class ShowCountInLine extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=gb2312");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC /"-//W3C//DTD HTML 4.01
Transitional//EN/">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
ServletContext application=this.getServletContext();
out.println("当前在线人数:"+application.getAttribute("count").toString());
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
这是因为由于Web服务器的资源有限,默认情况下访问网站上的servlet是不会创建Session的,所以上面的
Servlet要实现在线人数统计,则需要加上request.getSession()来使访问当前的servlet时产生一个Session
。注意如果使用HttpServletRequest的getSession(false),则表示如果当前的连接有Session时则返回当前的
Session,否则不创建Session而只是返回null值。
SLDemo.java
/*01*/ package servlet;
/*02*/ import java.util.*;
/*03*/ import javax.servlet.*;
/*04*/ import javax.servlet.http.*;
/*05*/ public class SLDemo extends HttpServlet
/*06*/ implements HttpSessionListener
/*07*/ {
/*08*/ private ServletContext application =null;
/*09*/ public void sessionCreated(HttpSessionEvent se)
/*10*/ {
/*11*/ application=se.getSession().getServletContext();
/*12*/ application.log("Create Session="+se.getSession().getId());
/*14*/ this.increateCounter(1);
/*15*/ }
/*16*/ public void sessionDestroyed(HttpSessionEvent se)
/*17*/ {
/*18*/ application.log("Des Session="+se.getSession().getId());
/*20*/ this.increateCounter(-1);
/*21*/ }
/*22*/ private void increateCounter(int i)
/*23*/ {
/*24*/ if(application.getAttribute("count")==null)
/*25*/ {
/*26*/ application.setAttribute("count",1);
/*27*/ }
/*28*/ else
/*29*/ {
/*30*/ String strTemp=application.getAttribute("count").toString();
/*31*/ int nCount=Integer.valueOf(strTemp).intValue()+i;
/*32*/ application.setAttribute("count",nCount);
/*33*/ }
/*34*/ }
/*35*/}
OnLineCount.jsp
<%@page language="java" contentType="text/html;charset=GBK"%>
<html>
<head>
<title>比特塞威斯培训</title>
</head>
<body>
<%
Object o=application.getAttribute("count");
out.print("当前在线人数:"+o);
%>
<form action="servlet/InvalidSession" method="post">
<input type="submit" value="退出">
</form>
</body>
</html>
在web.xml中添加
<listener>
<listener-class>servlet.SLDemo</listener-class>
</listener>
访问OnLineCount.jsp即可以看到当前网站应用的Session数,但如果使用下面的servlet就实现不了这个功能:
ShowCountInLine.java
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
import javax.servlet.http.*;
public class ShowCountInLine extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=gb2312");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC /"-//W3C//DTD HTML 4.01
Transitional//EN/">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
ServletContext application=this.getServletContext();
out.println("当前在线人数:"+application.getAttribute("count").toString());
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
这是因为由于Web服务器的资源有限,默认情况下访问网站上的servlet是不会创建Session的,所以上面的
Servlet要实现在线人数统计,则需要加上request.getSession()来使访问当前的servlet时产生一个Session
。注意如果使用HttpServletRequest的getSession(false),则表示如果当前的连接有Session时则返回当前的
Session,否则不创建Session而只是返回null值。
相关文章推荐
- 使用HttpSessionListener和ServletContextListener实现在线人数和历史访问人数统计的问题
- 在线人数统计,解决了关闭浏览器窗口,释放session的问题
- 利用文件属性结合Session实现在线人数统计
- 用HttpSessionListener与HttpSessionBindingListener实现在线人数统计
- 利用HttpSessionListener实现网站在线人数统计功能
- JavaWeb中的Session、SessionListener、在线人数统计
- 依据登录表统计累计在线人数(登录表去重问题)
- 用session监听器统计在线人数和在线人列表
- session-JSP下实现防止重复在线,统计在线人数的办法
- asp.net 使用Application和Session对象统计在线人数
- php session实现同时在线人数统计
- 利用HttpSessionListener实现网站在线人数统计功能
- java程序利用HttpSessionListener实现统计在线人数(示例代码)
- HibernateSessionBindingListener,在线人数统计
- 不用Global.asa和session实现在线人数统计
- JavaWeb中的Session、SessionListener、在线人数统计
- HttpSessionListener实现网站统计在线人数
- 【用户在线统计】如何处理浏览器关闭时候的session清理问题
- JavaWeb中的Session、SessionListener、在线人数统计
- JavaWeb中的Session、SessionListener、在线人数统计