Thursday, December 28, 2006

Virtual host redirection in tomcat



Go to install_dir/webapps/ROOT/WEB-INF, right-click on the classes directory, and select Copy. Then go to your development directory, right-click, and select Paste Shortcut (not just Paste). Note that if you use my preconfigured Jakarta Tomcat version, this shortcut is already in your C:\Servlets+JSP directory. Now, whenever you compile a packageless servlet, just drag the class files onto the shortcut. When you develop in packages, use the right mouse to drag the entire package directory (e.g., the coreservlets directory) onto the shortcut, release the mouse, and select Copy. On Unix/Linux, you can use symbolic links (created with ln -s) in a manner similar to that for Windows shortcuts.

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 Chapters 4-6 of More Servlets and JSP for details on Web applications). For instance, with Tomcat, you can easily make your own Web application by putting a copy of the install_dir/webapps/ROOT directory into your development directory and renaming it (for example, to testApp). Now, to deploy your Web application, just make a shortcut to install_dir/webapps and copy the entire Web application directory (e.g., testApp) each time by using the right mouse to drag the directory that contains your Web application onto this shortcut and selecting Copy (say Yes when asked if you want to replace existing files). The URL stays almost the same as it was without Web applications: just insert the name of the directory after the hostname (e.g., replace http://localhost/blah/blah with http:/localhost/testApp/blah/blah).

Note that the preconfigured Tomcat version already contains all the test files, has shortcuts from the development directory to the deployment locations, and has shortcuts to start and stop the server.

One disadvantage of this approach is that it requires repeated copying if you use multiple servers. For example, I usually have Apache Tomcat, Macromedia JRun, and Caucho Resin on my desktop system and regularly test my code with 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 does not matter on your desktop development server, but when you get to the "real" deployment server, you won't want to include the source code files.

No comments:

Post a Comment