File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes why we need javabeans? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "why we need javabeans?" Watch "why we need javabeans?" New topic
Author

why we need javabeans?

suseela surendra
Greenhorn

Joined: Dec 26, 2006
Posts: 3
we are able to connect to the database by using jdbc in jsp and servlets. Then what is the necessity of javabeans in jsp and servlets for getting date from the database?

Pls explain.....
Jigar Naik
Ranch Hand

Joined: Dec 12, 2006
Posts: 757
[Bear edit: uncited copy removed.]
[ December 28, 2006: Message edited by: Bear Bibeault ]

Jigar Naik


Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61309
    
  66

Jigar Naik, when you copy information from another site, either provide a link or give credit to the source where you found the information. Copying information and passing it off as your own is not an ethical action.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61309
    
  66

suseela surendra,

"Pls" is not a word. Please use real words such as "please" when posting.
Anu Pillai
Greenhorn

Joined: Oct 09, 2006
Posts: 28
JDBC is used to make a connection to the database and to retreive data from the tables. But you need a place to store the data that you retreive from the tables. This is where java beans are used. The data retreived from the tables are stored in JavaBean. And these JavaBean objects are passed between the servlets and the jsp according to the business logic.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61309
    
  66

Though their use is not limited to data obtained via JDBC.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by suseela surendra:
we are able to connect to the database by using jdbc in jsp and servlets. Then what is the necessity of javabeans in jsp and servlets for getting date from the database?....


You don't need them.
It's entirely possible to write an entire web application using only servlets.
Doing so, however, would be very time consuming, tiring, and would result in code that is very difficult to debug and maintain. By breaking your app up into smaller, more specialized components, you make it easier to re-use code, and your code becomes cleaner and easier to read.
Servlets are great for handling web flow. JSPs are better for formatting the output. By moving your database operations out of the servlets, into Javabeans or plain old Java objects, you make them easier to test, debug, maintain, and reuse.

Personally, I like to be able to test all of my database components from the command line before trying to incorporate them into a servlet app.
This speeds up development but is even more valuable later on when trying to debug something that is not working as expected.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Vivek tcs
Greenhorn

Joined: Dec 29, 2006
Posts: 2
the use of java bean is that it manages the data that can be large enough. Which can't be retrieved directly. And also it is used to pop up the values. You can think it of a bag in which we are storing values and later taking them out.
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

The idea of a JavaBean is that it is reusable, it is testable, and it conforms to a variety of naming conventions and standards. Furthermore, it should be serializable - you must be able to pour milk over it and have it for breakfast.

A question was just asked in the JSP forum about "how do you debug JSP files?" The answer? Not easily. But if your code is in a JavaBean, testing becomes easier. Plus, how do you reuse code in a JSP? Basically, you can't.

For JDBC, I might suggest going beyond just a JavaBean, and factoring out an entire database layer. MVC-D is important. With so many different and emerging data models,(EJB 2.0, EJB 3.0, Hibernate, JDO, JDBC, etc) being able to plug and play your data tier is essential if you want a maintainable and flexible solution.

You don't have to use JavaBeans. There are no Java cops out there. But then again, fear of prosecution should never be the most compelling reason to do the right thing. Using JavaBeans is the right thing to do!

-Cameron McKenzie
Cristiano Sganzerla
Ranch Hand

Joined: Jan 14, 2006
Posts: 44
Plus, how do you reuse code in a JSP? Basically, you can't.


With include directive?


SCJP 1.4 - SCWCD 1.4
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61309
    
  66

Originally posted by Cristiano Sganzerla:


With include directive?


That's a really poor and hard-to-manage technique for code reuse.
Neerav Narielwala
Ranch Hand

Joined: Dec 08, 2006
Posts: 106
I think it demonstrates the visual construction of applications using component assembly mechanisms.


<a href="http://www.java-tips.org/java-tutorials/tutorials/" target="_blank" rel="nofollow">Java Tutorials</a> | <a href="http://www.planet-java.org" target="_blank" rel="nofollow">Java Weblog</a> | <a href="http://computer-engineering.science-tips.org" target="_blank" rel="nofollow">Computing Articles</a>
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
This would definitely help.
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Beans are nothing but java data carrier.


Rahul Bhattacharjee
LinkedIn - Blog
ismail hassani
Greenhorn

Joined: Feb 20, 2006
Posts: 9
To reply to the question,with JavaBeans you can handle transaction,security and workflow features better as with servlets.
[ January 05, 2007: Message edited by: ismail ]
Robert Berg
Greenhorn

Joined: Dec 29, 2006
Posts: 10
Plus, how do you reuse code in a JSP? Basically, you can't.

The best way to reuse code in JSP is by creating and using Tag Libraries.


<a href="http://exceptionnull.net" target="_blank" rel="nofollow">Exception: null</a>
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61309
    
  66

"ismail",

There aren't many rules that you need to worry about here on the Ranch, but one that we take very seriously regards the use of proper names. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
bear
JavaRanch Sheriff
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61309
    
  66

Originally posted by Robert Berg:

The best way to reuse code in JSP is by creating and using Tag Libraries.


That's a really broad statement. Custom actions are one way (and a very powerful one) for creating re-usable code, but it's not always the best way.
 
GeeCON Prague 2014
 
subject: why we need javabeans?