Java Web Server 1.1 - Using Beans in Servlets and JHTML

JavaServer

Using JavaBeans in Servlets and in JHTML Files


Contents / New Features / Index / Servlet Beans

The JavaServerTM platform lets you install JavaBeansTM on the server, to use in servlets and JHTML files. These JavaBeans are invisible beans: they have no user interface. Any user interface (presentation layer) is separate, and might not even involve Java. For example, a JavaBean might be accessed through forms in a web browser. A JavaBean might also be used with a network protocol driven by a Java client or from an applet within a web browser.

This document presents some server-side uses of JavaBeans in servlets and JHTML pages, and describes how to install JavaBeans into a given JavaServer.

Another important way that servlets can use JavaBeans is when the servlet itself is a bean. In this case, the servlet bean adheres to the JavaBeans design patterns for getting and setting properties and saves state (such as configuration) in serialized (*.ser) files. See the document Using Servlet Beans for more information about how to use servlets as beans.

Installing JavaBeans into a JavaServer

JavaBeans are distributed in JAR files. A JavaBean is defined as the JAR file and, optionally, any associated native code. A JAR file contains the bean class code and any resources. The way you install a JavaBean depends on whether it is 100% Pure JavaTM. After installing the JavaBean, you must restart the server.

Installing 100% Pure Java Beans

To install a 100% Pure Java bean, move the JAR file containing the JavaBean and any code modules it might require, into the lib/ directory. This is the only installation step required.

Installing JavaBeans That Use JNI

Some JavaBeans might not be 100% Pure Java. For example, they might encapsulate legacy code for some particular business function and use the Java Native Interface (JNI) to provide access to that logic from Java. For each operating system, follow these installation instructions:

Operating SystemInstallation Instructions
Win32Copy the *.dll file holding the JNI module into the same directory as the JAR file.
This will be the lib/ directory.
Solaris/SPARCCopy the *.so file holding the JNI module into the lib/solaris/sparc directory.
Solaris/x86Copy the *.so file holding the JNI module into the lib/solaris/i386 directory.

Restarting the Server

You must restart the server after installing a beans JAR file (and any optional native code support). After the server restarts, all of the JavaBeans will be accessible through the default class loader of the server. This means that the JavaBeans will not be running inside the server sandbox.

Removing JavaBeans

If you need to remove JavaBeans after you have installed them, stop the server, remove the JAR file and any associated native code, and then restart the server. To remove the JAR file, either delete the file or move it to a location not on the CLASSPATH.

An Example

JavaBeans can be used in servlets or in JHTML pages. The following example describes using JavaBeans in JHTML files.

Using JavaBeans from JHTML Web Pages

Embedding Java code in HTML web pages by using *.jhtml files, gives you a powerful way to develop applications. With this technique, you can use Java code in your JHTML files to glue HTML presentation to data management logic encapsulated in JavaBeans. The JavaBean code can be either written explicitly in the JHTML file or it can be included by using an import statement.

For example, the UI could be an HTML page on a thin client which accesses some data management logic contained in a JavaBean. JHTML on the server generates the HTML page presentation data dynamically and provides the glue which attaches the HTML to the JavaBean.

In JHTML files, the Java code is surrounded by < java> ... </java> tags. For example, the following code snippit which instantiates a JavaBean can be included in a JHTML file.

< java>

    MyBeanClass	bean;

    try {
	Object	obj;

	//
	// this instantiates a bean by class name; you can also
	// pass the name of a saved bean
	//
	obj = Beans.instantiate (null, "com.mycorp.beans.MyBeanClass");
	if (obj instanceof MyBeanClass)
	    bean = (MyBean) obj:
	else {
	    //
	    //add code here to handle the error
	    //
	    bean = null;
	}
    } catch (ClassNotFoundException e) {
	//
        //add code here to handle the error
        //
    }
        
< /java>


Top
java-server-feedback@java.sun.com
Copyright © 1997 Sun Microsystems, Inc.
All Rights Reserved.