0% found this document useful (0 votes)
7 views12 pages

Understanding JavaBeans Basics and Usage

JavaBeans are reusable software components that require a no-arg constructor, must be Serializable, and provide getter and setter methods for property access. They encapsulate multiple objects into one, allowing for easier maintenance and reuse, although they can lead to boilerplate code and are mutable. The jsp:useBean action tag is used in JSP to instantiate or locate a bean class, with various scopes for its usage.

Uploaded by

MONALI DESHPANDE
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views12 pages

Understanding JavaBeans Basics and Usage

JavaBeans are reusable software components that require a no-arg constructor, must be Serializable, and provide getter and setter methods for property access. They encapsulate multiple objects into one, allowing for easier maintenance and reuse, although they can lead to boilerplate code and are mutable. The jsp:useBean action tag is used in JSP to instantiate or locate a bean class, with various scopes for its usage.

Uploaded by

MONALI DESHPANDE
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

JAVA BEANS

JAVA BEANS
o It should have a no-arg constructor.
o It should be Serializable.
o It should provide methods to set and get the values of
the properties, known as getter and setter methods.
Importance of JAVA Beans
• It is a reusable software component.
• A bean encapsulates many objects into one object so
that we can access this object from multiple places.
• It provides easy maintenance.
Example of JAVA Bean Class
1.//[Link]
2.
[Link] mypack;
[Link] class Students implements [Link]{
[Link] int id;
[Link] String name;
[Link] Students(){}
[Link] void setId(int id){[Link]=id;}
[Link] int getId(){return id;}
[Link] void setName(String name){[Link]=name;}
[Link] String getName(){return name;}
12.}
Access JAVABEAN Class
[Link] mypack;
[Link] class Test{
[Link] static void main(String args[]){
[Link] e=new Students();//object is created
[Link]("Amit");//setting value to the object
[Link]([Link]());
7.}}
JavaBean Properties

• A JavaBean property is a named feature that can be accessed by the user


of the object. The feature can be of any Java data type, containing the
classes that you define.
• A JavaBean property may be read, write, read-only, or write-only. JavaBean
features are accessed through two methods in the JavaBean's
implementation class:
• 1. getPropertyName ()
• For example, if the property name is firstName, the method name would
be getFirstName() to read that property. This method is called the
accessor.
• 2. setPropertyName ()
• For example, if the property name is firstName, the method name would
be setFirstName() to write that property. This method is called the mutator.
Advantages of JavaBeans
• The JavaBean properties and methods can be exposed
to another application.
• It provides an easiness to reuse the software
components.
Disadvantages of JavaBean

• The following are the disadvantages of JavaBean:


• JavaBeans are mutable. So, it can't take advantages of
immutable objects.
• Creating the setter and getter method for each property
separately may lead to the boilerplate code.
jsp:useBean action tag

• The jsp:useBean action tag is used to locate or instantiate a


bean class. If bean object of the Bean class is already created, it
doesn't create the bean depending on the scope. But if object of
bean is not created, it instantiates the bean.
• Syntax of jsp:useBean action tag
1.<jsp:useBean id= "instanceName" scope= "page | request | ses
sion | application"
[Link]= "[Link]" type= "[Link]
Name"
[Link]="[Link] | <%= expression >" >
4.</jsp:useBean>
Attributes and Usage of jsp:useBean action tag

[Link]: is used to identify the bean in the specified scope.


[Link]: represents the scope of the bean. It may be page, request, session or
application. The default scope is page.
1. page: specifies that you can use this bean within the JSP page. The default scope is page.
2. request: specifies that you can use this bean from any JSP page that processes the same
request. It has wider scope than page.
3. session: specifies that you can use this bean from any JSP page in the same session whether
processes the same request or not. It has wider scope than request.
4. application: specifies that you can use this bean from any JSP page in the same application. It
has wider scope than session.
[Link]: instantiates the specified bean class (i.e. creates an object of the bean class)
but it must have no-arg or no constructor and must not be abstract.
[Link]: provides the bean a data type if the bean already exists in the scope. It is
mainly used with class or beanName attribute. If you use it without class or
beanName, no bean is instantiated.
[Link]: instantiates the bean using the [Link]() method.
Simple example of jsp:useBean
action tag
• In this example, we are simply invoking the method of the Bean class.
• For the example of setProperty, getProperty and useBean tags, visit next page.
• [Link] (a simple Bean class)
1. package [Link];
2. public class Calculator{
3.
4. public int cube(int n){return n*n*n;}
5.
6. }
• [Link] file
1. <jsp:useBean id="obj" class="[Link]"/>
2.
3. <%
4. int m=[Link](5);
5. [Link]("cube of 5 is "+m);
6. %>

You might also like