[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Use of Java beans

Posted on 2003-12-02
7
Medium Priority
?
781 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 320 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 28

Assisted Solution

by:rrz
rrz earned 320 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 320 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
Industry Leaders: 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Working from home is a dream for many people who aren’t happy about getting up early, going to the office, and spending long hours at work. There are lots of benefits of remote work for employees.
It’s time for spooky stories and consuming way too much sugar, including the many treats we’ve whipped for you in the world of tech. Check it out!
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

649 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