Chapter 15 Java Beans Definitions n n A

Chapter 15 Java Beans

Definitions n n A reusable software component (程式設 計師於開發程式時可直接應用的小程式) that can be manipulated visually in a ‘builder tool’. The Java. Beans API provides a framework for defining reusable, embeddable, modular software components.

Intro to Java. Beans n What are Java. Beans? n n n Software components written in Java Connect and Configure Components Builder Tools allow connection and configuration of Beans Begins ‘Age of Component Developer’ Bringing Engineering methods to Software Engineering (e. g. electronics…)

Using Beans in JSP n You declare that your JSP page will use a Java. Beans component using either one of the following formats: <jsp: use. Bean id="bean. Name" class="fully_qualified_classname" scope="scope"/> or <jsp: use. Bean id="bean. Name" class="fully_qualified_classname" scope="scope"> <jsp: set. Property. . . /> </jsp: use. Bean>

Using Beans in JSP n 動作指令載入Beans <jsp: use. Bean id=“名稱” scope=“有效範圍” class=“Bean 類別” /> 類別名稱與儲存路徑 Scope: 含 page, request, session, application 等四種不同生命週期

Values of the scope Attribute n page (<jsp: use. Bean … scope="page"/> or <jsp: use. Bean…>) n Default value. Bean object should be placed in the Page. Context object for the duration of the current request

Values of the scope Attribute n application n Bean will be stored in Servlet. Context (available through the application variable or by call to get. Servlet. Context()). Servlet. Context is shared by all servlets in the same Web application.

Values of the scope Attribute n session n Bean will be stored in the Http. Session object associated with the current request, where it can be accessed from regular servlet code with get. Attribute and set. Attribute, as with normal session objects.

Values of the scope Attribute n request n Bean object should be placed in the Servlet. Request object for the duration of the current request, where it is available by means of get. Attribute

Using Beans in JSP <jsp: use. Bean id="book 1" class="coreservlets. Book" /> can be thought of as equivalent to the scriptlet <% coreservlets. Book book 1 = new coreservlets. Book(); %>




Using Beans in JSP <jsp: get. Property name="book 1" property="title" /> is equivalent to the following JSP expression <%= book 1. get. Title() %>

Bean 存放目錄 n 以Tomcat 為例 n 預設目錄 n WEB-INFclasses 子目錄 Example <jsp: use. Bean id=“A” scope=“session” class=“com. db. example”/> class 名稱: example. class 存放路徑: WEB-INFclassescomdb








Example package com. db; public class Login. Data { private String Name=“”; Private String Pwd=“”; public Login. Data() { } public void set. Login. Name(String name) { this. Name = name; } public void set. Password(String pwd) { this. Pwd = pwd; } public String get. Login. Name() { return this. Name; } public String get. Password() { return this. Pwd; } }
- Slides: 23