Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

ArrayList- How Safe to use in Jsp  RSS feed

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is it a good practice to use Arraylist in jsp compared to vector and all other collection object ?

thanks
Mahendra
SCJP,SCBCD,ICED
 
Bartender
Posts: 1844
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In general I would say ArrayList is preferred to Vector.

I would just declare it as a List, as far as a JSP is concerned. That way it doesn't need to bother what the actualy implementation is.

Is it good practice? It depends what you are using the list for, and how you are using it.
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your data is not shared or read only, ArrayList would be sufficient. If you need to ensure thread safe access to your list, then Vector or other synchronized list (Like those provided in J2SE 5.0) would be more appropriate.

Definitely you can safely use ArrayList for concurrency access, provided that you implements all the necessary synchronization yourself correctly.
 
mahendra survase
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies fellows

Eddy Lee Sin �do you mean not sharing the data among other threads by not creating them in Declaration tag <%! %>.

Thanks
Mahendra
 
Stefan Evans
Bartender
Posts: 1844
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your data is shared and you want a synchronized list, then just use Collections.synchronizedList(myArrayList), and voila - you have one.
IMO, Vector should be treated as deprecated, and only used when absolutely necessary - ie when imposed on you by someone elses API.


Declaring variables in <%! %> is NOT threadsafe.
However the better approach is to eliminate scriptlet code - don't declare any Lists at all - just use ones from the request/session/application attributes.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!