• Post Reply Bookmark Topic Watch Topic
  • New Topic

Server Wide Arraylist ?  RSS feed

 
Jon Kidd
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
Possibly not the right forum to post in but I'll try anyway as I'm not 100% sure where to ask!!
I'm currently trying to script a simple chat application (yes, another one **yawn** !!) and am trying to do it so that when a user enters they are added to a list of users currently in the room. I thought that somehow I could add their user object to an arraylist that would be available too all the users to call a method on a view a list of all the users in the 'room' ?
Sounds simple enough but I can't seem to find a simple solution, anyone here got any ideas ?
Thanks
Jon
 
Dominic Paquette
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I'm not sure if this is what you want to do but here goes!
You can use ServletContext.get/setAttribute to make any object available to all servlets and JSPs in your entire webapp.
Dominic
 
Dominic Paquette
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I guess your using JSPs! for this you would have to use code like this in your JSP
<%
ArrayList users = new ArrayList();
// add users to the users list
...
// set in application scope
application.setAttribute("userslist", users);
%>
and in another JSP you can access the users list like this:
<%
synchronized(application) {
ArrayList users = (ArrayList)application.getAttribute("userslist);
}
%>
Hope this helps
Dominic
 
Jon Kidd
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Dominik, I'll give that a try !!
 
Annina WG
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,I am also doing with a chat application.
The difference is that I use both servlet and JSP.
I create an object of Bean class in Servlet and use "ServletContext.setAttribute()" to make it available.
and in JSP side I use <jsp:useBean id="user" class="Web-inf/classes/package/UserList" scope="application" /> to read the list out.
I think this is the right way, but I cannot get the Beans .
could anybody help? I have been stopped here for servral days
[ January 09, 2003: Message edited by: Annina WG ]
 
Dominic Paquette
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I think there is a problem with your class attribute in your <jsp:useBean> tag. Here is how I did it:
1- Create a bean class
package dominic.beans;
import java.io.*;
class MyBean implements Serializable {
...
}
2- compile the class
3- put the class file in "/yourwebapp/WEB-INF/classes/dominic/beans" directory. Notice that WEB-INF is in upper case.
4- in your servlet put the bean in the session
session.setAttribute("theBean", myBean);
5- in your jsp do this to extract the bean from the session
<jsp:useBean id="theBean" class="dominic.beans.MyBean" scope="session/>
This should work, let me know if it dosen't
Dominic
 
Annina WG
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dominic Paquette:

3- put the class file in "/yourwebapp/WEB-INF/classes/dominic/beans" directory. Notice that WEB-INF is in upper case.

this is what I cannot do at the present.I will let you know next week.
Nice week end!
 
Annina WG
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes,this works. I have got my result
Thanks very much!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!