what are the objectives of javabeans?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shivapadma
    New Member
    • Mar 2007
    • 40

    what are the objectives of javabeans?

    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.ad d" />
    <jsp:setPropert y name="addition" property="value 1" value="1" />
    <jsp:setPropert y name="addition" property="value 2" value="2" />

    after addition<jsp:ge tProperty name="addition" name="totalvalu e" />
    </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....
  • BigDaddyLH
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #2
    1. There's more to being a JavaBean class, than, ugh, being a class. Primarily, "properties " can be exposed through a naming convention:

    [CODE=Java]public String getName() {...}
    public void setName(String name) {...}[/CODE]

    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.

    Comment

    Working...