Introduction to JavaBeans Objectives: • Understand the benefits of JavaBeans • Learn how to create JavaBeans • Utilize JavaBean classes • Demonstrate how to access JavaBean properties • Explicitly setting JavaBean properties from request parameters Concepts As we have discussed on our previous lesson, (Using Regular Classes in JSP), we understood the benefits of using separate Java classes instead of embedding large amounts of code directly in JSP pages. But what do JavaBeans provide that other classes do not? After all , JavaBeans are regular Java classes that follow some conventions defined by the JavaBeans specification; JavaBeans extend no particular class, are in no particular package and use no particular interface . True, but with JavaBeans in general, visual manipulation tools and other programs can automatically discover information about classes that follow this format and can create and manipulate the classes without user having to explicitly write any code. The Advantages of JavaBeans in JSP in Particular are: 1. No Java syntax . By using JavaBeans, page authors can manipulate Java objects using only XML- compatible syntax. This promotes a stronger separation between the content and the presentation and is especially useful in large development teams that have separate Web and Java Developers. 2. Simpler object sharing . When you use the JSP JavaBean constructs, you can much more easily share objects among multiple pages or between requests than if you use the equivalent Java code. 3. Convenient correspondence between request parameters and object properties . The JSP JavaBean constructs greatly simplify the process of reading request parameters, converting from strings and putting the results inside objects. What are JavaBeans - How To Code a JavaBean 1. A JavaBean must contain a constructor that does not accept any arguments. 2. A JavaBean cannot declare any public instance variables. However, it is also possible to declare instance variables as protected. 3. A JavaBean must contain get and set methods for all properties that need to be accessed by JSPs.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Introduction to JavaBeans
Objectives:
• Understand the benefits of JavaBeans
• Learn how to create JavaBeans
• Utilize JavaBean classes
• Demonstrate how to access JavaBean properties
• Explicitly setting JavaBean properties from request parameters
Concepts
As we have discussed on our previous lesson, (Using Regular Classes in JSP), we understood
the benefits of using separate Java classes instead of embedding large amounts of code directly in JSP
pages. But what do JavaBeans provide that other classes do not? After all , JavaBeans are regular Java
classes that follow some conventions defined by the JavaBeans specification; JavaBeans extend no
particular class, are in no particular package and use no particular interface. True, but with JavaBeans in
general, visual manipulation tools and other programs can automatically discover information about
classes that follow this format and can create and manipulate the classes without user having to
explicitly write any code.
The Advantages of JavaBeans in JSP in Particular are:
1. No Java syntax. By using JavaBeans, page authors can manipulate Java objects using only XML-
compatible syntax. This promotes a stronger separation between the content and the
presentation and is especially useful in large development teams that have separate Web and
Java Developers.
2. Simpler object sharing. When you use the JSP JavaBean constructs, you can much more easily
share objects among multiple pages or between requests than if you use the equivalent Java
code.
3. Convenient correspondence between request parameters and object properties. The JSP
JavaBean constructs greatly simplify the process of reading request parameters, converting from
strings and putting the results inside objects.
What are JavaBeans - How To Code a JavaBean
1. A JavaBean must contain a constructor that does not accept any arguments.
2. A JavaBean cannot declare any public instance variables. However, it is also possible to declare
instance variables as protected.
3. A JavaBean must contain get and set methods for all properties that need to be accessed by
JSPs.
o The setter method is just a method that starts with “set” followed by the name of the
property. Take note that the first character of the property is upper-cased.
o The getter method is just a method that starts with “get” followed by the name of the
property. Take note that the first character of the property is upper-cased.
o To provide access to a Boolean value, you code is and set methods instead of get and set
methods.
o Example, you can code methods isEmailUpdated and setEmailUpdated to provide access
to a Boolean property named emailUpdated.
4. A JavaBean implements the Serializable interface.
o The Serializable interface is a tagging interface in the java.io package that indicates that
a class contains get,set and is methods that another class can read and write an object's
instance variables to and from a persistent data source
o Example: your JavaBean is on a server in New York helping the rest of an application in
Los Angeles. The Java application server hosting your JavaBean receives a message that
the server is going down for maintenance, so it contacts another server elsewhere and
arranges for the other server to take over. Not wanting to crash out an end user using
your JavaBean, the Java application server has your JavaBean save its state, using
serialization and it moves the JavaBean location and data to the new server, where the
saved JavaBean data is loaded again and the user continues using the application
without noticing a thing.
JavaBean Tags
The main benefit that you get from coding your classes so they qualify as JavaBeans is that you can then
use JSP tags for working with the beans.
The useBean tag
The <jsp:useBean> element locates or instantiates a JavaBean component. It first attempts to locate an
instance of the Bean. If the JavaBean does not exists, <jsp:useBean> instantiates it from a class or
serialized template.
To locate or instantiate the Bean, <jsp:useBean> takes the following steps, in this order:
1. Attempts to locate a Bean with the scope and name you specify.
2. Defines an object reference variable with the name you specify.
3. If it finds the Bean, stores a reference to it in the variable. If you specified type, gives the Bean
that type.
4. If it does not find the Bean, instantiates it from the class you specify, storing a reference to it in
the new variable. If the class name represents a serialized template, the Bean is instantiated
byjava.beans.Beans.instantiate.
5. If <jsp:useBean> has instantiated (rather than located) the Bean, and if it has body tags or
elements (between<jsp:useBean> and </jsp:useBean>), executes the body tags.