Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8263797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T04:13:33+00:00 2026-06-08T04:13:33+00:00

I have developed a servlet in which user enters the name and upon clicking

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T04:13:34+00:00Added an answer on June 8, 2026 at 4:13 am

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have developed a small administration page for game servers which you can add/remove/edit
I have developed a simple server using Tomcat which runs a servlet. The servlet
I have developed an osgi plugin for Domino. It contains a servlet which needs
I have developed servlet on apache tomcat which is having mysql connectivity. and android
I have developed an application which contains servlet and few java classes and additionally
We have a locally-developed triple store based on b-trees which I want to use
I have developed Java Servlet on Netbeans IDE and now wish to deploy them
I have developed a web site that requires user registration and authentication for some
I have developed a website using Razor (Microsoft WebMatrix) and now I want to
i have developed an app which executes sql jobs. When I click on execute

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.