300 likes | 537 Views
JavaBeans. JavaServer Pages By Xue Bai. 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
E N D
JavaBeans JavaServer Pages By Xue Bai Chapter 8
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 Chapter 8
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 Chapter 8
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 Chapter 8
Class Example public void setRadius(float radius){ this.radius = radius; } public void calculateArea(){ area = radius*Math.PI; } public float getArea(){ return area;} } 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; } Chapter 8
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); Chapter 8
Method Usage 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 Chapter 8
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; Chapter 8
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”); Chapter 8
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;} } Chapter 8
Installing Bean Classes • A directory the JSP engine looks for in the classpath • One choice in Tomcat is: • TomatRoot\WEB-INF\classes Chapter 8
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. Chapter 8
Instantiate a Bean Object <jsp:useBean id="bean_name" class="class_name" /> • 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 Chapter 8
Accessing Bean Properties <jsp:getProperty name=”bean_name” property=”property_name” /> • 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 Chapter 8
Set Bean Properties <jsp:setProeprty name=”bean_name” property=”property_name” value=”a new property value” /> • 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 Chapter 8
Setting a Bean Property Form Chapter 8
Setting a Bean Property Chapter 8
Beans and Forms • Beans interact with forms to generate dynamic Web pages • Use form control elements to interact with bean properties Chapter 8
Setting Properties with form Input Fields <jsp:setParamter name=”bean_name” property=”foo”/> • 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 Chapter 8
Setting Properties with form Input Fields <jsp:setProperty name=”bean_name” property=”propertyName” param=”inputFieldName”/> • 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” Chapter 8
Setting Properties with Form Input Fields <jsp:setProperty name=”bean_name” property=”*”/> • Looks through all the input fields provided by the form and all the methods provided by the bean, and links them together automatically Chapter 8
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 Chapter 8
A SETTER Method Taking Array Argument public void setValues(String[] values){ this.values = values; } Chapter 8
Beans and Scriptlet • <jsp:useBean id=”bean_name” class=”class_Path”/> 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 Chapter 8
Beans and Scriptlets Example <jsp:useBean id="calcBean" class="com.jspbook.chapter08.CalcBean1"/> <% calcBean.setValue1(request.getParameter("value1")); calcBean.setValue2(request.getParameter("value2")); %> <%= calcBean.getSum() %> Chapter 8
A Bean performs I/O • Write JavaBean class to perform I/O • Use Bean in JSP Chapter 8
Bulletin Board Chapter 8
Posting a Bulletin Chapter 8
Saving the Bulletin Chapter 8
Viewing the Bulletin Chapter 8