Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

when to use beans

Status
Not open for further replies.

pigsie

Programmer
Dec 19, 2000
307
0
0
GB
Hi

I am new to JSP and am trying to figure out when to use beans. Basically I have a system where i would like to have some objects held in ram but instead of using beans i can use objects is there any reason to use one method over the other? Does one perform better than the other?

Thanks
 
Are you referring to JavaBeans or Enterprise JavaBeans. A standard JavaBean is just a plain Java class with getter and setter methods. This definitely suffices for most web only applications.
 
What would an Enterprise JavaBean be?? ________________________
JoelMac
ICQ#:48144432
 
I was referring to java beans for a web application

I cant see any advantages to using beans
over classes are there any?

Thanks
 
JavaBeans are just plain classes !!! jou only put some set and get methods in it to fill or read it's variables from your jsp.

As far as I know, Enterprise JavaBean (which came with J2EE) are classes that sit over a database connection, or maintain process information. You use them to get an extra level of structure (abstraction) in your application.
 
Yeah. A JavaBean IS a class. It is a specialized class. If you follow the rules for making JavaBeans it will make your life easier that just making the class your way. It does things for you. Example JavaBean:

public class MyString {

String str1;

public void setStr1(String s){str1 = s;}
public void getStr1(String s) {return str1;}
}

Now in your JSP you can use this:

<html><body>
<jsp:useBean name=&quot;MyString1&quot; class=&quot;MyString&quot;>
<jsp:setProperty name=&quot;MyString1&quot; property=&quot;str1&quot; value=&quot;I&quot;>
Here is my string: <jsp:getProperty name=&quot;MyString1&quot; property=&quot;str1&quot;>
</body></html>

Notice that I never call the methods getStr1() or setStr1() in the JSP. How does it know which method to call? Simple. If you follow the pattern of making your metod names set + &quot; capped variable name&quot; or get + &quot;capped variable name&quot;, the JSP will know what to do. It knows because you put in jsp:getProperty or jsp:setProperty. It puts two and two together for you. If you do not do this, things will get more difficult for you. Much more difficult. This is one advantage to creating a class in a JavaBean format.

Remember, a JavaBean IS a class. It is just a class that follows certain rules as to how it is written. Those rules help it do its specific role in a MVC environment.

Enjoy!
 
Thanks for the reply. SO are you saying that the rules
of javabeans make it easier for a developer to know what methods are needed to set/get properties?

Also should beans be used to hold state information? for example if i have an application which has logged in users would it be better to use a bean to hold the user session info short term(given that it may be updated) or place a user object in the session scope?

thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top