6. JavaServer Pages and JavaBeans

Objective

JavaServer Pages and JavaBeans is a big topic and this tutorial doesn't really do it justice. The objective therefore is to provide an introduction to JSP JavaBean architecture and discuss the advantages of using JavaBeans.

What is a JavaBean

A JavaBean can be defined as a reusable software component. What this means is that we can write a JavaBean that can then be used in a variety of other Java based softwares such as applications, Servlets or JSP pages. In this way we can define our business logic within a JavaBean and then consistently use that logic in seperate applications. In many ways they are analogous to Microsoft's COM and ActiveX technologies.

JavaBeans can also have a visual component in a manner similar to the ActiveX controls that can be dragged-and-dropped into a Visual Basic application. This tutorial does not discuss this as we are concentrating on how JavaBeans can be of benefit to our JavaServer Page technology.

Benefits of JavaBeans

We now have three ways of writing code to be used by a JSP. These are,

  1. Place the code at the start of a JSP in a declaration,
  2. Use an include statement to reference another file which contains the code and now
  3. Package the code in a JavaBean

So which method do we use and when ?

As always, it depends on your circumstances. Writing the code within a JSP page is certainly the most straightforward but it does limit the amount of code reuse. Using include statements does lend itself to a certain amount of code reuse and certainly it is the only way to allow reuse of chunks of HTML.

By using JavaBeans you can fully separate the business logic from the generation of the display. This is an important philosophy that leads to better structured and more maintainable systems. In our case you would use the JavaServer Page to dynamically generate display and also to handle the user interaction. The JavaBean would take over when you need to perform some complex data processing or when you need to access databases or the file system.

The other advantage of using JavaBeans is that the business logic can be used by more than one application. For example, both a client based Java application and a JSP page can access the same JavaBean thus guaranteeing the same functionality.

A final point to note is that by using JavaBeans you can split your development team into Java experts and HTML experts. The Java experts would write and develop the JavaBeans and the HTML experts would concentrate of the design of the web application.

Example JavaBean

More specifically a JavaBean is just a Java class that adheres to the following rules,

Properties are always accessed using a common naming convention. For each property two methods must exist: a getXxx() and a setXxx() method where xxx is the name of the property and of a private instance variable.

This is an important point as you can easily get tripped up with the syntax. For example, if we want a property called fileName then we would need to define an instance variable called fileName and two methods: getFileName() and setFileName(). Notice the change of case of the f in 'fileName'.

The following code is a simple JavaBean

package  com.bo.jspBean;

public class employeeBean
{
  private String firstName = "";
  private String lastName  = "";

//First Name property
  public void setFirstName(String name)
  {
    firstName = name;
  }

  public String getFirstName()
  {
    return firstName;
  }


//Last Name Property
  public void setLastName(String name)
  {
    lastName = name;
  }

  public String getLastName()
  {
    return lastName;
  }

//Full Name Property - Read Only
  public String getFullName()
  {
    return firstName + " " + lastName;
  }
}

This bean is a bit trivial. It has three properties firstName, lastName and fullName. The last property is read only as it doesn't have a set method.

Compiling a JavaBean

The best way to do this is to get hold of a Java Development tool such as JBuilder which makes the creation of a JavaBean relativley straightforward. It will do the debugging, compiling and packaging all at the click of a mouse!

Some Application Servers can only use beens once they have been packaged others can work with JavaBeans unpackaged but still compiled as Java class files.

This section gives an overview of the steps involved.

Step 1. Compile the class files

The installation of the JDK included several utilities that we will use for our JavaBeans. The first utility, javac - the java compiler, is used to compile class files from the java source code files using the following syntax,

javac -verbose *.java

This option will compile all the ".java" files in the current directory. The "verbose" option is used to generate compilation information. There are other options available and are detailed in the JDK documentation.

Step 2. Build the JAR Archive File

This step is not required by every JSP Engine however it is a good means by which multiple JavaBeans can be packaged in a single file, for example, all the financial JavaBeans are packaged in the finance.jar package.

This is where a Java Development Tool is really useful as it can be a bit complex and is very easy to make a mistake. The key is to have the right files in the right locations with the right case sensitive file names.

Under our working directory we need to create two other folders, one called "meta-inf" and the second must have the same name as the package name used in our class file definitions. Into this second folder we place all the class files that we want bundled into our JAR archive file. Into the first folder we create a manifest file.

The manifest file is necessary as JSP Containers use this to determine the structure of the JAR file. It is a plain text file typically named "manifest.mf" and is structured like,

Name: packageName/className.class
Java-Bean: True

where packageName is the name of the package (and, of course, is also the name of the folder containing the class files) and className is the name of the class we are including in the JAR file. Each class needs it's own entry like this and, note, the last entry must be followed by a carriage return.

Next we run the jar utility that will build the JAR file. It takes the following format,

jar cvmf meta-inf\manifest.mf jarName.jar packageName\*.class

This should be entered as a single line and jarName is the name of the JAR file we are building and packageName is the name of the folder in which our class files reside (again it is also the name of the package). The JAR file is usually given the same name as the package.

Note if the package name is structured like com.acme.mypackage then the "." need to be replaced with "\" when writing the jar command. For example,

jar cvmf meta-inf\manifest.mf jarName.jar
                  com\acme\mypackage\*.class

The cvmf clause are options available to the JAR utility. The c instructs the utility to create a JAR file, v forces verbose output of the compilation, m indicates that we are providing a manifest file and f indicates that we are providing the JAR file name. These and other options are discuses in more detail in the JDK documentation. So in our example we would enter (as a single line),

jar cvmf meta-inf\mainfest.mf jspBeans.jar jspBeans\*.class

For further details on JAR files see the JAR section of the Java Tutorial at Sun's web site.

Step 3. Deploying the JavaBean

Once we have created our JavaBean we then need to place the file in a suitable location on our hard drive so that it can be accessed by the Application Server.

If you are using the JavaBean unpackaged then place the file under the classes folder of the WEB-INF folder. If you are using a package name such as com.acme.mypackage then you need to create this folder structure under "WEB-INF/classes" such as "WEB-INF/classes/com/acme/mypackage/mybean.class".

WBE-INF sub folder structure

If you have packaged the JavaBean in a jar file then place the file in the "WEB-INF/lib" folder.

You may need to restart your Application Server before the JavaBean recongnised.

JavaBeans and JSP

The next section looks at how to use JavaBeans from a JavaServer Pages.

Back Next