Creating and Deploying the HelloWorld Web Application

To create a Web application and deploy it to a tc Runtime instance:

  1. When you install Ant, add or update the following environment variables:

    • ANT_HOME: Set this variable to the location where you installed Ant, such as /usr/local/ant/apache-ant-1.8.2.

    • PATH: Update your PATH variable to include ANT_HOME/bin.

  2. Set the JAVA_HOME environment variable to the directory where the JDK is installed.

  3. Create a project directory structure to contain the HelloWorld Web application source files.

    Create the top-level directory called helloworld. You can create the helloworld directory in any location on your computer that you have permission to update. The helloworld directory will contain the Ant build file (build.xml).

    Create two sub-directories of the helloworld directory: src to contain the Java source file for the HelloWorld servlet and web that will contain the JSP file, static HTML file, images, and deployment descriptor.

    In the src directory, create an examples sub-directory. This directory corresponds to the package that contains the HelloWorld servlet.

    In the web directory, create two subdirectories: images, which will contain any images used by the Web application, and WEB-INF, which is a standard directory that contains the Web application deployment descriptor files.

    The following graphic describes the project directory hierarchy:

    Figure 5. meDirectory Hierarchy of the HelloWorld Application

    meDirectory Hierarchy of the HelloWorld Application

  4. Create the Hello.java servlet Java source file and put it in the helloworld/src/examples directory.

    For sample Java code that you can copy and paste into your own Java file and a brief explanation of how to program a simple servlet, see Hello.java.

  5. Create the hello.jsp JSP file and put it in the helloworld/web directory.

    For sample JSP code that you can copy and paste into your own JSP file and a brief explanation of how to program a simple JSP, see hello.jsp.

  6. Create the web.xml Web application deployment descriptor and put it in the helloworld/web/WEB-INF directory.

    For sample XML that you can copy and paste into your own web.xml file and a brief explanation of the elements, see web.xml.

  7. Create the default index.html page and put it in the helloworld/web directory.

    For a sample HTML file that you can copy and paste into your own file, see index.html.

  8. Create the Ant build file (build.xml) that includes targets for compiling and packaging the Web application and put it in the helloworld directory.

    For a sample Ant build file that you can copy and paste into your own file, see build.xml.

  9. In your own build file, update the tcserver.home property to fit your environment; it should point to the CATALINA_HOME of your tc Runtime installation. For example, if you installed tc Server Standard Edition in the /opt/vmware directory, set the tcserver.home property in the build.xml file to something like the following:

    <property name="tcserver.home" value="/opt/vmware/vfabric-tc-server-standard/tomcat-6.0.25.A.RELEASE" />
  10. Right-click the following image and save it with name springsource.png to the helloworld/web/images directory.

    When you finish creating all the artifacts that make up the HelloWorld Web application, your directory structure and contents should look like the following:

    helloworld
    helloworld/build.xml
    helloworld/src
    helloworld/src/examples
    helloworld/src/examples/Hello.java
    helloworld/web
    helloworld/web/hello.jsp
    helloworld/web/images
    helloworld/web/images/springsource.png
    helloworld/web/index.html
    helloworld/web/WEB-INF
    helloworld/web/WEB-INF/web.xml 
  11. Compile and package the HelloWorld Web application by opening a command window, changing to the helloworld directory, and executing the following command:

    prompt> ant all

    This ant command creates a deployable WAR file of the HelloWorld application called hello.war in the helloworld/dist directory. You will see the following output from the ant command if it completes successfully:

    Buildfile: build.xml
    
    clean:
    
    prepare:
        [mkdir] Created dir: /home/samples/helloworld/dist
        [mkdir] Created dir: /home/samples/helloworld/work/WEB-INF/classes
         [copy] Copying 4 files to /home/samples/helloworld/work
    
    compile:
        [javac] Compiling 1 source file to /home/samples/helloworld/work/WEB-INF/classes
    
    dist:
          [jar] Building jar: /home/samples/helloworld/dist/hello.war
    
    all:
    
    BUILD SUCCESSFUL
    Total time: 2 seconds
  12. Start a tc Runtime instance. See Starting and Stopping tc Runtime Instances.

  13. Deploy the Web application JAR file to the tc Runtime instance. See Deploying Applications to tc Runtime Instances.

  14. Invoke the HelloWorld Web application in your browser:

    http://host:port/hello

    where:

    • host is the name of the computer that is hosting the tc Runtime instance. If it is the same as the computer hosting your browser, you can use localhost.

    • port is the port to which the tc Runtime instance listens. The default value is 8080.

    In the example, /hello is the default URL context of the Web application, which in this example is the name of the WAR package without the trailing .war file extension.

    For example:

    http://localhost:8080/hello