Tuesday, April 12, 2011

Chapter 38: Introduction to JavaBeans

JavaBeans is a very useful component of any J2EE application. They are used in almost every Web application that uses the J2EE Technology. In the next few chapters, we are going to look in detail about using these JavaBeans in our JSP & Servlet based Web Applications.
This chapter is just the introduction and it is just to familiarize you with the concept of JavaBeans.

So, lets get Started!!!

What are JavaBeans?

Before we begin explaining what the JavaBeans are, that are used in Web Applications, we need to clarify one important thing.

Web Application JavaBeans are not the same as Enterprise JavaBeans. EJBs are a totally different entity and thankfully they are not part of the SCWCD Exam.

Programmers easily confuse Web application JavaBeans with Enterprise JavaBeans. JavaBeans and Enterprise JavaBeans are two entirely different things sharing the same name due to their use for similar purposes.

Regular JavaBeans and the Enterprise JavaBeans are similar in only one way – They are both used to encapsulate the business logic of an application.

Since EJBs are pretty complicated and also since they are not part of the exam, I am not going to waste any of your time by talking about them. Lets just cut the chase and get down to business with the regular Java Beans.

The way Sun has designed JavaBeans for use in a JSP environment ignores much of JavaBeans' original GUI focus. While it is possible to have a JSP IDE which represents beans graphically, making visual components for presentation, the original JavaBeans motivation is left out completely. The name indicates reusable components, so they kept it along with the read/write (get/set method convention) properties and some of the other characteristics. In JSP, we have a cut-down version of JavaBeans. In other words, JavaBeans are not used for GUI representation in JSPs.

That being said, JSPs can get ugly and hard to maintain if the number of scriptlet lines in a JSP exceeds 200. Of course, in a J2EE application that uses JSPs and Servlets, you can always use servlets. The point here is that you can always use JavaBeans in a JSP page but you must use them cautiously and in a controlled manner. Too much JavaBeans in a JSP can make it unmanageable and complicated.

If you ask me, what is the motivation factor behind using JavaBeans in a JSP page – the answer is simple. Beans help us separate out UI display (in HTML) and logic (in Beans) whereas servlets tend to mix the two. Each JavaBean component has a regular Java class structure that follows the get/set method naming conventions. This is actually what transforms a regular java class into a JavaBean.

A JavaBean itself is ordinary. JavaBeans do not extend any specific class. Rather, the container allows JSP to access classes, and this access, along with the JavaBean convention of set/get methods, are the interesting features. A class becomes a JavaBean if it is public, has a public constructor with no arguments, follows the set/get paradigm, and is placed where the container can access it. Once you meet these criteria you can take advantage of the JavaBean, sometimes called simply Bean, by writing the XML tags in JSP that bring that Bean into play. It is more a matter of how you use the class that makes a Bean rather than syntax or inheriting a special class.

Now that we know what the JavaBeans are let us learn how to use them in a JSP. That is exactly what the next few chapters is going to explain. For now, this chapter is over!!!

Previous Chapter: Self Test - Chapters 31 to 37

Next Chapter: Chapter 39 - Creating JavaBeans

No comments:

Post a Comment

© 2013 by www.inheritingjava.blogspot.com. All rights reserved. No part of this blog or its contents may be reproduced or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the Author.

ShareThis

Google+ Followers

Followers