One of the important ways that developers can take advantage of JavaBeanTM technology is to create and use servlets that are themselves beans. Servlets that are beans offer two distinct advantages:
Another important way that developers can take advantage of JavaBean technology is to use JavaBeans in servlets and JHTML files. For more information on this topic, see Using JavaBeans in Servlets and JHTML Files.
This document contains these sections which describe how to install, administer, and invoke servlet beans.
A servlet bean is a servlet that adheres to the JavaBeans design pattern for getting and setting properties. This pattern defines a standard method/class combination for getting and setting properties, and uses mandatory type signatures and naming conventions. The design pattern for read/write properties is:
void setFoo(Xyz x); Xyz getFoo();
The JavaServer assumes that all servlets are beans. The server sets all of the servlet bean properties (passed to the servlet as initial arguments) by using JavaBean introspection.
Servlet beans can be distributed as
.ser
"
files)
Servlet beans can be serialized into .ser
files.
A serialized file
can be thought of as an instance of the class.
The serialized servlet bean files and the class file they reference
can be packaged together in a JAR file.
JAR files are also refered to as installed files.
Serialized servlet bean files and the class file they reference can also reside outside of a JAR file. These files are refered to as not installed or dropped-in files.
The distinction between installed files and dropped-in files is that you cannot use the JavaServer Adminstration Tool to change the initial arguments specified in the dropped-in serialized server bean files (dropped-in serialized files are read-only). Nor can you use the Tool to create serialized files for dropped-in class files. Instead, you use the Tool to specify properties and initial values for the dropped-in files.
NOTE: All references to servlet bean files in this
document refer to installed files. This document does not describe
dropped-in files unless they are mentioned specifically.
Servlet Bean Instantiaton
The server provides support for instantiating
servlet beans by providing a classloader that deserializes the
server beans files from .ser
files. These .ser
files can be dropped-in files or reside in a JAR file.
When a JAR or dropped-in file changes on disk, the classloader
automatically reloads it.
Servlet beans files can reside in any of these directories:
servlet/
directory
servletbeans/
directory
To take advantage of the automatic reloading feature of the
JavaServer, we suggest that you store your servlet beans files
(class, .ser
, and .jar
files) in the
servletbeans/
directory and not include it in the CLASSPATH.
(Automatic reloading is not performed on files residing in
directories in the CLASSPATH.)
Servlet Bean Properties
Properties are the initial arguments which are
passed to a servlet bean. For example, for a servlet bean named "Counter",
there could be an initial argument initial=0
. Similarly,
for a servlet bean named "CGI" there could be an initial argument
bindir=cgi-bin
.
Passing Properties to Servlet Beans
Properties are passed to the servlet bean by:
ServletConfig
interface. The server always
passes properties with this interface.
Properties are passed to the servlet bean by the
ServerConfig
interface when the init
method
of the Servlet
interface is called. The
init
method is called by network service when it loads the
servlet.
Properties can also be passed to the servlet bean by calling set methods
for
the corresponding property. For example, the following code snippet
sets the value for the argument PropertyName
:
void setPropertyName (String);
This method will be called for every property which you set by using
the Administration Tool. After the servlet is loaded, if you
use the Tool to change properties, this method
will be called with a new value.
Servlet Bean Installation
Once the server knows about the servlet bean's existence, it is considered
to be installed. How a servlet bean is installed depends on whether
the servlet bean has been configured. A servlet bean is considered to be
configured if there is a .ser
file that contains the
appropriate initial
arguments for all of the servlet bean's properties.
There are two ways in which you can install servlet beans:
If your servlet bean is configured (that is, if it has a
.ser
file that contains initial arguments),
move the servlet bean file into the appropriate directory. Usually,
this will be the servletbeans/
directory. If the directory
is not on the CLASSPATH, the server
will automatically reload the servlet.
NOTE: If your servlet is a class file
or a .ser
file, you will not be able to
use the
Administration Tool to change any of the servlet bean's properties. You
can use the Tool to change the properties of only those
servlets that are contained in .jar
files.
Installing Unconfigured Servlet Beans
If your servlet bean is not configured, use the Administration Tool to specify initial arguments for it.
.class
extension. For example,
sun.server.http.FileServlet
is a valid class name.
.jar
file that will contain the servlet bean.
After the servlet bean is loaded, you can invoke it in the same way as you would invoke any other servlet:
Servlet bean files can be invoked by calling them as a URL address.
Note that even though you installed the servlet bean in the
servletbeans/
directory, you call it with a
servlet/
URL. The server will search the
servletbeans/
directory for the appropriate file.
http://<
host_name>:
< port
>/servlet/<
ServletName>
ServletName can represent either the name of a servlet bean
class file or a .ser
file. You invoke a class file or a
serialized file
with the name of the file without the .class
or
.ser
extension.
You can do this regardless of whether the
file resides in a directory "as is" (that is, it is a dropped-in file),
or it resides in a JAR file.
For example, a
servletbeans/
directory contains the files Counter.class
,
counter1.ser
, and a JAR file
Hello.jar
. The JAR file contains the files
Hello.class
, hello1.ser
, and
hello2.ser
.
The following table describes how to invoke various servlet beans files.
To Invoke this Servlet Bean File | Use this URL Address |
---|---|
serialized file in the servletbeans/
directory | Use the file name without the .ser extension.
For example: http://host:port/servlet/counter1 |
serialized file in a .jar file in the
servletbeans/ directory | Use
the file name without the .ser extension. For example:
http://host:port/servlet/hello1 |
class file in the servletbeans/ directory |
Use the file name without the .class extension. For example:
http://host:port/servlet/Counter |
class file in a .jar file in the
servletbeans/ directory | Use
the file name without the .class extension. For example:
http://host:port/servlet/Hello |
The servlet Hello.class
creates a web page with a greeting.
A code snippet
from Hello.class
appears below.
String greeting = "Hello World" //code to get, set, and display greetings Public String getString { return greeting; } Public void setSrting(String g){ greeting = g; } public void doGet (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { PrintWriter out; res.setContentType("text/html"); out = res.getWriter(); out.println("< html >"); out.println("< head >< title >Greeting < /title >< /head >"); out.println("< body >"); out.println("< h1 >" + getGreeting() + "< /h1 >"); out.println("< /body > < /html >"); }
There are also two .ser
files, hello1.ser
and hello2.ser
, which contain alternate greetings for the
servlet. These three files, Hello.class
,
hello1.ser
, and hello2.ser
are archived
as a .jar
file and stored in the servletbeans/
directory.
The
hello1.ser
file contains the initial argument
Greeting="Good Morning"
and hello2.ser
contains the initial argument Greeting="Good Evening"
.
The invocation http://
server_root
/servlet/hello1
returns "Good Morning
"
The invocation http://
server_root
/servlet/hello2
returns "Good Evening
"
The invocation http://
server_root
/servlet/Hello
returns "Hello World
"
Embedding Servlet Beans in a Server-side Include Statement
You can invoke a servlet bean by embedding it in a server-side include
statement inside an HTML document. For example, in an
.shtml
file, you can add a server
side include with this statement:
< servlet name="servletAliasName" code=ServletBeanExample > <
/servlet >
In this example, name="servletAliasName"
represents an
alias for the servlet bean and code=ServletBeanExample
represents the name of the servlet class. Note that the code
value can be omitted if an alias has been created for the servlet bean
with the Administration Tool.
Invoking an Alias for the Servlet Bean
You can use the Servlet Aliases page of the Administration Tool to create an alias for the servlet bean. The Servlet Aliases page lets you specify the pathname mapping rules that the JavaServer uses to invoke servlets. These rules allow you to use a shortcut URL to call a servlet from your browser, or to embed the shortcut within HTML documents (using server-side includes), or within other Java programs.
For more information, see Servlet Aliases
Top java-server-feedback@java.sun.com |
Copyright © 1997
Sun Microsystems, Inc. All Rights Reserved. |