Using JavaBeans Components in JSP Documents. Contents 1. Why using beans? 2. What are beans? 3....

Post on 28-Dec-2015

217 views 0 download

Transcript of Using JavaBeans Components in JSP Documents. Contents 1. Why using beans? 2. What are beans? 3....

Using JavaBeans Components in JSP Documents

Contents

1. Why using beans?2. What are beans?3. Using Beans: Basic Tasks4. Sharing Beans 5. Practice exercise

1. Why Use Beans?

- Separate class are easier to write, compile, test, debug and reuse.

- No Java syntax - Simpler object sharing - Convenient correspondence

between request parameters and object properties.

- (Part II, chapter 14)

2. What Are Beans?

A bean class must have a zero-argument (default) constructor

A bean class should have no public instance variables (fields)

Persistent values should be accessed through methods calledget Xxx and setXxx

3. Using Beans: Basic Tasks jsp:useBean <jsp:useBean id="beanName"

class="package.Class" /> jsp:getProperty <jsp:getProperty name="beanName"

property="propertyName" /> jsp:setProperty <jsp:setProperty name="beanName"

property="propertyName" value="propertyValue" />

<jsp:useBean id="book1" class="coreservlets.Book" />

<% coreservlets.Book book1 = new coreservlets.Book(); %>

Accessing Bean Properties: jsp:getProperty

<jsp:getProperty name="book1" property="title" /> <%= book1.getTitle() %>

<jsp:setProperty name="book1" property="title" value="Core Servlets and JavaServer Pages" /> <% book1.setTitle("Core Servlets and JavaServer Pages"); %>

4. Sharing Beans

<jsp:useBean ... scope="page" /> <jsp:useBean ...

scope="request" /> <jsp:useBean ...

scope="session" /> <jsp:useBean ...

scope="application" />

5. Practice Exercise

Develop a WebApp to manage product information. The WebApp allow use add and update a product. Each product has name, description, price, producer name.

Create JSP to add new Product using Product bean

<jsp:useBean id=“product" class=“mylib.Product" scope="page"/>

<jsp:setProperty name=“product" property="*" />

productType.insert(conn)