Web Development with JavaServer Pages

Скачать в pdf «Web Development with JavaServer Pages»


We ’ll cover actions in more depth in chapters 4 and 6. For now, let’s take a look at how these tags might help you manage your JSP applications.

3.4.1 Reusing logic with JavaBeans


One common use of the special XML-based tags we’ve mentioned is to communicate with JavaBeans. In fact, JSP provides several standard action tags to help you communicate with these beans. As we discussed in chapter 1, JavaBeans are reusable Java components: they are Java classes that follow conventions, defined in the Java-Beans standard, that promote modularity and reusability. The details of this standard, as it relates to JSP pages, will be covered in chapter 8. For now, let’s look at a simple JavaBean class so that we can present a JSP page that uses it:


package com.taglib.wdj sp.firststeps;


public class HelloBean implements java.io.Serializable {


String name = «world»; public String getName() { return name;


}


public void setName(String name) { this.name = name;


}


}


Indeed, this is a very simple Java class. It contains a single instance variable, name, which refers to a string. By default, this string has the value world, but it can be changed using the method setName(), which takes an instance of the Java String class as its parameter. Code outside the bean can retrieve the name by using getName(). These methods have names that the JavaBeans framework will look for, by default, when it needs to modify or retrieve the name variable, which in bean terms is called a property.

Скачать в pdf «Web Development with JavaServer Pages»