Solved

Use of Java beans

Posted on 2003-12-02
7
770 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
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

In this article, you will read about the trends across the human resources departments for the upcoming year. Some of them include improving employee experience, adopting new technologies, using HR software to its full extent, and integrating artifi…
Owning a franchise can be the dream of a lifetime. It provides a chance for economic growth. You can be as successful as you want.  To make your franchise successful, you need to market it successfully. Here are six of the best marketing strategies …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

705 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now