Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 81 JavaBeans JavaServer Pages By Xue Bai.

Similar presentations


Presentation on theme: "Chapter 81 JavaBeans JavaServer Pages By Xue Bai."— Presentation transcript:

1 Chapter 81 JavaBeans JavaServer Pages By Xue Bai

2 Chapter 82 Objectives In this chapter, you will: Survey the basics of the Java programming language Write JavaBeans Compile and install bean classes Use beans with action tags Get and set bean’s properties Connect beans with forms Use beans in scriptlets Write a file bean to read from and write to files

3 Chapter 83 Java Primer JavaBeans are written in Java JSP uses the same syntax as Java programming language JSP is built on top of servlet, which in turn is built on Java

4 Chapter 84 Objects and Classes A class is a template or blueprint for objects A class consists of three parts: –Data members –Methods –Constructors Objects are instances of classes

5 Chapter 85 Class Example public class Circle{ private float radius; private float area; public Circle(){ radius=0.0f; area = 0.0f; } public Circle(float radius){ this.radius = radius; area = 0.0f; } public void setRadius(float radius){ this.radius = radius; } public void calculateArea(){ area = radius*Math.PI; } public float getArea(){ return area;} }

6 Chapter 86 Vector One of the most frequently used classes in Java Create Vector object: –Vector v1 = new Vector(); –Vector v2 = new Vector(Collection c); –Vector v3 = new Vector(int initialCapacity); –Vector v4 = new Vector(int initialCapacity, int increments);

7 Chapter 87 MethodUsage addElement(Object o) or add(Object o) Appends the specified object in the argument to the end of the vector. elementAt(int index)Returns the component at the specified index. The index of the first element is zero, the second one is 1, and so on. Contains(Object o)Tests whether the specified object is a component in this vector. It returns a Boolean value indexOf(object o)Searches for the first occurrence of the given argument, testing for equality using the equals method. It returns the index of the element in the vector if found, -1 otherwise. InsertElementAt(Object o, int index) Inserts the specified object as a component in this vector at the specified index size()Returns the number of components in this vector as an int

8 Chapter 88 Using Vector Storing objects Vector v = new Vector(); v.addElement(“Lynne”); Retrieving object: Object obj = v.elementAt(0); Cast object to its original data type: String s = (String)obj;

9 Chapter 89 Writing JavaBean Reusable components in JSP Written in Java Regular Java classes with certain rules: –Rules for GET and SET methods: –If a data member is called foo, then its GET and SET methods as follow: getFoo(); setFoo(“arguments”);

10 Chapter 810 Class Example package com.jspbook.chapter08; public class SimpleBean{ private String message; public SimpleBean(){ message = "Hi, there."; } public String getMessage(){ return message;} public void setMessage(String aMessage){ message = aMessage;} }

11 Chapter 811 Installing Bean Classes A directory the JSP engine looks for in the classpath One choice in Tomcat is: –TomatRoot\WEB-INF\classes

12 Chapter 812 Using Beans With action tags, you can use these beans No Java programming language is required in order to use beans With simple action tags, you can utilize all Java powerful features in JSP: –Perform I/O –Use well-fined collection classes, etc.

13 Chapter 813 Instantiate a Bean Object Create and load a JavaBean “bean_name” is the name that refers to the bean It must be unique everywhere it is to be used No two beans can have the same name in the same page The bean name serves as a local variable reference to the bean object

14 Chapter 814 Accessing Bean Properties This tag retrieves the value of a bean property, converts it to a string, and inserts it into the output The two required attributes are name and property The “bean name” is the same name specified in the ID attribute when the bean is created, and the “property name” is the name of the property to get

15 Chapter 815 Set Bean Properties This tag assigns a new value to the specified property In this tag, the “value” attribute specifies the new value to be assigned to the bean property

16 Chapter 816 Setting a Bean Property Form

17 Chapter 817 Setting a Bean Property

18 Chapter 818 Beans and Forms Beans interact with forms to generate dynamic Web pages Use form control elements to interact with bean properties

19 Chapter 819 Setting Properties with form Input Fields The value assigned to the property is presumed to come from the form In order for this technique to work, the bean must have a public setter method called setFoo, and this method takes a String object as an argument This tag requires the match between the property name and the form’s input parameter name

20 Chapter 820 Setting Properties with form Input Fields Used when the name of the form parameter and the name of the property do not match This tag uses the form input field called “inputFieldName” to set the bean’s property called “propertyName”

21 Chapter 821 Setting Properties with Form Input Fields Looks through all the input fields provided by the form and all the methods provided by the bean, and links them together automatically

22 Chapter 822 Working with Arrays Multiple values are associated with one input field You can also use a bean to get all these values associated with the same input field on a form

23 Chapter 823 A SETTER Method Taking Array Argument public void setValues(String[] values){ this.values = values; }

24 Chapter 824 Beans and Scriptlet locates or instantiates a bean object from the class and binds the variable specified as “bean_name” to the bean object After the variable “bean_name” has been bound to the bean object, you can use this variable as a reference to the bean object in your JSP script Since the variable is a reference to the bean object, you can call all methods provided by the bean in your JSP scriptlets

25 Chapter 825 Beans and Scriptlets Example <% calcBean.setValue1(request.getParameter("value1")); calcBean.setValue2(request.getParameter("value2")); %>

26 Chapter 826 A Bean performs I/O Write JavaBean class to perform I/O Use Bean in JSP

27 Chapter 827 Bulletin Board

28 Chapter 828 Posting a Bulletin

29 Chapter 829 Saving the Bulletin

30 Chapter 830 Viewing the Bulletin


Download ppt "Chapter 81 JavaBeans JavaServer Pages By Xue Bai."

Similar presentations


Ads by Google