Using Java Beans Technology for Java Web Applications 2 – Develop Java Web Applications to Access Databases

There is no difference between creating a help class and creating a Java bean class. In fact, the help class FacultyBean.java we created in the last section is a Java bean class.
The JSP provide three basic tags for working with beans.

<jsp:useBean id=”bean name” class=”bean class” scope = “page | request | ses-sion |application”/>
The definitions for these three tags are:

1) The bean name is just a given name to refer to the Java bean. You can use any valid name for this tag.

2) The bean class is the full name of the Java bean class you created. The so-called full name means that you need to use both the bean class name and the package name in which the bean is located. For example, this bean class should be mypackage.mybeanclass if the bean class named mybeanclass is located in the package mypackage.

3) The scope indicates the range or the lifetime the bean can be used. Refer to the four scopes we discussed previously to get more detailed information about them.

A very useful JSP directive used for Java bean classes is <jsp:setProperty />. The protocol of this directive is:

<jsp:setProperty name = “id” property = “someProperty” value = “someValue” />

The three arguments for this directive are:

1) The id is the bean name as we discussed in step 1 previously.
2) someProperty is the property name defined inside the Java bean class, such as faculty Id and facultyName we defined in our FacultyBean.java class in the last section.

3) someValue is the initialized value assigned to a property in the bean class.

A variant for this tag is that the property attribute can be replaced by an “*”. What this does is accept all the form parameters and thus reduce the need for writing multiple setProperty tags. The only point to be remembered when you use this variant is that the form parameter names must be the same as the bean property names.

An example of using the setProperty tag is:

<jsp:setProperty name=“dbFaculty”property=“*” />

FIGURE 8.19   The modified help class—now it is a Java bean class.

In the setProperty tag, the id of the Java bean class is dbFaculty. The * in the property value means that all parameters transferred from another page can be assigned to the associated properties in the Java bean class.

Now let’s modify FacultyBean.java to make it a Java bean class to replace the help class file FacultyBean.java we built in the last section.

Leave a Comment



                    Blog, Contribute ,Privacy,Terms of Use,Trademarks,© estherknow 2024