1.6. Set Up Your Development Environment

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.

Create a Development Directory

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.

Core Warning

Don’t use the server’s deployment directory as your development location. Instead, keep a separate development directory.


Make Shortcuts to Start and Stop the Server

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.

Set Your CLASSPATH

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.

Core Approach

Remember to add your development directory to your CLASSPATH . Otherwise, you will get “Unresolved symbol” error messages when you attempt to compile servlets that are in packages and that make use of other classes in the same package.


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.

  • Windows 98/Me. Put the following in your autoexec.bat. (Note that this all goes on one line with no spaces—it is broken here for readability.)

    set CLASSPATH=.; 
                  C:devel; 
                  install_dircommonlibservlet.jar 

  • Windows NT/2000. Go to the Start menu and select Settings, then Control Panel, then System, then Environment. Then, enter the CLASSPATH value from the previous bullet.

  • Unix/Linux (C shell). Put the following in your .cshrc. (Again, in the real file it goes on a single line without spaces.)

    setenv CLASSPATH .: 
                     /usr/devel: 
                     install_dir/common/lib/servlet.jar 

Bookmark or Install the Servlet and JSP API Documentation

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:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.30.210