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
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 ]
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
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.
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
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