title: Counting the number of users in web App in servlet
body: <p>I have modified my application to find out the number of users logged in a web application below is my piece of code..</p>
  
  <p>the listener class</p>
  
  <pre><code>    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));
                  }
          }
  }
  </code></pre>
  
  <p>and the main servlet is ..</p>
  
  <pre><code>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----&gt;");
  
          // TODO Auto-generated method stub
          String name=request.getParameter("txtName");
          response.setContentType("text/html");
          PrintWriter out=response.getWriter();
          out.println("Hello,"+name);
          out.println("&lt;br&gt; this output is generated by a simple servlet.");
          out.println("Total Number of users logged in---&gt;"+SessionCounter.getActiveSessions());
          out.close();
  
  
      }
  
  }
  </code></pre>
  
  <p>and the web.xml is ...</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;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"&gt;
    &lt;display-name&gt;FirstDemo&lt;/display-name&gt;
  
  
    &lt;context-param&gt;
          &lt;param-name&gt;log4jConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;/WEB-INF/log4j.properties&lt;/param-value&gt;
  &lt;/context-param&gt;
  
  
    &lt;servlet&gt;
      &lt;servlet-name&gt;hello&lt;/servlet-name&gt;
      &lt;servlet-class&gt;com.saral.MyServlet&lt;/servlet-class&gt;
    &lt;/servlet&gt;
    &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;hello&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/helloServlet&lt;/url-pattern&gt;
    &lt;/servlet-mapping&gt;
    &lt;welcome-file-list&gt;
      &lt;welcome-file&gt;home.html&lt;/welcome-file&gt;
    &lt;/welcome-file-list&gt;
  
  
    &lt;listener&gt;
    &lt;listener-class&gt;com.saral.SessionCounter&lt;/listener-class&gt;
  &lt;/listener&gt;
  
  &lt;/web-app&gt;
  </code></pre>
  
  <p>but I am getting the total number of users logged in as 0 , which is not perfect, please advise where I am wrong and how can I overcome from this.</p>