Solved

Use of Java beans

Posted on 2003-12-02
7
776 Views
Last Modified: 2012-06-27
My ques is a very basic one.
What is the use of beans in JSP.
If I make a form in html and submit it to a jsp page where I receive it  using

request.getParameter("var1");

and insert it into the database.
The other method is to send it to a bean using
      <jsp:setProperty name="TheBean" property="*" />

Which method is better and why.
The same thing i'd like to know when retreiving data.
I retreive the data in jsp page and display it in HTML table

<TD><%=rs.getString("col1")%></TD>

The second method could be to retreive the data in the bean and concatenate it to a variable

      showData=showData+"<td>";
            showData=showData+col1;
            showData=showData+"</td>";
And then fetch the variable in the JSP page and print it there.
Which method of the 2 is better. Moreover when I use the method of beans to retreive data it makes the application very slow. What could be the reason for that?
Kindly Suggest
          
0
Comment
Question by:linda22
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 19

Assisted Solution

by:Kuldeepchaturvedi
Kuldeepchaturvedi earned 80 total points
ID: 9857434
Well Beans are something by which you achieve data encapsulation.... in this you do not meddle with your data directly but rather ask bean methods to do it for you..
One apparent benefit is encapsulation..
next is that you put your own Ifs and buts inside beans.... like your database needs some string have to be 20 bytes... so now you bean can pad your data up for your requirements...

This list can go on but I suggest you to go to Java site and have a look at..
http://java.sun.com/products/javabeans/
0
 
LVL 27

Assisted Solution

by:rrz
rrz earned 80 total points
ID: 9859454
>makes the application very slow. What could be the reason for that?
Maybe because of the String class.  
>     showData=showData+"<td>";
            showData=showData+col1;
            showData=showData+"</td>";
Did you try it with StringBuffer class ?
0
 
LVL 4

Accepted Solution

by:
kokchoon78 earned 80 total points
ID: 9864891
>> The second method could be to retreive the data in the bean and concatenate it to a variable

Normally, i will use the second method - retreive the data in the bean and send the bean to the form as request.

for example, in your action class ( which you call database methods) :

...
...
//get database connection ..
//define prepared statement ...
...

ResultSet rs = pstmt.executeQuery();

TheBean theBean = new TheBean();
theBean.setName( rs.getString(1) );
...
...

//add this bean to your request
request.setAttribute("MYBEAN", theBean);

//forward to your jsp for displaying purposes.


In your display jsp, just get the bean from the request :

<%
   TheBean theBean = (TheBean)request.getAttribute("MYBEAN");
%>
...
...
<html>
..

<td><%=theBean.getName()%></td>

..
</html>


opps! write too long, sorry if i am not answering your question :)

cheers,







0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:linda22
ID: 9872148
Thanks everyone for  suggestions.
Another thing i want to know. What sort of code should be placed in the JSP page?
Should it be only calls to methods in java beans and some HTML or something else?
Can there be any situation when make making a connection and performimg DML or select operations be done in the JSP page or they should always be done in the bean.
0
 

Author Comment

by:linda22
ID: 9895741
Points increased to 240 from 190
0
 
LVL 4

Expert Comment

by:kokchoon78
ID: 9900653
>>What sort of code should be placed in the JSP page?
Normally, i will put iteration or looping code in the JSP, for example :

<%
   for( int i = 0 ; i < thisList.size(); i ++ )
   {
%>
<html tag>
<%
  }
%>

>>Can there be any situation when make making a connection and performimg DML or select operations be done in the JSP page or they should always be done in the bean.

Basically, In MVC model, JSP page plays the Viewer role, and the logic should be seperated in the java beans.
0
 

Author Comment

by:linda22
ID: 9901875
Thank You so much
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Arrow Electronics was searching for a KVM  (Keyboard/Video/Mouse) switch that could display on one single monitor the current status of all units being tested on the rack.
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question