Developing Applications with Tomcat


Integrating a Sample Application with Tomcat



NB. The batch files found in %TOMCAT_HOME%\bin have been written and tested under Windows NT. Some modifications may be necessary for operation on a Windows 98 platform.


If you surf to the following site and scroll to the bottom of the page you will see a hyperlink labeled Download Sample DataAccess Servlet. Click on the link and save the respected zip file to a directory on your drive. Now unzip the file to the same directory and you see the following files:




Setting up the sample project folder

We are going to create a directory within the webapps folder for our sample application and move the required source files to the respective directories.

  1. Create a directory within the %TOMCAT_HOME%\webapps folder named student.

  2. Browse (using Windows Explorer) to the %TOMCAT_HOME%\doc\appdev\sample folder and copy the contents of that folder to our student folder. We should have 3 sub-directories within our student folder now: web, etc and src and 3 build files, 1 .xml, a .bat(ch) file and .sh (linux). Delete the contents of the src and web directories.

  3. Copy index.html and StudentRegistration.html to the web directory.

  4. Copy the java source files to the src directory.



Required editing of files

  1. Open the build.xml file in a text editor and change the following line

       property name="app.name" value="myapp"/

    to read

       property name="app.name" value="student"/   (context path)

  2. Open the build.bat file in a text editor also and add the following lines in the respective area:

       set CLASSPATH=%CLASSPATH%;%TOMCAT_HOME%\lib\jaxp.jar

       set CLASSPATH=%CLASSPATH%;%TOMCAT_HOME%\lib\parser.jar

       set CLASSPATH=%CLASSPATH%;%TOMCAT_HOME%\lib\jasper.jar

       set CLASSPATH=%CLASSPATH%;%JAVA_HOME%\lib\tools.jar

  3. Open the index.html as well and change the href value of "/servlet/StudentDBServlet" to "student".
    student is the value of the url mapping we are going to give for our servlet in the servlet mapping specification in the web.xml file. See modifications of the web.xml file below.

  4. Open the StudentRegistration.html file

  5. Open the StudentDBServlet.java file

  6. Open the web.xml file and change to file to resemble the following:

    <web-app>
        <display-name>Stduent Database Demo</display-name>
        <description>
    	This is a simple web application with a source code organization
    	based on the recommendations of the Application Developer's Guide.
        </description>
        <servlet>
            <servlet-name>StudentServlet</servlet-name>
            <servlet-class>StudentDBServlet</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>StudentServlet</servlet-name>
    	<url-pattern>/student</url-pattern>
        </servlet-mapping>
    </web-app>
    
    Please note that the url-pattern value DOES NOT have to 
    be the same as the value for the app.name property in the build.xml file.
    


Building of the Web Application

We now are ready to build our web application.

  1. Open a command window and go to the project root directory.

  2. Set the JAVA_HOME and TOMCAT_HOME variables.

  3. Issue the build command to build the application.

As a result of the build command our application is now built (once there are no errors) and you should see the following new folders, files in your application root directory: javadoc and Web-inf directories and index.html and StudentRegistration.html are now in the root directory of the sample application.


Testing the Application

Before we can actually test our application via a web browser we need to set up the Data Source. Remember the Access database file from the zip file, well we are going to use that as our ODBC Data Source. Follow the instructions below:

  1. From the Start menu, select Settings, then Control Panel and then double-click on the ODBC Data Sources icon.

  2. This should bring up a window labeled ODBC Data Source Administrator and click the Add button.

  3. We are now going to create a new data source. From the list of drivers displayed select the Microsoft Access driver and click Finish.

  4. A window labeled ODBC Microsoft Access 97 Setup is displayed and in the text box labeled Data Source Name type StudentDatabase. Click the Select button and browse to the directory where you saved student.mdb. Select this database and click Ok. Click Ok again when the ODBC Microsoft Access 97 Setup window re-appears.

  5. The ODBC Data Source Administrator window should re-appear and in the list of data sources displayed you should see the StudentDatabase data source that was just created. Click the Ok button to close the window and close the Control Panel window as well.

Now that our data source is set up we can test our application.

  1. Using the command window go to the %TOMCAT_HOME%\bin directory and start the Tomcat servlet engine by issuing the command startup. This opens a new window for the Tomcat servlet engine. To shutdown the engine we enter the command shutdown.

  2. Now, point your web browser at the home page for your application, by opening the following URL

      http://localhost:8080/student



Previous    Next