• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

How to store array in servlet context???

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all, I am making an chat application, in which I am mainting the current login list to an array.. which is defined in Chatter.java. Now, I am invoking the addChatter() and getChatter() from that class to add and retrive the currently login user's list...

But when I login from another machine, while one is login it doesn't show me the all the list..i.e. 2 users. It shows only the last user that has been login . One of my friend says that it's an scope problem that u have to make it as whole application scope..that anyone can use that array.. without it is being initilized.. and for that you have to store that array somewhere in servlet context..

But I don't know that how to store and retrive the string array and its element from servlet context()..

So, please help me regarding the same....

Regards,
Siddharth
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all it is not good idea to use arrays to store your login user list. Instead of that you can use Collection framework. eg.ArrayList to store user names. If you are using Servlets in your service(get/post) you can call the following .

ServletContext sc = getServletContext();
ArrayList al = sc.getAttribute("loginList");
if(al == null)
al= new ArrayList();
al.add("username");
sc.setAttribute("loginlist",al);

you have to consider synchronization issues.
 
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. Consider synchronization issue
2. Use Set rather than ArrayList.
 
Siddharth Ramavat
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply...

I am able to store the name of the user inside the arraylist, but the problem that is i am currently facing is that it stores one name only at a time... I have already mentioned that for another user I am opening another Login window. So it gets reset when I do the login from the another window..

What is the reason behind it..? Hope you will help me regarding the same??

Should I edit my web.xml or I have to modify it?

Waiting for your reply,
Regards,
Siddharth
 
Ranch Hand
Posts: 225
IBM DB2 Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi siddharth ..

i am really astonished though u had stored all data releating to user info in arraylist which is basically not sync'zd .how when open in another window re intailies the arryList content though u have used if (arrylist obj == null) then only u are creating the new arrayList obj there by storing the data ...

G R Gowtham grgowtham_cse@yahoo.com
 
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Siddharth Ramavat:
I am able to store the name of the user inside the arraylist, but the problem that is i am currently facing is that it stores one name only at a time... I have already mentioned that for another user I am opening another Login window. So it gets reset when I do the login from the another window..

What is the reason behind it..? Hope you will help me regarding the same??

Should I edit my web.xml or I have to modify it?



Where are you initializing you ArrayList? Need some code snippet.

Use Vector instead. It is similar to ArrayList the only distinction is, it is synchronized.

or you can do this,




Moreover, The Iterators returned by Vector's iterator and listIterator methods are fail-fast. But the Enumerations returned by Vector's elements method are not fail-fast. for more refer to javadocs.
 
Paper beats rock. Scissors beats tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic