Java Beans Why use Beans Java Beans are
Java Beans Why use Beans? Java Beans are Java classes that are written in standard format. Uses: 1) No Java Syntax 2) Simpler object sharing 3) Convenient between request & object porp.
What are Beans are simply classes, having three simple points outlined here: 1) Bean class must have a zero argument constructor 2) A bean class must have no public instance variable 3) Persistent values should be accessed through methods called getxxx and setxxx
Using Beans Basic Tasks You can use three main constructs to build and manipulate Java. Beans components in JSP 1) jsp: use. Bean <jsp: use. Bean id=“beanname” class=“package. class” /> 2) jsp: get. Property <jsp: get. Property id=“beanname” property=“propertyname” /> 3) jsp: set. Property <jsp: set. Property id=“beanname” property=“property. Value” />
Building Beans: jsp: use. Bean The jsp: Bean is used to load a bean to be used in JSP page. Using syntax <jsp: use. Bean id=“name” class=“package. class” /> This means “instantiate an object of the class specified by class, and bind it to a variable in _jsp. Service with the name specified by id. Ex: <jsp: use. Bean id=“book 1” class=“pack 1. Book” /> It is equivalent to scriptlet: <% pack 1. Book ob = new pack 1. Book(); %>
Installing Bean classes The Bean class definition should be placed in the same directory where servlets can be installed, not in the directory where the JSP file. WEB-INF/classes/Packages/classfiles And Jar files containing bean classes should be placed in the following directory WEB-INF/lib
Using jsp: use. Bean options: scope, bean. Name, and type You can specify a scope attribute that associates the bean with more than just the current page. One can use local variable to have the same type as the object being created. Use the type attribute to control the declaration, as in the following example: <jsp: use. Bean id=“Thread 1” class=“pack 1. myclass” type=“java. lang. Runnable” />
Accessing get. Property and set. Property Once you have a bean, you can access its properties with jsp: get. Property which takes a name attribute that should match the id given in jsp: use. Bean. <jsp: get. Property name=“Book 1” property=“title” /> or <% Book 1. get. Title() %> To modify bean properties, you normally use jsp: set. Property. In this you have to supply three attributes: name, property and value. <jsp: set. Property name=“Book 1” property=“title” value=“JSP”/> or <% Book 1. set. Title(“JSP”)
- Slides: 7