You configured and tested the server, so you're all set, right? Well, no, not quite. That's just the local deployment environment. You still have to set up your personal development environment. Otherwise, you won't be able to compile servlets and auxiliary Java classes that you write. Configuring your development environment involves the following steps.
The following subsections give details on each of these steps. Creating a Development DirectoryThe first thing you should do is create a directory in which to place the servlets and JSP documents 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 structuresee Section 2.11, "Web Applications: A Preview"). 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 . After compiling, you can simply copy the class files to the server's default Web application. Many developers put all their code in the server's deployment directory (see Section 2.10). We strongly discourage this practice and instead recommend one of the approaches described in Section 2.9 (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 development and deployment 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
Setting Your CLASSPATHSince servlets and JSP are not part of the Java 2 Platform, Standard Edition, you must 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 in the install_dir /lib directory. 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:
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 we discussed in the first subsection. Forgetting this setting is perhaps the most common mistake made by beginning servlet programmers. Core Approach
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:\ServletDevel (Windows) or /usr/ServletDevel (Unix) 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; they are case sensitive (even on a Windows platform!). If a Windows path contains spaces (e.g., C:\Documents and Settings\ Your Name \My Documents\... ), enclose it in double quotes. Note that these examples represent only one approach for setting the CLASSPATH . For example, you could create a script that invokes javac with a designated value for the -classpath option. In addition, many Java integrated development environments have a global or project-specific setting that accomplishes the same result. But those settings are totally IDE specific and aren't discussed here.
Making Shortcuts to Start and Stop the ServerDuring our development, we find ourselves frequently restarting the server. As a result, we find it convenient to place shortcuts to the server startup and shutdown icons inside the main development directory or on the desktop. 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 . Some users like to put the shortcuts on the desktop or their Start menu. If you put the shortcuts there, you can even right-click on the shortcut, select Properties, then enter a keyboard shortcut by typing a key in the "Keyboard shortcut" text field. That way, you can start and stop the server just by pressing Control-Alt- SomeKey on your keyboard. 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 Macromedia JRun 4, right-click on the JRun Launcher icon, 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 the JRun Management Console if you so desire . There is no separate shutdown icon; the JRun Launcher lets you both start and stop the server. For Resin on Windows, right-click on install_dir /bin/httpd.exe , 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 icon; invoking httpd.exe results in a popup window with a Quit button that lets you stop the server. Bookmarking or Installing the Servlet and JSP API DocumentationJust as no serious programmer should develop general-purpose Java applications without access to the Java 1.4 or 1.3 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. (Remember that the source code archive at http://www.coreservlets.com/ has up-to-date links to all URLs cited in the book, in addition to the source code for all examples.)
|