title: How to send data from servlet to all logged In users
body: <p>My requirement is admin should send data to all logged in users. I have one servlet first one user send login HTTP req and gets login success response from servlet now session for this user is started.Similarly other user logs in. Now admin which is also one user can login and see two users user1 and user2 logged in. Admin should send data to all logged in users. Servlet should push data to that user without users HTTP request. kindly note during session user won't send any other HTTP request. </p>
  
  <p>Instead of comet I tried to store response obj's of each logged in user and later using that obj tried to post data back to user but that didn't worked.</p>
  
  <p>Please check below code: <strong>LoginServlet.java</strong> </p>
  
  <pre><code>    public class LoginServlet extends HttpServlet {
          private static final long serialVersionUID = 1L;
          ServletContext sc;
  
          protected ArrayList&lt;HttpServletResponse&gt; connections = new ArrayList&lt;HttpServletResponse&gt;();
  
          public LoginServlet() {
              super();
  
          }
  
          public void init() throws ServletException {
              super.init();
  
              sc = getServletContext();
  
          }
  
          protected void doGet(HttpServletRequest request,
                  HttpServletResponse response) throws ServletException, IOException {
  
          }
  
          protected void doPost(HttpServletRequest request,
                  HttpServletResponse response) throws ServletException, IOException {
  
  
              synchronized (connections) {
                  connections.add(response);
              }
              ServletContext sc = getServletContext();
              Integer x = (Integer) sc.getAttribute("hit");
  
              if (x == null)
                  x = 1;
              else
                  x++;
              sc.setAttribute("hit", x);
  
              String username = request.getParameter("username");
              String password = request.getParameter("password");
  
              HttpSession session = request.getSession(true);
  
              session.setAttribute("username", username);
              session.setAttribute("password", password);
              response.setContentType("text/html");
  
              ArrayList&lt;LoggedInUser&gt; loggedInUsers = (ArrayList&lt;LoggedInUser&gt;) sc
                      .getAttribute("users");
  
              if (loggedInUsers == null) {
                  System.out.println("loggedInUsers creates");
                  loggedInUsers = new ArrayList&lt;LoggedInUser&gt;();
  
              }
              loggedInUsers.add(new LoggedInUser(username, password));
  
              sc.setAttribute("users", loggedInUsers);
  
              PrintWriter out = response.getWriter();
              System.out.println("loggedInUsers ==&gt;" + loggedInUsers.size());
              if (username != null &amp;&amp; username.equals("admin")) {
                  out.println("Admin Login Success");
                  ServletContext scx = getServletContext();
                  Integer xx = (Integer) scx.getAttribute("hit");
  
                  out.println("&lt;H1&gt;Users visited " + xx + "&lt;/h1&gt;");
                  loggedInUsers = (ArrayList&lt;LoggedInUser&gt;) sc.getAttribute("users");
  
                  for (int i = 0; i &lt;= loggedInUsers.size() - 1; i++) {
                      LoggedInUser LoggedInUser = loggedInUsers.get(i);
                      out.println(LoggedInUser.getmUsername()+ "&lt;br&gt;");
                  }
  
                  for (int i = 0; i &lt; connections.size(); i++) {
  
                      try {
  
                          LoggedInUser LoggedInUser = loggedInUsers.get(i);
                          PrintWriter writer = connections.get(i).getWriter();
                          System.out.println("inside loope " + i + "\n" + writer);
                          writer.println("MESSAGE FROM ADMIN HERE" + "&lt;br&gt;");
  
                          writer.flush();
                          writer.close();
                      } catch (IOException e) {
                          log("IOExeption sending message", e);
                      }
  
                  }
  
              } else {
                  out.println("Welcome   " + username + "   Login Success");
              }
          }
      }
  </code></pre>
  
  <p>As soon as user logs in stored resp obj in arraylist.</p>
  
  <pre><code>synchronized (connections) {
      connections.add(response);
  }
  </code></pre>
  
  <p>And later using that obj got the printwriter obj.</p>
  
  <pre><code>PrintWriter writer = connections.get(i).getWriter();  
  </code></pre>
  
  <p><strong>Questions:</strong></p>
  
  <ol>
  <li>How to send one specific msg to all logged In users?</li>
  <li>Without comet can we achieve this?    </li>
  </ol>
  
  <p>Kindly correct the code if anything wrong I have done and help with other suggestions. Thanks</p>