2012-07-23 43 views
0

在web应用程序的用户数我已经修改我的应用程序,找出用户的数量记录在一个Web应用程序下面是我的代码..计数在servlet的

监听器类

import javax.servlet.ServletContext; 
import javax.servlet.http.HttpSessionEvent; 
import javax.servlet.http.HttpSessionListener; 


public class SessionCounter implements HttpSessionListener 
{ 
     private static int count; 

     public static int getActiveSessions() { 
      return count; 
      } 



     public SessionCounter() 
     { 
     } 

//The "sessionCount" attribute which has been set in the servletContext should not be modified in any other part of the application. 
//Since we are using serveltContext in both the methods to modify the same variable, we have synchronized it for consistency. 


     public void sessionCreated(HttpSessionEvent e) 
     { 
       count++; 
       ServletContext sContext = e.getSession().getServletContext(); 
       synchronized (sContext) 
       { 
         sContext.setAttribute("sessionCount", new Integer(count)); 
       } 
     } 

     public void sessionDestroyed(HttpSessionEvent e) 
     { 
       count--; 
       ServletContext sContext = e.getSession().getServletContext(); 
       synchronized (sContext) 
       { 
         sContext.setAttribute("sessionCount", new Integer(count)); 
       } 
     } 
} 

和主要的servlet ..

package com.saral; 

import java.io.IOException; 
import org.apache.log4j.Logger; 
import org.apache.log4j.PropertyConfigurator; 
import java.io.PrintWriter; 

import javax.servlet.ServletException; 
import javax.servlet.annotation.WebServlet; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

/** 
* Servlet implementation class First 
*/ 
//@WebServlet("/First") 
public class MyServlet extends HttpServlet 
{ 
    private static final long serialVersionUID = 1L; 
    static final Logger logger = Logger.getLogger(MyServlet.class); 


    /** 
    * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 
    */ 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
     PropertyConfigurator.configure("log4j.properties"); 
     logger.info("before---->"); 

     // TODO Auto-generated method stub 
     String name=request.getParameter("txtName"); 
     response.setContentType("text/html"); 
     PrintWriter out=response.getWriter(); 
     out.println("Hello,"+name); 
     out.println("<br> this output is generated by a simple servlet."); 
     out.println("Total Number of users logged in--->"+SessionCounter.getActiveSessions()); 
     out.close(); 


    } 

} 

和web.xml中是...

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> 
    <display-name>FirstDemo</display-name> 


    <context-param> 
     <param-name>log4jConfigLocation</param-name> 
     <param-value>/WEB-INF/log4j.properties</param-value> 
</context-param> 


    <servlet> 
    <servlet-name>hello</servlet-name> 
    <servlet-class>com.saral.MyServlet</servlet-class> 
    </servlet> 
    <servlet-mapping> 
    <servlet-name>hello</servlet-name> 
    <url-pattern>/helloServlet</url-pattern> 
    </servlet-mapping> 
    <welcome-file-list> 
    <welcome-file>home.html</welcome-file> 
    </welcome-file-list> 


    <listener> 
    <listener-class>com.saral.SessionCounter</listener-class> 
</listener> 

</web-app> 

但我获得的登录用户总数为0,这并不完美,请告知我错在哪里以及如何克服这一点。

+0

尝试从ServletContext的 – 2012-07-23 04:41:12

+0

阅读我建议增加计数器,当用户登录的意义在登录请求处理的servlet,并将其设置在应用程序上下文并在注销中从上下文获取计数器值并减少它......并且您应该将此值作为上下文参数传递给Web应用程序。 – 2012-07-23 04:58:58

+0

嗨user1538526,它已发布已久。你找到答案了吗?如果是的话,你可以在下面回答你的问题,对其他人有好处? – Vivek 2014-01-03 07:21:05

回答

0

当客户端请求到达Tomcat服务器并且您没有调用request.getSession()时,那么Tomcat服务器stil会自动创建一个会话。之后,调用SessionCounter类中的方法sessionCreated(...)

当会话被销毁时,方法sessionDestroyed(...)将被调用。当您拨打session.invalidate()时会发生这种情况。如果关闭浏览器上的选项卡或关闭浏览器,则会话在您的tomcat服务器上仍然存在。

我这么认为。你可以使用一些不同势听众归档你的目标:HttpSessionAttributeListenerHttpSessionBindingListener,...