1 / 7

Java Beans

Java Beans. Why use Beans? Java Beans are Java classes that are written in standard format. Uses: No Java Syntax Simpler object sharing Convenient between request & object porp. What are Beans. Beans are simply classes, having three simple points outlined here:

Télécharger la présentation

Java Beans

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java Beans Why use Beans? Java Beans are Java classes that are written in standard format. Uses: No Java Syntax Simpler object sharing Convenient between request & object porp.

  2. What are Beans Beans are simply classes, having three simple points outlined here: Bean class must have a zero argument constructor A bean class must have no public instance variable Persistent values should be accessed through methods called getxxx and setxxx

  3. Using Beans Basic Tasks You can use three main constructs to build and manipulate JavaBeans components in JSP 1) jsp:useBean <jsp:useBean id=“beanname” class=“package.class” /> 2) jsp:getProperty <jsp:getProperty id=“beanname” property=“propertyname” /> 3) jsp:setProperty <jsp:setProperty id=“beanname” property=“propertyValue” />

  4. Building Beans: jsp:useBean The jsp:Bean is used to load a bean to be used in JSP page. Using syntax <jsp:useBean id=“name” class=“package.class” /> This means “instantiate an object of the class specified by class, and bind it to a variable in _jspService with the name specified by id. Ex: <jsp:useBean id=“book1” class=“pack1.Book” /> It is equivalent to scriptlet: <% pack1.Book ob = new pack1.Book(); %>

  5. 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

  6. Using jsp:useBean options: scope, beanName, 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:useBean id=“Thread1” class=“pack1.myclass” type=“java.lang.Runnable” />

  7. Accessing getProperty and setProperty Once you have a bean, you can access its properties with jsp:getProperty which takes a name attribute that should match the id given in jsp:useBean. <jsp:getProperty name=“Book1” property=“title” /> or <% Book1.getTitle() %> To modify bean properties, you normally use jsp:setProperty. In this you have to supply three attributes: name, property and value. <jsp:setProperty name=“Book1” property=“title” value=“JSP”/> or <% Book1.setTitle(“JSP”)

More Related