Hi, I have a welcome.jsp page. I am also keeping track of users logged in. Once a user logs in, I update an attribute in ServletContext. Besides, his user deials, I also store a reference to his request and response objects in the ServletContext. Whenever a new user logs in he is shown the welcome page. But I also want to notify all the other users that are logged in and show them a specific page telling them about the user who just logged in. I am trying to do it through ServletContextAttributeListener. But when I fetch the request objects stored (for users already logged) from the ServletContext and try to send them a message about the new user, by saying (requestObject.getRequestDispatcher("/notifyOtherUsers")).include(requestObject, responseObject), I do not get any result. There is no exception thrown either and the Servlet does not execute beyond this line. If I comment out the line, the servlet executes further without problems. Can I use the previously stored request objects this way to obtain a RequestDispatcher and then to include a page to those response objects? Can the other logged in users be notified this way or they have to click on a "refresh" link somewhere to see who is the latest user that logged in! Thanks in advance.
Joined: Jan 07, 1999
I'm afraid this approach simply won't work. HTTP is a "stateless" request/response protocol: a browser sends a request, the server sends something back, and it's over. There is no way to send something from the server to a browser without the browser requesting it. This fundamental fact about HTTP shaped the way the servlet API was designed. Request and response objects exist only while a request is being serviced. As soon as the server has sent the response, both the request and response objects are "cleaned up" by the server (often they are reused for another request, sometimes they are just emptied and destroyed). In most applications the usual way to achieve what you are asking is to provide a "refresh" link as you suggest. However, it is also possible to get the browser to automatically refresh a page if you really need to. See the entry for "refresh" on this page for more information.