0

I have developed a servlet in which user enters the name and upon clicking of a button of submit it greets the user , I also want to add the functionality that it should show the number of users currently logged in for example If i hit the servlet from one browser it should show the number of users logged in is 1 as I am the first user and if someone other hit the servlet from the next browser it should show him he is the 2 visitor , so in this way it will show him the number of users currently logged in ,please advise how to achieve that..

The web.xml for the project is ..

   <?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>


  <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> 

</web-app>

The HTML is ...

<html>
<head>
<title> A Simple web Application</title>
</head>
<body>
<form method="get" action="helloServlet">
Name<input type="text" name="txtName">
<br><input type="submit" value="Login">
</form>
</body>
</html>

Please advise how to add the functionality of the numbers of users logged in. Below is the snapshot of my project structure is..enter link description here

I have added the counter class below as shown...

package com.saral;

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


public class SessionCounter implements HttpSessionListener
{
        private static int count;

        public SessionCounter()
        {
        }

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

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

and I have modified my servlet also to this..

import java.io.IOException;
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;


    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String name=request.getParameter("txtName");
        response.setContentType("text/html");
        request.getAttribute("sessionCount");
        PrintWriter out=response.getWriter();
        out.println("Hello,"+name);
        out.println("<br> this output is generated by a simple servlet.");
        out.println("--->"+(request.getAttribute("sessionCount")));
        out.close();


    }

}

But the session I am getting is null...in output..http://imageshack.us/photo/my-images/405/sessionoutput.jpg/output of session

Please advise how can I get the value there I am getting null rite now , I want as I MAKE first request it shoud show 1 and if I open second it should show 2 , Please advise how to achieve that as I am stuck up on this .

user1538526
  • 95
  • 7
  • 22

1 Answers1

0

You can store a simple POJO in application scope where you can maintain the count.

devang
  • 5,376
  • 7
  • 34
  • 49
  • ..could you please post it here the code that will make understanding more clear – user1538526 Jul 22 '12 at 16:06
  • See examples http://techdive.in/java/how-find-number-users-logged-web-application and http://stackoverflow.com/questions/4592961/how-can-i-check-which-all-users-are-logged-into-my-application – devang Jul 22 '12 at 16:14
  • Thanks a lot I have done that and based upon it I have made the sessioncounter class also but in main servlet how to retrieve it please advise and see my updated post.thanks – user1538526 Jul 23 '12 at 03:52
  • You need to access in following way `request.getSession().getServletContext().getAttribute("sessionCount")`. `request.getAttribute("sessionCount")` is all-together different scope, you wont have that variable in request scope. – devang Jul 23 '12 at 05:04