2.9 Establish a Simplified Deployment Method

OK, so you have a development directory. You can compile servlets with or without packages. You know which directory the servlet classes belong in. You know the URL that should be used to access them (at least the default URL; in Section 2.11, "Web Applications: A Preview," you'll see how to customize that address). But how do you move the class files from the development directory to the deployment directory? Copying each one by hand every time is tedious and error prone. Once you start using Web applications (see Section 2.11), copying individual files becomes even more cumbersome.

There are several ways to simplify the process. Here are a few of the most popular ones. If you are just beginning with servlets and JSP, you probably want to start with the first option and use it until you become comfortable with the development process. Note that we do not list the option of putting your code directly in the server's deployment directory. Although this is one of the most common choices among beginners , it scales so poorly to advanced tasks that we recommend you steer clear of it from the start.

  1. Copying to a shortcut or symbolic link.

  2. Using the -d option of javac .

  3. Letting your IDE take care of deployment.

  4. Using ant or a similar tool.

Details on these four options are given in the following subsections.

Copying to a Shortcut or Symbolic Link

On Windows, go to the server's default Web application, right-click on the classes directory, and select Copy. Then go to your development directory, right-click, and select Paste Shortcut (not just Paste). Now, whenever you compile a packageless servlet, just drag the class files onto the shortcut. When you develop in packages, use the right mouse button to drag the entire directory (e.g., the coreservlets directory) onto the shortcut, release the mouse button, and select Copy. See Figure 2-15 for an example setup that simplifies testing of this chapter's examples on Tomcat, JRun, and Resin. On Unix, you can use symbolic links (created with ln -s ) in a manner similar to that for Windows shortcuts.

Figure 2-15. Using shortcuts to simplify deployment.


An advantage of this approach is that it is simple. So, it is good for beginners who want to concentrate on learning servlets and JSP, not deployment tools. Another advantage is that a variation applies once you start using your own Web applications (see Section 2.11). Just make a shortcut to the main Web application directory (typically one level up from the top of the default Web application), and copy the entire Web application each time by using the right mouse button to drag the directory that contains your Web application onto this shortcut and selecting Copy.

One disadvantage of this approach is that it requires repeated copying if you use multiple servers. For example, we keep three different servers (Tomcat, JRun, and Resin) on our development system and regularly test the code on all three servers. A second disadvantage is that this approach copies both the Java source code files and the class files to the server, whereas only the class files are needed. This may not matter much on your desktop server, but when you get to the "real" deployment server, you won't want to include the source code files.

Using the -d Option of javac

By default, the Java compiler ( javac ) places class files in the same directory as the source code files that they came from. However, javac has an option ( -d ) that lets you designate a different location for the class files. You need only specify the top-level directory for class files javac will automatically put packaged classes in subdirectories that match the package names . So, for example, with Tomcat you could compile the HelloServlet2 servlet (Listing 2.4, Section 2.8) as follows (line break added only for clarity; omit it in real life).

 javac -d  install_dir  /webapps/ROOT/WEB-INF/classes          HelloServlet2.java 

You could even make a Windows batch file or Unix shell script or alias that makes a command like servletc expand to javac -d install_dir /.../classes . See http://java.sun.com/j2se/1.4/docs/tooldocs/win32/javac.html for more details on -d and other javac options.

An advantage of this approach is that it requires no manual copying of class files. Furthermore, the exact same command can be used for classes in different packages since javac automatically puts the class files in a subdirectory matching the package.

The main disadvantage is that this approach applies only to Java class files; it won't work for deploying HTML and JSP pages, much less entire Web applications.

Letting Your IDE Take Care of Deployment

Most servlet- and JSP-savvy development environments (e.g., IBM WebSphere Studio Application Developer, Sun ONE Studio, Borland JBuilder, Eclipse) have options that let you specify where to deploy class files for your project. Then, when you tell the IDE to build the project, the class files are automatically deployed to the proper location (package-specific subdirectories and all).

An advantage of this approach, at least in some IDEs, is that it can deploy HTML and JSP pages and even entire Web applications, not just Java class files. A disadvantage is that it is an IDE-specific technique and thus is not portable across systems.

Using ant or a Similar Tool

Developed by the Apache foundation, ant is a tool similar to the Unix make utility. However, ant is written in the Java programming language (and thus is portable) and is touted to be both simpler to use and more powerful than make . Many servlet and JSP developers use ant for compiling and deploying. The use of ant is especially popular among Tomcat users and with those developing Web applications (see Section 2.11). Use of ant is discussed in Volume 2 of this book.

For general information on using ant , see http://jakarta.apache.org/ant/manual/. See http://jakarta.apache.org/tomcat/tomcat-4.1-doc/appdev/processes.html for specific guidance on using ant with Tomcat.

The main advantage of this approach is flexibility: ant is powerful enough to handle everything from compiling the Java source code to copying files to producing Web archive (WAR) files (see Section 2.11, "Web Applications: A Preview"). The disadvantage of ant is the overhead of learning to use it; there is a steeper learning curve with ant than with the other techniques in this section.

Core Servlets and JavaServer Pages (Vol. 1.Core Technologies)
Core Servlets and Javaserver Pages: Core Technologies, Vol. 1 (2nd Edition)
ISBN: 0130092290
EAN: 2147483647
Year: 2002
Pages: 194

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net