|
Last updated April 24, 2001
Java servlets VS CGI
Servlets provide a powerful alternative to traditional CGI.
Servlets
can be used wherever CGI might normally be used yet they allow for session
persistence and have faster startup times than traditional CGI. Servlets also
have all the advantages of the Java language such as ease of development, platform
independence, and a large set of available APIs.
Like CGI, servlets extend the functionality of a web server. For
example, a servlet could be written to do any of the following:
- Process and/or store data from an HTML form
- Provide dynamic content (such as returning the results from a database query)
- Manage state information such as for an online shopping cart system
But servlets have several advantages over CGI:
- They do not run as a separate process and so do not incur the overhead of
creating a new process for every request.
- Servlets stays in memory between requests and do not need to be loaded and
started as a CGI program would.
- Servlets can run as a single instance answering all requests concurrently.
With only one instance, memory is better utilized and servlets can easily
manage persistent data.
- A servlet can run in its own zone (restricted area) for increased security.
Technical overview
This Java servlets freeware consists of an add-on module to Apache,
called "mod_jserv", and a standalone Java application, referred to as the
"servlet engine".
Mod_jserv is implemented as a Dynamic Shared Object (DSO) and the
servlet engine runs on top of the MPE's Java Virtual Machine (JVM). Mod_jserv
module and the servlet engine are open source software, Apache JServ 1.1,
from the Java Apache Project.
This implementation of Java servlets is referred to as a three-tiered
model
because it consists of three components. Some implementations of Java servlets
are two-tiered with the servlet engine being part of the web server. The
three tiers are:
- The web browser
- The Apache web server with add-on module, mod_jserv.so
- The servlet engine, JServ
Java Servlet Model on HPe3000
Apache's mod_jserv and the servlet engine communicate through a special
protocol,
Apache JServ Protocol (AJP), which runs on top of TCP. The TCP connection
is usually through the localhost, 127.0.0.1, but can also be via a remote host.
For a remote host, the Apache Web Server and the servlet engine are on
separate machines.
When making a request, the web browser acts as a client and sends its request
to Apache using the HTTP protocol. Apache's mod_jserv translates this HTTP
request
into AJP and acts as the AJP client by sending the request over the network
(local or remote) to the servlet engine. The servlet engine acts as the AJP
server
and listens on a specific port (8007 by default) for incoming requests. The
servlet engine will process a request by executing the requested servlet.
When
the servlet has completed processing its request, the servlet engine responds
back to Apache using AJP. Apache converts the AJP response to HTTP and sends it
back to the client browser.
Major components
The Java servlets software installs and runs under directory /APACHE/PUB
for Apache or directory /APACHE/SECURE for WebWise Secure Web Server.
Under one of these directories will be the following files and subdirectories:
JSRV - The Apache job stream file for starting the JServ engine. It contains
the necessary variables for running the engine as a standalone process. JSRVS
is the job file for WebWise.
JSRVOFF - The Apache job stream file for stopping the JServ engine. JSRVSOFF
is the job file for WebWise.
libexec - The subdirectory containing mod_jserv.so, the JServ class
files ( ApacheJServ.jar), and an example file and documentation for creating
your own DSOs.
servlets - This subdirectory is the pre-configured repository for servlet
files.
It contains two simple servlet source files and their corresponding class files.
These files are used for testing the installation.
conf/jserv - A subdirectory containing three new configuration files:
jserv.conf, jserv.properties, and root.properties.
htdocs/jservdocs - This subdirectory is the root for the Apache JServ
documentation
including the API specification in javadoc format, a feature list, FAQs,
and several in-depth articles on functionality.
htdocs/JSDK2.0/doc/apidoc/ - This subdirectory contains the JSDK2.0 API
specification in javadoc format.
lib/jsdk.jar - Class files in jar format of the Java Servlet Development Kit
2.0 (JSDK2.0).
logs/mod_jserv.log - The log file created and used by the Apache module,
mod_jserv.so.
logs/jserv.log - The log file created and used by the servlet engine, JServ.
Mod_jserv.so
Mod_jserv.so is the add-on module to Apache that communicates with the
servlet engine, JServ.
It converts browser HTTP requests to servlet AJP requests then converts
servlet
AJP responses to HTTP responses and returns them to the client browser.
Mod_jserv.so
has a ".so" suffix because it is a Dynamic Shared Object (DSO), an NMXL on
MPE. Mod_jserv.so is written in C and has the same structure as other Apache compiled-in modules.
Because it is a DSO, it is loaded and run at Apache startup or
restart.
ApacheJServ.jar
This is the "servlet engine", a 100% pure java application that executes
servlets.
This servlet engine executes like a regular Java application with a static
main method in class org.apache.jserv.JServ. This version of the servlet
engine,
Apache JServ 1.1, executes servlets that are compliant with JavaSoft's Java
Servlet APIs, version 2.0. The servlet engine forwards AJP client request data
to the requested servlet and then forwards the servlet response back to the AJP
client.
Java servlet development kit 2.0
Apache JServ 1.1 uses the class definitions from Sun's Java Servlet Development
Kit 2.0 (JSDK2.0). JSDK2.0 is a Sun product that can be downloaded for free
from Sun's
web site. For your convenience, the JSDK2.0 class definitions are installed as
part of the Java servlets HP freeware in their complete and unmodified form.
These servlet classes come packaged by Sun in a Java archive file, jsdk.jar.
Any version of the servlet classes other than JSDK 2.0 (past, present, or
future version) may not work with the Apache JServ 1.1 and should not be used.
The jsdk.jar class library file is installed in /APACHE/PUB/lib for standard
Apache and in /APACHE/SECURE/lib for WebWise.
All servlets use the class definitions in jsdk.jar. Because the servlet
engine
is itself a servlet, the jsdk.jar file is also required for the servlet engine
to run.
The JSDK2.0 API documentation is also distributed in its complete and
unmodified
form with your installation at http://yourhost.com/JSDK2.0/doc/apidoc.
This includes:
- a servlet tutorial
- example servlets
- class source code
- servlet API documentation, Version 2.0
Additional components of the JSDK2.0 are available by downloading the entire
package from Sun's site at
http://java.sun.com/products/servlet.
Jserv.conf
This is the first of three new configuration files. This configuration
file contains directive for Apache only and is not read by the servlet engine.
It
contains directives which, for example, indicate whether mod_jserv.so should be
loaded and whether the servlet engine will be run automatically or manually
started
Automatic launching of the serlvet engine does not work correctly on MPE
/iX. The ApJServManual directive should always be set to true:
ApJServManual on
The grammar for this file follows the grammar used by other Apache
configuration files:
DirectiveName value
This file is read once at Apache startup or restart.
Jserv.properties
This is the second of three new configuration files. This file contains
all the properties required by the servlet engine. Some properties are passed
to
the Java Virtual Machine (JVM) and others include various security preferences,
settings for internal communication between the servlet engine and the Apache
web server, properties for enabling logging, and a list of all servlet zones and
their respective property files.
The grammar for this file follows a grammar similar to Java property files:
property_name=value, value, ...
This file is read by both mod_jserv and by the servlet engine. It is loaded
once at startup, so changes are available only after restart of the web server
and of the JVM.
Root.properties
This is the third of three new configuration files. Each servlet zone
requires
its own properties file. The installation and configuration processes set
up one servlet zone called "root" and the its corresponding properties file
called "root.properties". A properties file contains a list of servlet
repositories
for a specific zone. This is the location of the servlet class files for that
zone. Any other settings that are specific to a zone must also be in the
properties
file. It also contains aliases and initialization arguments for the servlets
in that zone.
Properties files use the same grammar as the jserv.properties file and
are usually named using the zonename.properties syntax.
This file is read once by the servlet engine at startup.
Definitions
Servlet Repository - A servlet repository is a collection of servlets
and may be either a directory or archive, such as a zip or jar file.
Servlet Zone - The servlet engine partitions its execution space into
secured, separated zones. A servlet zone represents a collection of servlets
that
represent the same security restrictions. They are similar to a web server's
virtual host.
Local Mode - This is when the web server and the
servlet engine reside on the same machine. This is the fastest and most common
mode of operation because the client (web server) communicates with the servlet
engine over a local loopback interface (127.0.0.1) instead of over the
network.
Remote Mode - This is when the web server and
the servlet engine reside on different machines. This mode might be desirable
when load balancing.
Log files
Mod_jserv.log
Mod_jserv.log records messages and errors encountered by Apache's mod_jserv
module.
This includes messages about communicating via AJP to JServ, the servlet
engine. When this log file is disabled, its messages are redirected to Apache's
error_log. The number of messages logged to this file can be adjusted by
setting
the log level. Directives for disabling or changing the log file
(ApJServLogFile)
and for setting the logging level (ApJServLogLevel) are found in conf/jserv.conf
file. Mod_jserv.log is created at Apache startup time, if it doesn't already
exist, similar to Apache's other configuration files.
Jserv.log
Jserv.log records messages and errors encountered by the servlet engine.
Directives
to control this log file are found in the jserv.properties file. Jserv.log
can be configured to also log servlet exceptions and various internal debugging
information. Due to the large number of objects created and managed for
logging,
logging to jserv.log can cause performance degradation. This effect of
logging should be considered when deciding if or how much to log.
Using Java servlets
Writing Java servlets
Java servlet files on MPE follow the same rules and conventions as Java programs on MPE.
Any HPe3000 editor can be used to create a servlet file but, like Java
progams, Java servlets
must be in bytestream format. Servlets created with vi or using Samba from a PC
will always be in bytestream format. Servlet files created using traditional
HPe3000
editors such as EDIT/3000 and HP EDIT must be converted to bytestream using
the tobyte utility. For information on tobyte, execute "man tobyte" in the
POSIX shell to invoke the POSIX help facility.
The naming conventions for Java servlets follow those of Java programs:
- The name should be followed by the extension .java
- The name is case-sensitive. While not a requirement, it is conventional to
use mixed case names with an initial capital and with additional capitals used
if the class name contains more than one word (for example, HelloWorld.java)
Java servlet class definitions and documentation
The jsdk.jar servlet classes in the JSDK2.0 are required for proper
servlet
execution whether you write your own servlets or get them from another source.
The jsdk.jar file contains the javax.servlet and javax.servlet.http packages.
To view the classes in the jsdk.jar file, use the Java Archive Utility
:
shell/iX>/usr/local/java/latest/bin/jar tvf /APACHE/SECURE/lib/jsdk.jar
or
shell/iX> /usr/local/java/latest/bin/jar tvf /APACHE/PUB/lib/jsdk.jar
For servlet API documentation and examples, use the following URL on your own
server:
http://yourhost.com/JSDK2.0/doc/apidoc
An example servlet
Servlets can be written to call any of the Java APIs available to Java
applications.
Servlets use the Java standard extension classes in the packages
javax.servlet
(the basic Servlet framework) and javax.servlet.http (extensions of the servlet
framework for servlets that answer HTTP requests) that are in the jsdk.jar.
Since servlets are written in Java and follow a standard framework,
they are portable between operating systems.
Here is an example of a simple servlet.
File: HelloWorld.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
/**
* This is a simple example of an HTTP Servlet. It responds to the GET
* and HEAD methods of the HTTP protocol.
*/
public class HelloWorld extends HttpServlet
{
public void doGet (HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
PrintWriter out;
String title = "Hello World Servlet";
// set content type and other response header fields first
response.setContentType("text/html");
// then write the data of the response
out = response.getWriter();
out.println("<HTML><HEAD><TITLE>");
out.println(title);
out.println("</TITLE></HEAD><BODY bgcolor=\"#FFFFFF\"
>");
out.println("<H2><Hello, World!<br>");
out.println("</BODY></HTML>");
out.close();
}
}
The above is an example of an HTTP servlet since it extends the HttpServlet
class.
For HTTP servlets, an HTTP method (i.e. GET) is generally handled by a
corresponding
Java method (i.e. doGet). The method should return a response which
contains a body and header field(s) which describe the body. In this example,
the doGet method in the HttpServlet base class is overridden. The body is the
HTML
output and the header field is the Content-Type "text/html". This is an example
of a static servlet that returns the same output every time it is called.
Compiling Java servlets
The class path is where the JVM looks for class definitions. The jsdk.jar
file, which contains servlet class definitons, should be specified to the JVM
by adding its path to the CLASSPATH environment variable. CLASSPATH can be set
at the command line or in a profile file:
shell/iX>export CLASSPATH=/APACHE/PUB/lib/jsdk.jar:$CLASSPATH
or
shell/iX>export CLASSPATH=/APACHE/SECURE/lib/jsdk.jar:$CLASSPATH
Servlets are compiled using the Java compiler, javac. The path to the javac
binary can be fully qualified or the PATH variable can be modified to include
it. PATH can be set at the command line or in a profile file.
shell/iX>/usr/local/java/latest/bin/javac HelloWorld.java
or
shell/iX>export PATH=/usr/local/java/latest/bin:$PATH
shell/iX>javac HelloWorld.java
If you are planning to compile multiple servlets, you may want to set the
PATH
and CLASSPATH variables in the .profile file for MGR.APACHE or in the
system-wide
/etc/profile.local file. To create a new .profile file with these variables
for MGR.APACHE:
:HELLO MGR.APACHE
:XEQ SH.HPBIN.SYS -L
shell/iX>echo export PATH=/usr/local/java/latest/bin:$PATH > .profile
shell/iX>echo export CLASSPATH=/APACHE/PUB/lib/jsdk.jar:$CLASSPATH >> .profile
shell/iX>. ./.profile
Running Java servlets
The sample configuration files are set up to execute servlets from the
/APACHE/PUB/servlets
directory for standard Apache and from /APACHE/SECURE/servlets for WebWise.
This is where your servlet .class files must reside. This directory
is a servlet repository in the "root" zone and is configured in the
root.properties file:
repositories=/APACHE/SECURE/servlets
To run the servlet HelloWorld from the servlets repository, enter its URL in
your web browser:
http://yourhost.com:port/servlets/HelloWorld
or
http://yourhost.com:port/servlet/HelloWorld
"Hello World" is the name of the servlet to run and "servlets" and "servlet"
are the mount points for the "root" zone. Zone mount points are specified in
the jserv.conf file using the ApJServMount directive. Here are the ApJServMount
values set in the jserv.conf.sample file:
ApJServMount /servlets /root
ApJServMount /servlet /root
Mounting maps a URL to a zone. With the above mounts, requests to a URL
starting
with /servlets or /servlet will be handled by the servlets in the zone "root".
Servlets can be passed input in a manner similar to CGI. To run the servlet
"MyServlet" with an address input value, the URL would be,
http://yourhost.com:port/servlets/MyServlet?address=foo@bar.com
Adding class files
If your servlet requires any utility classes, you will need to add these
classes to the classpath variable in the
JServ startup job, JSRV or JSRVS:
shell/iX>cat /APACHE/PUB/JSRV
...
!setvar classpath "!hpcwd/libexec/ApacheJServ.jar:!hpcwd/lib/jsdk.jar"
...
!eoj
The wrapper.classpath directive in the jserv.properties.sample
file is not the correct place to set your classpath. The wrapper.classpath
value is only used when JServ is autostarted by Apache. This
mode of operation does not work well on MPE. The JSRV and JSRVS
startup jobs should be used instead on MPE to manually start up JServ.
New classes can be added to the class path by modifying the existing
setvar command or by adding an additional setvar command to JSRV:
!setvar classpath "/CLASSLIB:!hpcwd/libexec/ApacheJServ.jar:!hpcwd/lib/jsdk.jar"
or
!setvar classpath "!hpcwd/libexec/ApacheJServ.jar:!hpcwd/lib/jsdk.jar"
!setvar classpath "/CLASSLIB:!classpath"
|