Programming a Web Application - The
Direct Approach
It is possible to create a Web application without using any
external tool, except for a text editor (e.g. Emacs) and a Java
compiler (e.g. javac).
To create a Web application, follow these instructions:
- After setting up a base directory
for Tomcat, create a Web-application directory under $CATALINA_BASE/webapps/. For example,
$CATALINA_BASE/webapps/myApp/.
- Put Web pages anywhere under the application's base
directory, except for the directory WEB-INF. The relative URL of a WEB page is
exactly its (Unix-style) path relative to $CATALINA_BASE/webapps/. For example, the file
$CATALINA_BASE/webapps/myApp/a.html is
accessible by the URL http://localhost:8090/myApp/a.html,
assuming that Tomcat runs on localhost on port 8090. As another example, the file $CATALINA_BASE/webapps/myApp/a/b/c.jsp
is accessible by the URL http://localhost:8090/myApp/a/b/c.jsp.
- Put your Java source files under the directory WEB-INF/src/.
- Put your compiled Java classes under the directory WEB-INF/classes/.
- Put JAR files that contain Java classes under the
directory WEB-INF/lib/.
- If you are using Servlets, write down the Servlets
declaration and mapping (as thought in class) in WEB-INF/web.xml.
To access your application, you will have to restart Tomcat. You will also have to
restart Tomcat to reflect changes you make in your Java class
files and in the file WEB-INF/web.xml.
An Example
In the following example, we create a Web application named
dbinfo. This application shows a user
a list of her tables in her Oracle database (used by our
course). For this application, we use a Web page and a Servlet
(click to view ShowTables.java, tables.html and web.xml).
The following image shows the commands executed from the
Unix shell in our school. We use Emacs to edit our files.
After (re)starting Tomcat, the
application is accessible using Mozilla:

Notes
- Classes under WEB-INF/classes/ should follow the classpath rules when using packages. For example, if the class MyClass is located in the package myPackage then the file
MyClass.class should be placed under
WEB-INF/classes/myPackage/.
It is conventional to follow these rules for your Java source files, under the directory src.
- Note that the suitable Oracle driver is accessible from the Servlet. This is due to the fact that the directory $CATALINA_BASE/shared/lib/ is a soft link to
~dbi/tomcat/shared/lib/ and the latter directory contains the suitable JAR file.