Tài liệu Using JavaBeans in JavaServer Pages - Chương 3 pptx

31 702 0
Tài liệu Using JavaBeans in JavaServer Pages - Chương 3 pptx

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Using JavaBeans in JavaServer Pages Chương 3 / 2 of 36 Session Objectives  Describe the various features of a JavaBean  Differentiate between a JavaBean and a Java class  Describe the basic structure of a JavaBean  Describe the various ways of creating beans  Describe the advantages of using beans  Explain how to access beans through JSP scriptlets  Describe the use of various JSP bean tags  Define the scope of beans / 3 of 36 JavaBean or a Bean, is a simple Java class that follows a set of naming and design conventions, outlined by the JavaBeans specifications These components can be combined into applets, applications, or composite components With the help of the JavaBeans API, you can create reusable and platform-independent components What is a JavaBean? JavaBeans brings component technology to the Java platform / 4 of 36 Difference between a JavaBean and a Java class  The class must be instantiable  It must have a default constructor  It must be serializable  It must follow the JavaBeans design patterns A bean is a Java class with the following additional characteristics: / 5 of 36 JavaBeans Design Patterns  Naming conventions for bean methods and data members  Design patterns are important for introspection  Introspection: mechanism using components makes its internal structure known to the outside world?  Methods are exposed through reflection / 6 of 36 An Example of a JavaBean public class Tower { private float height; public float getHeight() { return height; } public void setHeight(float h) { height = h; } public boolean isGreaterHeight(int initialHeight, int finalHeight) { if((finalHeight - initialHeight) > 0) { return true; } else { return false;} } public Tower() { height = (float)10.5; } } Property get PropertyName() set PropertyName() / 7 of 36  JSP provides three basic bean tags:  To find and use the bean – jsp:useBean  To set one or more properties – jsp:setProperty  To get a property – jsp:getProperty  These are also known as actions and are specific tags that affect the following:  the runtime behavior of the JSP  the responses that are sent back to the client Using JSP Bean Tags / 8 of 36 Using JSP Bean Tags – (1) The Black box approach getProperty_Name() setProperty_Name( value ) Bean The JSP need not know the inner structure of the bean / 9 of 36 jsp:useBean  This is used to associate a JavaBean in JSP.  It ensures that the object can be referenced from JSP, using an ID and scope  The syntax is as follows: <jsp:useBean id = "bean_name" class = "bean_class“ /> The bean class must be available to the JSP engine / 10 of 36 jsp:useBean – (1) <jsp:useBean id = "id_name" class = "bean_class" scope = "page|request|session|application" beanDetails / >  class = "className"  class = "className" type = "typeName"  beanName = "beanName" type = "typeName"  type = "typeName" beanDetails is one of: [...]... Application Scope – (3)    Both these pages share the same instance of the Counter bean Each page increments the other page’s instance of the bean These pages will share this same instance, until the JSP engine is shut down / 21 of 36 jsp:setProperty   This is used along with the useBean action It sets the values of bean properties in various ways: At request time, using the parameters in the request... instantiate the bean Hello World! / 13 of 36 Session Scope     Beans with session scope are accessible only within pages processing requests that are in the same session as the one in which the bean was created Beans cannot be defined in a page whose page directive has an attribute session=false References to the session scope are stored in. .. same instance of the browser, the counter increases from the last value shown in the first JSP If a new instance of the browser is opened, the current count attribute will be reset Each instance of a client creates its own instance of the HttpSession, which is where the Counter bean is stored / 17 of 36 Application Scope     Beans with the application scope are accessible within pages that are in. .. property = "propertyName" param = "paramName" /> / 28 of 36 An Example The Bean – CalcBean.java public class CalcBean implements java.io.Serializable { private int value1; private int value2; public void setValue1(int num1) { value1 = num1; } public void setValue2(int num2) { value2 = num2; } public int getSum() { return value1 + value2; } } / 29 of 36 An Example – (1) The Form – Calculator.html ... various ways: At request time, using the parameters in the request object  At request time, using the result of an evaluated expression  From a specified string  / 22 of 36 jsp:setProperty – (1)   The bean introspection method is used to discover what properties are present, their names, whether these are simple or indexed, their types and the accessor methods The syntax is as follows: / 23 of 36 jsp:getProperty     This action is complementary to the jsp:setProperty action It is used to access the properties of a bean It converts the retrieved value to a String The syntax is as follows: / 24 of 36 An Example The Bean – Counter.java public class Counter { Property int count; public Counter() {... object Beans with the application scope are best used when there is a need for sharing the information among JSPs and servlets for the life of an application All clients access the same object / 18 of 36 Application Scope – (1) An Example - The first JSP Instantiate the Counter bean . Using JavaBeans in JavaServer Pages Chương 3 / 2 of 36 Session Objectives  Describe the various features. height = h; } public boolean isGreaterHeight(int initialHeight, int finalHeight) { if((finalHeight - initialHeight) > 0) { return true; } else

Ngày đăng: 25/01/2014, 11:20

Từ khóa liên quan

Mục lục

  • Using JavaBeans in JavaServer Pages

  • Session Objectives

  • What is a JavaBean?

  • Difference between a JavaBean and a Java class

  • JavaBeans Design Patterns

  • An Example of a JavaBean

  • Using JSP Bean Tags

  • Using JSP Bean Tags – (1)

  • jsp:useBean

  • jsp:useBean – (1)

  • Scope of Beans

  • Scope of Beans – (1)

  • An Example

  • Session Scope

  • Session Scope – (1)

  • Session Scope – (2)

  • Session Scope – (3)

  • Application Scope

  • Application Scope – (1)

  • Application Scope – (2)

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan