Defining Simple Servlet Classes
This section compares a simple Java Servlet with a simple Jython Servlet. The
section on testing these Servlets describes how to install the
jython.jar file in a Tomcat web application.
A Simple Java Servlet
Listing 12.1 is the most basic of Java Servlets.
Listing 12.1 A Basic Java Servlet
""'// Filename: JavaServlet.java
import javax.servlet.*;
import java.io.*;
public class JavaServlet extends GenericServlet {
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter toClient = res.getWriter();
toClient.println("<html><body>" +
"This is a Java Servlet." +
"</body></html>");
}
}
A base class from the Servlet API is required, and Listing 12.1 uses
GenericServlet, imported from the javax.servlet package in the
first line. The service method in GenericServlet is abstract,
so it must be implemented in any subclasses. This is the method invoked in
response to a web request for JavaServlet (the name of the file in
Listing 12.1). Output destined for the client is sent through a
PrintWriter object retrieved from the ServletResponse object.
This could also be an OutputStream for binary data.
A Simple Jython Servlet
Implementing a Jython Servlet comparable to Listing 12.1 should be similar in
what it imports, inherits, and implements, but done in Jython's syntax.
Listing 12.2 demonstrates.
Listing 12.2 A Basic Jython Servlet
# Filename: JythonServlet.py
from javax import servlet
import random # Not used, just here to test module imports
class JythonServlet(servlet.GenericServlet):
def service(self, req, res):
res.setContentType("text/html")
toClient = res.getWriter()
toClient.println("""<html><body>
This is a Servlet of the Jython variety.
</body></html>""")
Listing 12.2 is a subclass of GenericServlet just like its Java
counterpart in Listing 12.1. It also implements the service() method of
GenericServlet as required. Syntactic differences in Listing 12.2
include parentheses after the class definition to designate the superclass, the
omission of the throws statement, an explicit self parameter
in the service method, and, of course, the absence of semicolons and
explicit type declarations. Additionally, the import statements differ in
Jython. As stated earlier, the from module import * syntax is strongly
discouraged; instead, Listing 12.2 imports the parent package. One
additional import in Listing 12.2 is the random module. This module is
not actually used, and only exists to test module imports.
Testing the Java and Jython Servlets
Testing the Servlets in Listings 12.1 and 12.2 requires the installation of
Tomcat. The Jython Servlet in Listing 12.2 additionally requires including the
jython.jar file in Tomcat. This section first describes the steps to
installing Tomcat, then addresses the installation and testing of the two
Servlets discussed.
Installing Tomcat
The first step is to download Tomcat from
http://jakarta.apache.org.
This section addresses the installation of a binary release of Tomcat in
stand-alone mode. The suggested version to download is
jakarta-tomcat-3.2.3. Download the zip or tar.gz file
appropriate for your platform.
Next, unzip or untar the archive to the directory in which you have
sufficient permissions. If you unzip the archive into the
C:\jakarta-tomcat-3.2.3 directory, this becomes the Tomcat home
directory. If you use /usr/local/jakarta-tomcat-3.2.3, this becomes the
Tomcat home directory. You should set an environment variable to the Tomcat home
directory. For the directory C:\jakarta-tomcat-3.2.3 on Windows, add
the following to your environment settings:
set TOMCAT_HOME=c:\jakarta-tomcat-3.2.3
For the directory /usr/local/jakarta-tomcat-3.2.3 on *nix,
add the following to your bash environment:
export TOMCAT_HOME=/usr/local/jakarta-tomcat-3.2.3
If you do not set the TOMCAT_HOME environment variable, you must
start Tomcat from within its home or bin directory.
Next, set the environment variable JAVA_HOME to the root directory
of your JDK installation. Here's an example using JDK1.3.1:
# on Windows
set JAVA_HOME=c:\jdk1.3.1
# bash (*nix) setup
export JAVA_HOME=/usr/java/jdk1.3.1
The installation is complete. You can start Tomcat with the startup script
appropriate for your platform:
# Windows
%TOMCAT_HOME%\bin\startup.bat
# bash (*unix)
$TOMCAT_HOME/bin/startup.sh
You should see startup information printed to the screen as the Tomcat server
loads. An important line to look for is this:
date time - PoolTcpConnector: Starting HttpConnectionHandler on 8080
This designates the port that you will be using to connect to the Servlet
container: 8080 is the default. When you see this, Tomcat is running and ready
to accept connections on port 8080.
When you wish to stop Tomcat, use the following shutdown scripts:
# Windows
%TOMCAT_HOME%\bin\shutdown.bat
# bash (*nix)
$TOMCAT_HOME/bin/shutdown.sh
The Servlet 2.2 specification designates a directory hierarchy for web
applications that begins in the directory %TOMCAT_HOME%\webapps.
Folders within this directory are web applications, or contexts, and each
follows a specific directory hierarchy. The examples in this chapter use the
context named jython. The directory structure required for the
jython context is shown here:
%TOMCAT_HOME%\webapps\
%TOMCAT_HOME%\webapps\jython The context's root
%TOMCAT_HOME%\webapps\jython\WEB-INF
%TOMCAT_HOME%\webapps\jython\WEB-INF\classes Servlet classes
%TOMCAT_HOME%\webapps\jython\WEB-INF\lib Library archives
You should create these directories before continuing with the examples. If
you restart Tomcat, you should see an additional line of information when it
restarts. The following line confirms that Tomcat has loaded the new
context:
date time - ContextManager: Adding context Ctx( /jython )
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.