[ Team LiB ] Previous Section Next Section

Recipe 2.3 Deploying an Individual Servlet on WebLogic


You want to take your compiled servlet and install it in BEA WebLogic Server 7.0 to find out if it is working.


Copy and paste the class file into WebLogic's default web application (or into a web application that you have already installed). Use the WebLogic Administration Console to alter the web.xml file and give the servlet a sensible name with which to request it in a browser, or use an Ant build file to move the file temporarily into the WebLogic default web application.


WebLogic 7.0's default web application is located on the following path: <WebLogic-installation-directory>/user_projects/<mydomain>/applications/DefaultWebApp. In the default installation of the WebLogic 7.0 server, not much exists in the default web application but a web.xml deployment descriptor, and some image files. To add a servlet to the default application, paste your servlet class, including its package-related directories, into the DefaultWebApp/WEB-INF/classes directory. You might have to create a classes directory the first time you do this. Change the web.xml file to give the servlet a name (which is easier through the Administration Console) before redeploying the web application as described in Recipe 2.4.

Use the Administration Console to edit the web.xml file in order to give the new servlet a registered name and servlet-mapping element. You can also use another available tool, such as WebLogic Builder (Recipe 2.9) or a text editor. Figure 2-1 shows the DefaultWebApp in the Administration Console. Click on "Edit Web Application Deployment Descriptors . . . ".

Figure 2-1. WebLogic Server Administration Console

This displays the screen shown in Figure 2-2. This screen provides an easy graphical method of editing the web.xml file for any web application (in this case, the WebLogic default web application).

Figure 2-2. Editing the web.xml file graphically

With this graphical editor, create the servlet and servlet-mapping elements for the servlet that you just added. Make sure to click on the "Web Descriptor" button in the left column of the Figure 2-2 window and then persist the changes that you made in the web.xml file. This action rewrites the web.xml file, adding the new servlet and servlet-mapping elements.

Now redeploy the web application, which is just a matter of clicking a few hypertext links in the Console. Choose the name of your web application in the left column of the Console, under the mydomain Deployments Web Applications node of the tree navigation structure in this lefthand column. Figure 2-3 shows the resulting window.

Figure 2-3. Using the Console to redeploy a web application

Click on the "Deploy" tab, then click the "Undeploy" button in the resulting HTML table. The web application is now unavailable for service.

To redeploy the application, click the "Deploy" tab, then select the "Deploy" button, as shown in Figure 2-4.

Figure 2-4. Graphically deploying a servlet

If the servlet that you are working on already exists in the web application, then you can also copy and paste a new servlet class over the old one in the WEB-INF/classes directory of the web application. The new servlet version becomes available immediately, without using the Console to redeploy the entire web application.

You can also use an Ant file to compile the servlet and copy it into WebLogic's default web application. The build file in Example 2-3 is very similar to the one used and described in Recipe 2.1; it's just revised for use with WebLogic's web container instead of Tomcat's.

Example 2-3. Using an Ant file with a WebLogic servlet
<project name="Cookbook" default="deploy-servlet" basedir=".">
        <property file="wl.properties" />
        description="Initializes some properties.">
        <echo message="Initializing properties."/>
        <property name="build" value=".\build" />
        <property name="src" value=".\src" />
  <target name="prepare" depends="init">
         <echo message="Cleaning up the build directory."/>
         <delete dir="${build}"/>
         <mkdir dir="${build}"/>
  <path id="classpath">
       <fileset dir="${wl.dir}\server\lib">
             <include name="*.jar" />

  <target name="deploy-servlet" depends="prepare" 
                description="Compile the specified servlet, then move it into 
                WL's default Web application.">
          <echo message="Compiling the servlet ${compiled.servlet}...."/>
          <javac srcdir="${src}" destdir="${build}">
                <include name="${compiled.servlet}.java" />  
                <classpath refid="classpath"/>
         <echo message="Copying the servlet to WL default web application..."/>
         <copy todir="${wl.webapp}/WEB-INF/classes">
         <fileset dir="${build}" />

This Ant build file first loads a set of properties contained in a file called wl.properties, which is located in the same directory as the build file. The build file typically has the name build.xml; however, you can call another build file in the same directory by using the -buildfile command-line option, as in ant -buildfile wl_build.xml. The wl.properties file for this example is shown in Example 2-4.

Example 2-4. wl.properties for WebLogic Ant build file

The deploy-servlet target depends on a target named prepare that is also defined in this build file. The prepare target in turn has "init" as its depends attribute, which means that the init target executes prior to the prepare target. So calling the deploy-servlet target creates a chain of executing targets: init prepare deploy-servlet. In all, this is what the build file accomplishes:

  1. init creates a couple of properties (build and source) that point to directories.

  2. The prepare target deletes and then remakes the build directory, so that you start with a clean build.

  3. deploy-servlet compiles the servlet into the build directory, then copies it into the directory specified by the wl.webapp property (which contains its value in the wl.properties file).

The path element creates a classpath out of the JAR files found in the k:/bea/weblogic700/server/lib directory. This directory path is how Ant resolves the phrase "${wl.dir}\server\lib," which is parsed by attaching the value of the property wl.dir to the string "\server\lib."

See Also

Recipe 2.5, Recipe 2.7-Recipe 2.10; the deployment sections of WebLogic: The Definitive Guide, by Mountjoy and Chugh (O'Reilly); WebLogic's Server 7.0 programmer documentation: http://e-docs.bea.com/wls/docs70/programming.html .

    [ Team LiB ] Previous Section Next Section