Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Are javabeans of any use?  RSS feed

 
k Oyedeji
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I am new to jsp and javabeans so i'm trying to decide where its best to use beans. AFAIK beans are held in server ram and thus therefore should not be too resource intensive. Bearing this in mind i see no use for them other than maintaining state which can be maintained else where using implicit objects. So my question is, is it a best practice to use beans and if so for what?
Thanks
 
Simon Brown
sharp shooter, and author
Ranch Hand
Posts: 1913
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With respect to JSP, JavaBeans are useful for maintaining state and for representing the information (the model) of your web application. You can use them to wrap up reusable functionality, although custom tags (also known as JSP tag extensions) are the preferred solution.
For a more detailed look at what JavaBeans and custom tags are useful for, check out Chapter 8 (Tag Patterns) of Professional JSP Tag Libraries that is available to download for free.
Hope that's useful
Simon
[ September 26, 2002: Message edited by: Simon Brown ]
 
k Oyedeji
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is what i dont understand. I would have thought for reusable functionality the fact that beans stay in memory would mean lots of additional use of server ram for a site that was built in this way. Is this acceptable (i.e. does not harm performance that much)?
thanks
 
Ken Robinson
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Common practice is to have a Servlet place the bean in an object available to the JSP Page (Request, Session, Context). Usually the Request is used. Once the page is rendered, the Request goes out of scope. Once it goes out of score, all beans placed in it go out of scope. The beans will not stay in memory for long.
The alternative is to create a bean pool type object that recycles the beans.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The main reason for using JavaBeans or any other helper class is to get the Java code out of JSP. The theory being that this helps separate content generation from presentation and makes the whole package more maintainable.
After you have spent countless hours trying to debug a huge JSP that mixes code and HTML you may come to appreciate JavaBeans.
Personally I like the idea of writing a separate test application to thoroughly wring out a JavaBean before using it in JSP.
Bill
 
k Oyedeji
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Thanks for the replies. I think its starting to click. Am i right in saying tags exist mainly to access these beans (so that you dont have scriplets and html intermingled). And if so what advantages does using beans have over regular classes ? I think because so many things can be done so many ways the hardest thing to grasp is what to use where and why (i.e what tradeoffs have to be made etc.) in anysight into this or pointers to resources would be gratefully helpful!
Thanks
Kola
 
k Oyedeji
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry one other thing i wanted to ask, why do servlets place the bean in the request scope why not just do it in the page, is this again to avoid having html and java intermingled?
thanks
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!