By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,480 Members | 1,146 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,480 IT Pros & Developers. It's quick & easy.

what are the objectives of javabeans?

P: 40
i know javabean is nothing but a class written in java language

the business logic is written in javabean

but, i want know the advantages and objectives of using javabeans

question : suppose,if i am using jsp to write a program
i can write business logic in jsp then why to use javabeans in jsp...

example:writing business logic in jsp itself
<html>
<body>
<%
int i=1,j=2;
int k;
k=i+j;
out.println(k);
%>
</body>
</html>

writing business logic in a javabean as a separate class

<html>
<body>
<jsp:useBean id="addition" class="beans.add" />
<jsp:setProperty name="addition" property="value1" value="1" />
<jsp:setProperty name="addition" property="value2" value="2" />

after addition<jsp:getProperty name="addition" name="totalvalue" />
</body>
</html>

beans.add class

public class add
{
public int i,j,k;
public add()
{
i=j=k=0;
}
public void setvalue1(int i1)
{
i=i1;
}
public void setvalue2(int j1)
{
j=j1;
}
public int gettotalvalue()
{
k=i+j;
return(j);
}

}


please,someone help me in solving my problem....
Jul 22 '08 #1
Share this Question
Share on Google+
1 Reply


BigDaddyLH
Expert 100+
P: 1,216
1. There's more to being a JavaBean class, than, ugh, being a class. Primarily, "properties" can be exposed through a naming convention:

Expand|Select|Wrap|Line Numbers
  1. public String getName() {...}
  2. public void setName(String name) {...}
2. While you can slap any code you can imagine onto a JSP, including code that could be refactored into JavaBeans, servlets, filters, etc... why do any of this?

To write better code.

One can point out how doing this helps you share and reuse code, sometimes coders will respond, "this is my only project, I don't care about code reuse".

For me the following is sufficient motivation to write better code: being able to test your code.

In particular, being able to unit-test individual components of code. It is notoriously difficult to test presentation-layer code the JSP or GUI code (say, Swing). Smart programmers make this layer as simple as possible so that there is less to go wrong! So, move as much as you can out of the JSP, and you'll thank yourself every time you run a suite of unit tests.
Jul 22 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.