您的位置:首页 > 移动开发

Session应用问题-统计在线人数

2009-09-27 09:25 323 查看
在学习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值。

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息