The server startup script automatically sets the server’s CLASSPATH to include the standard servlet and JSP classes and the WEB-INF/classes directory (containing compiled servlets) of each Web application. But you need similar settings, or you will be unable to compile servlets in the first place. This section summarizes the configuration needed for servlet development.
The first thing you should do is create a directory in which to place the servlets and JSP pages that you develop. This directory can be in your home directory (e.g., ∼/ServletDevel on Unix) or in a convenient general location (e.g., C:ServletDevel on Windows). It should not, however, be in the server’s installation directory.
Eventually, you will organize this development directory into different Web applications (each with a common structure—see Chapter 4). For initial testing of your environment, however, you can just put servlets either directly in the development directory (for packageless servlets) or in a subdirectory that matches the servlet package name. Many developers simply put all their code in the server’s deployment directory (see Section 1.9). I strongly discourage this practice and instead recommend one of the approaches described in Section 1.8 (Establish a Simplified Deployment Method). Although developing in the deployment directory seems simpler at the beginning since it requires no copying of files, it significantly complicates matters in the long run. Mixing locations makes it hard to separate an operational version from a version you are testing, makes it difficult to test on multiple servers, and makes organization much more complicated. Besides, your desktop is almost certainly not the final deployment server, so you’ll eventually have to develop a good system for deploying anyhow.
|
Since I find myself frequently restarting the server, I find it convenient to place shortcuts to the server startup and shutdown icons inside my main development directory. You will likely find it convenient to do the same.
For example, for Tomcat on Windows, go to install_dir/bin, right-click on startup.bat, and select Copy. Then go to your development directory, right-click in the window, and select Paste Shortcut (not just Paste). Repeat the process for install_dir/bin/shutdown.bat. On Unix, you would use ln -s to make a symbolic link to startup.sh, tomcat.sh (needed even though you don’t directly invoke this file), and shutdown.sh.
For JRun on Windows, go to the Start menu, select Programs, select JRun 3.x, right-click on the JRun Default Server icon, and select Copy. Then go to your development directory, right-click in the window, and select Paste Shortcut. Repeat the process for the JRun Admin Server and JRun Management Console.
For the ServletExec Debugger (i.e., standalone development server), go to install_dir, right-click on StartSED40.bat, and select Copy. Then go to your development directory, right-click in the window, and select Paste Shortcut (not just Paste). There is no separate shutdown file; to stop ServletExec, just go to http://localhost/ (see Figure 1-5) and click on the Shutdown link in the General category on the left-hand side.
Since servlets and JSP are not part of the Java 2 platform, standard edition, you have to identify the servlet classes to the compiler. The server already knows about the servlet classes, but the compiler (i.e., javac) you use for development probably doesn’t. So, if you don’t set your CLASSPATH, attempts to compile servlets, tag libraries, or other classes that use the servlet API will fail with error messages about unknown classes. The exact location of the servlet JAR file varies from server to server. In most cases, you can hunt around for a file called servlet.jar. Or, read your server’s documentation to discover the location. Once you find the JAR file, add the location to your development CLASSPATH. Here are the locations for some common development servers:
Tomcat 4 Location.
install_dir/common/lib/servlet.jar
Tomcat 3 Location.
install_dir/lib/servlet.jar
JRun Location.
install_dir/lib/ext/servlet.jar
ServletExec Location.
install_dir/ServletExecDebugger.jar
Now, in addition to the servlet JAR file, you also need to put your development directory in the CLASSPATH. Although this is not necessary for simple packageless servlets, once you gain experience you will almost certainly use packages. Compiling a file that is in a package and that uses another class in the same package requires the CLASSPATH to include the directory that is at the top of the package hierarchy. In this case, that’s the development directory I just discussed in the first subsection. Forgetting this setting is perhaps the most common mistake made by beginning servlet programmers.
|
Finally, you should include “.” (the current directory) in the CLASSPATH. Otherwise, you will only be able to compile packageless classes that are in the top-level development directory.
Here are a few representative methods of setting the CLASSPATH. They assume that your development directory is C:devel (Windows) or /usr/devel (Unix/Linux) and that you are using Tomcat 4. Replace install_dir with the actual base installation location of the server. Be sure to use the appropriate case for the filenames. Note that these examples represent only one approach for setting the CLASSPATH. Many Java integrated development environments have a global or project-specific setting that accomplishes the same result. But these settings are totally IDE-specific and won’t be discussed here.
set CLASSPATH=.;
C:devel;
install_dircommonlibservlet.jar
setenv CLASSPATH .:
/usr/devel:
install_dir/common/lib/servlet.jar
Just as no serious programmer should develop general-purpose Java applications without access to the JDK 1.3 or 1.4 API documentation (in Javadoc format), no serious programmer should develop servlets or JSP pages without access to the API for classes in the javax.servlet packages. Here is a summary of where to find the API:
http://java.sun.com/products/jsp/download.html This site lets you download the Javadoc files for either the servlet 2.3 and JSP 1.2 API or for the servlet 2.2 and JSP 1.1 API. You will probably find this API so useful that it will be worth having a local copy instead of browsing it online. However, some servers bundle this documentation, so check before downloading.
http://java.sun.com/products/servlet/2.3/javadoc/ This site lets you browse the servlet 2.3 API online.
http://java.sun.com/products/servlet/2.2/javadoc/ This site lets you browse the servlet 2.2 and JSP 1.1 API online.
http://java.sun.com/j2ee/j2sdkee/techdocs/api/ This address lets you browse the complete API for the Java 2 Platform, Enterprise Edition (J2EE), which includes the servlet 2.2 and JSP 1.1 packages.
18.220.181.186