Solved

Use of Java beans

Posted on 2003-12-02
7
774 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

 

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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Suggested Solutions

Title # Comments Views Activity
session bean client machine 9 63
Can't access image with tomcat/html 6 102
Market Place not working in Eclipse Luna 2 118
jsp insert to database example 2 45
IT certifications are a concrete representation of continual learning on the part of the candidate.  Continual learning is necessary for the long term success of an IT professional, but are IT certifications the right path for you?
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

839 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