Using JavaBeans in Servlets and in JHTML FilesToolkit Documentation / Developer Docs / Administrator Docs / Index The JavaServer 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.
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 JavaBeans 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 Installing JavaBeans That Use JNISome 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:
Restarting the ServerYou 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 JavaBeansIf 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 ExampleJavaBeans 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, 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
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
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>
![]()
|