[ Team LiB ] |
![]() ![]() |
Recipe 2.1 Deploying an Individual Servlet on TomcatProblemYou want to take a compiled servlet and install it in Tomcat to find out if it is working. You are doing a preliminary test and do not want to take the time to build a complete web application for the servlet. SolutionCopy and paste the class file into Tomcat's default web application (or into a web application that you have already installed), then request it using the invoker servlet. Or use an Ant build.xml file to move the file temporarily into the Tomcat default web application. DiscussionSometimes you design a servlet and are anxious to see if the servlet works. Unless the servlet depends on other servlets or components in the application, you can test it on Tomcat by pasting the class file (including its package-related directories) into the default Tomcat web application. By default, this application is located at the path <Tomcat-installation-directory>/webapps/ROOT. If the fully qualified class name of the servlet is jspservletcookbook.CookieServlet, then here is the entire process for manually getting a single servlet going on Tomcat:
By now you are probably saying, "There must be a more elegant alternative to this slow, manual installation of a single servlet!" You are correct, and can use Jakarta Ant to convert this manual process to an automated one. The build.xml file in Example 2-1 accomplishes the same testing process, assuming you have downloaded and installed Ant as described in Chapter 4. Place this build file in a convenient directory. Create in that directory a global.properties file that is customized according to your needs (see Example 2-2). Change to that directory in a command-line window and type ant. Ant takes care of the rest of the tasks, including starting and stopping Tomcat's default web application. Example 2-1. Installing a servlet in the default web application<project name="Cookbook" default="deploy-servlet" basedir="."> <taskdef name="start" classname="org.apache.catalina.ant.StartTask" /> <taskdef name="stop" classname="org.apache.catalina.ant.StopTask" /> <!-- Load in some global properties --> <property file="global.properties" /> <target name="init" description="Initializes some properties."> <echo message="Initializing properties."/> <property name="build" value=".\build" /> <property name="src" value=".\src" /> <!-- The context-path is just a slash character when it is the ROOT application; see the start and stop targets, which already include the slash as part of the URL pattern --> <property name="context-path" value="" /> </target> <target name="prepare" depends="init"> <echo message="Cleaning up the build directory."/> <delete dir="${build}"/> <mkdir dir="${build}"/> </target> <!-- Set the CLASSPATH to various Tomcat .jar files --> <path id="classpath"> <fileset dir="${tomcat.dir}/common/lib"> <include name="*.jar" /> </fileset> <fileset dir="${tomcat.dir}/common/endorsed"> <include name="*.jar" /> </fileset> </path> <!-- start the default Tomcat web application --> <target name="start" description="Starts the default Web application"> <echo message="Starting the default application...."/> <start url="${url}" username="${username}" password="${password}" path="/${context-path}" /> </target> <!-- stop the default Tomcat web application --> <target name="stop" description="Stops the default Web application"> <echo message="Stopping the application...."/> <stop url="${url}" username="${username}" password="${password}" path="/${context-path}" /> </target> <!-- stop the default Tomcat web application, compile your servlet, add it to the default Web application, then start the default web application --> <target name="deploy-servlet" depends="prepare" description= "Compile the specified servlet, then move it into Tomcat's default Web application."> <echo message="Stopping the default Tomcat application...."/> <antcall target="stop"/> <echo message="Compiling the servlet...."/> <javac srcdir="${src}" destdir="${build}"> <include name="${compiled.servlet}.java" /> <classpath refid="classpath"/> </javac> <echo message= "Copying the servlet to Tomcat ROOT web application..."/> <copy todir="${tomcat.webapps}/WEB-INF/classes"> <fileset dir="${build}" /> </copy> <echo message="Starting the default application...."/> <antcall target="start"/> </target> </project> The global.properties file that sits in the same directory as build.xml looks like Example 2-2. Example 2-2. global.properties file for Anttomcat.webapps=k:/jakarta-tomcat-4.1.12/webapps/ROOT tomcat.dir=k:/jakarta-tomcat-4.1.12 url=http://localhost:8080/manager compiled.servlet=CookieServlet username=tomcat password=tomcat global.properties is just a list of property-name=value pairs. In other words, each line is composed of a string of characters that represents the property name (optionally including a period character), followed by an "=" sign and another bunch of characters that represents the value.
See AlsoThe deployment sections of Tomcat: The Definitive Guide, by Brittain and Darwin (O'Reilly); Recipe 2.2, Recipe 2.4, and Recipe 2.6; the Jakarta Ant online manual at: http://jakarta.apache.org/ant/manual/index.html |
[ Team LiB ] |
![]() ![]() |