Solved

Use of Java beans

Posted on 2003-12-02
7
773 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need to view directory list in apache/tomcat 9 115
Lamba in java 8 9 242
spring JDBC Template example error 26 238
countXY challenge 28 151
Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
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…

770 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