UserPreferences

Software/Tomcat/HelloWorld


See also:

  1. Notes
    1. Accessing Tomcat
    2. File Structure of a Project
    3. Hello World (.java version)
    4. Hello World (.jsp version)

1. Notes

1.1. Accessing Tomcat

Web apps will be stored in the following directory:

Try testing that Tomcat is started by accessing these webpages. You should expect to see a page that says "Tomcat" as a result:

1.2. File Structure of a Project

1.3. Hello World (.java version)

http://www.keyboardsamurais.de/mt/archives/000053.html

We are going to make a web application that should be accessible by the following URL:

We create the initial structure for the project:

Now we can create a new class named HelloServlet in the directory WEB-INF/src. Now we can copy and paste following code into this class.

Now compile the Java class:

Unfortunately we are not yet finished. We still need to create the web.xml descriptor, which contains certain elements of configuration specific to our application and the server behaviour. So we create the file web.xml in the directory WEB-INF (Note: not in the directory WEB-INF/src !!!). For our simple application, the following parameters should be fine.

What follows now is a little explanation of what we just copied and pasted into web.xml. The Doctype is an xml specific item that tells the xml parser where to look for the dtd consistency rules for our xml document. The dtd ensures that no wrong parameter combinations are entered. The main tag <web-app> contains all preferences for our servlet. The <servlet> tag basically contains the name (<servlet-name>) that will be used throughout the xml document to reference our servlet and its linked class (<servlet-class>) . The tag <servlet-mapping> is responsible for telling the server, to what document name in the URL our application should respond to. Note, that the correct order of these tags has to be retained to form a valid web.xml descriptor. If we followed all steps correctly, we should now be able to fire up Tomcat again.

Finally, we need to tell Tomcat where to find our Hello World project:

1.4. Hello World (.jsp version)