Note that there are some explanatory texts on larger screens.

plurals
  1. POAdding the functionality to the servlet
    text
    copied!<p>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..</p> <p>The web.xml for the project 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;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;/web-app&gt; </code></pre> <p>The HTML is ...</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;title&gt; A Simple web Application&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;form method="get" action="helloServlet"&gt; Name&lt;input type="text" name="txtName"&gt; &lt;br&gt;&lt;input type="submit" value="Login"&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Please advise how to add the functionality of the numbers of users logged in. Below is the snapshot of my project structure is..<a href="http://imageshack.us/photo/my-images/685/39284405.jpg/" rel="nofollow">enter link description here</a></p> <p>I have added the counter class below as shown...</p> <pre><code>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)); } } } </code></pre> <p>and I have modified my servlet also to this..</p> <pre><code>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("&lt;br&gt; this output is generated by a simple servlet."); out.println("---&gt;"+(request.getAttribute("sessionCount"))); out.close(); } } </code></pre> <p>But the session I am getting is null...in output..http://imageshack.us/photo/my-images/405/sessionoutput.jpg/<a href="http://imageshack.us/photo/my-images/405/sessionoutput.jpg/" rel="nofollow">output of session</a></p> <p>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 .</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload