Installing the Java Servlet
To install the Java Servlet from Listing 12.1, first place the
JavaServlet.java file in the directory
%TOMCAT_HOME%\webapps\jython\WEB-INF\classes. This directory is the
root directory for class files. Because JavaServlet is not within a
package, it belongs in the root of the classes directory. Note that
Listing 12.1 is not within a package; if you had chosen to designate a package,
such as the package demo for example, you would have then placed
the compiled class file in the classes\demo directory in order to
comply with Java class directory structures. This is only a note for those
Servlets placed within a package, which is not the case for our example,
however.
From within that directory, compile the JavaServlet.java file with
the following command:
javac -classpath %TOMCAT_HOME%\lib\servlet.jar JavaServlet.java
After compiling JavaServlet, you are ready to view it. First, start the
Tomcat server, and then point your browser to
http://localhost:8080/jython/servlet/JavaServlet.
You should see the simple string message This is a Java Servlet.
Installing the Jython Servlet
There are two ways to use Jython Servlets. One is to compile the Servlet with
jythonc and place the resulting class files in the
%TOMCAT_HOM%\jython\WEB-INF\classes directory. The other is to use
Jython's PyServlet mapping class. This section uses
jythonc. The PyServlet mapping is often a better way to deploy
Jython Servlets, but jythonc-compiled Servlets are equally sensible at
times.
The three steps required to install a jythonc-compiled Servlet in
Tomcat are as follows:
Compile the Jython Servlet module with jythonc.
Add the jython.jar file to the web application. Make
the modules from Jython's lib directory available to Jython
Servlets.
Compiling a Jython Servlet with jythonc
Compiling with jythonc requires that the servlet.jar file
exists within the classpath. The servlet.jar file contains the
javax.Servlet.* classes and packages, and it is found in Tomcat's
lib directory (%TOMCAT_HOME%\lib). If you use jythonc
to compile a Servlet without servlet.jar in the classpath,
there are no errors or warnings during the compilation; however, when you try to
run a Servlet compiled that way, you will get a
java.lang.ClassCastException (at least that is the case for
jythonc at the time of this writing).
Place the JythonServlet.py file from Listing 12.2 to the directory
%TOMCAT_HOME%\jython\WEB-INF\classes. Ensure that your environment
CLASSPATH variable does in fact include Servlet.jar, and then
use jythonc to compile the Jython code into Java classes by using the
following command from within the %TOMCAT_HOME%\jython\WEB-INF\classes
directory:
jythonc w . JythonServlet.py
Specifying the current working directory with the w switch
eliminates the need to copy the generated class files from the jpywork
directory. There should be two class files in the classes directory.
Remember that a compiled Jython file will have at least two associated class
files. The files produced from compiling JythonServlet.py with jythonc
should be JythonServlet.java, JythonServlet.class, and
JythonServlet$_PyInner.class. Both the class files are required to use
the Servlet and must both be in the WEB-INF\classes directory.
During the compilation with jythonc, it is important to look for the
lines that read as follows:
Creating .java files:
JythonServlet module
JythonServlet extends javax.servlet.GenericServlet
If you do not see these lines, something is wrong and the Servlet will not
work. Double-check the CLASSPATH and setup options and compile
the file again until you see the lines noted previously.
Adding jython.jar to the classpath
All Jython Servlets must have access to the classes within the
jython.jar file. There are three ways to add the jython.jar
file to Tomcat's classpath:
Add jython.jar to the context's lib directory.
This is the preferred way.
Add jython.jar to Tomcat's lib directory. This
method is discouraged.
Leave jython.jar in Jython's installation directory, but
add it to the classpath before running Tomcat. This is reasonable, but
not as good as placing it in the context's lib directory.
The preferred approach is to place the jython.jar file in the
context's lib directory. The context should include the directory
{context}\WEB-INF\lib. For the jython context used in this
chapter, it is %TOMCAT_HOME%\webapps\ jython\WEB-INF\lib. Class archive
files, such as jython.jar, belong in this directory. This is preferred
because it makes a self-contained web application. As soon as a web application
requires access to archives outside of its context, archiving, packaging, and
installing the application on other servers becomes troublesome. You are
strongly urged to keep all web applications self-contained unless you are sure
it is not necessary in your situation.
You can also place the jython.jar file in Tomcat's lib
directory (%TOMCAT_HOME%\lib). Jar files in this directory are
automatically added to the classpath. However, this is the least
preferred of the three approaches. You may reduce duplicate jython.jar
files, but your web application is no longer self-contained. Additionally, you
do not get automatic access to Jython's lib directory as you do
with the third approach.
The third option is to leave the jython.jar file in Jython's
installation directory, and add it to the classpath before starting
Tomcat. This also eliminates duplicate jython.jar files; however, it
has the added advantage of providing access to the registry file and
Jython's lib directory. Remember that the registry is sought in
the python.home directory, or in the location in which the
jython.jar file was found if there is no python.home property.
Leaving the jython.jar file in Jython's installation directory is
therefore an advantage over placing it in Tomcat's lib directory.
It's worth mentioning again, however, that a self-contained context is
preferred.
Making Jython's lib Directory Available to
Servlets
There are three ways to make the modules in Jython's lib
directory available to Servlets:
If you chose to leave the jython.jar file in Jython's
installation directory, no additional steps are required to gain access to
Jython's lib directory. If you chose to place the
jython.jar file in the context's lib directory, you must
set the python.home property, explicitly append a directory to
sys.path, or freeze the modules before your Jython Servlets can use the
Jython module library.
To set the python.home property you can set the TOMCAT_OPTS
environment variable. Before you do this, you must decide where the modules will
be located. Again, the best way is to create a self-contained web application. A
good recommendation is to create an additional directory within the
context's WEB-INF directory. The name of this directory will be
jylib for the purposes of this section. Create the directory
%TOMCAT_HOME%\webapps\jython\WEB-INF\jylib. Because Jython looks for
the lib directory within python.home, continue by also making
the directory %TOMCAT_HOME%\webapps\jython\ WEB-INF\jylib\Lib. Place
any required modules within this directory, and specify the jylib
directory as the python.home. Here are some examples that set
TOMCAT_OPTS to the proper python.home property setting:
# Windows
set TOMCAT_OPTS=-Dpython.home=
%TOMCAT_HOME%\webapps\jython\WEB-INF\jylib
# bash (*nix)
export TOMCAT_OPTS=-Dpython.home=
$TOMCAT_HOME/webapps/jython/WEB-INF/jylib
Note:Color coded lines have been split for display purposes
Note that some versions of Windows do not allow an equals sign in an
environment string. In this case, you must edit the tomcat.bat file to
include the python.home parameter setting.
It is possible to just begin Servlets by explicitly adding the module
directory to sys.path. The problem is that this often requires
explicit, machine-dependent paths, and thus limits cross-platform portability.
Here is an example of what would appear at the top of a Servlet where you wish
to explicitly append to the sys.path:
import sys
libpath = "c:/jakarta-tomcat_3.2.3/webapps/jython/WEB-INF/jylibs/Lib"
sys.path.append(libpath)
Another useful approach to making Jython's modules available is to
freeze them. The jythonc deep option compiles all
required modules, making the python.home and Jython's lib
directory unimportant. To freeze the JythonServlet.py file from Listing
12.2, use the following command from within the Jython context's
classes directory:
jythonc w . deep JythonServlet.py
Class files for the JythonServlet and for all the modules it
requires are now located within the context's classes directory.
In other words, the Servlet and modules are now installed in a self-contained
web application. Note that compiling other Jython Servlets this way will
overwrite any previously compiled modules. This means you must be careful when
updating modules, as a newer version may adversely affect an older Servlet.
Compiling with the deep options creates a number of files,
but the generated *.java files may be deleted after compilation.
Freezing is beneficial because changes to modules are infrequent, and because
it is an easy way to make a fully self-contained web application. You don't
need to set the python.home property. A web application set up this way
can simply be archived as a .war file and plugged into any other
compliant server without a single extra installation step required.
Testing the Jython Servlet
With the Servlet from Listing 12.2 compiled with jythonc, the
jython.jar in the classpath, and Jython's modules
accessible, you can now view it. Point you browser to
http://localhost:8080/jython/servlet/jythonServlet.
You should see the simple message "This is a Servlet of the Jython
variety." If this is not what you see, the likely alternatives are one of
three error messages. If the Servlet.jar file was not in the
classpath while compiling the JythonServlet, you will likely
see a ClassCastException. You will also see a
ClassCastException if the filename and classname differ, even if only
in capitalization. If one of the class files generated from compiling the
Servlet with jythonc is in the context's classes
directory, you will see an AttributeError. If Jython's modules are
not available, you will see an ImportError.
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.
|