J2EE Course - Creating a Tomcat Web Application

This free online course will cover installCation and configuration of Tomcat, and creation of a basic J2EE web application. You'll learn how to configure a default HTML welcome page by understanding the web.xml deployment descriptor, and will learn how to set the web server port number by understanding the server.xml deployment descriptor.

Course Prep:

  1. Read Chapter 1 in the Core Servlets and JavaServer Pages textbook.

  2. View the J2EE_Web_Applications Powerpoint lecture (also available in pdf format here).
  3. Install Java.

    Tomcat requires a Java J2SE SDK (standard edition software development kit, also referred to as a JDK) to be installed on your system.  It will not work with only a JRE (Java runtime edition) so be sure to install the Java 1.4.2 or greater SDK before installing Tomcat. Here's a link to download the Java J2SE SDK 1.4.2.

Course Exercises:

Download and install Tomcat 5.0.28 (select the Windows Executable link, which is about 10 MB).  The default installation folder is C:\Program Files\Apache Software Foundation\Tomcat 5.0 but I highly recommend changing this to something shorter that does not have spaces in the path, such as C:\Java\Tomcat5.0.28.  This comes in handy later when Java classpath settings need to be managed from the command line.

  • Specify a password for the Administrator Login during installation:

    Tomcat Password

  • Ensure that the default Java Virtual Machine points to the Java SDK on your system, instead of the JRE.  Otherwise Tomcat will not be able to compile JSP pages, which we'll learn about in a future lesson.
    Tomcat Java SDK

Download and install the Sysdeo plugin for Eclipse (installation instructions are located at the bottom of the Sysdeo download web page).

UPDATE (Jan 14, 2007): The sysdeo website is down (dating as far back as June 2006 from what I can see from the google trail of discussions about it). It's still the most highly recommended plugin for Tomcat and Eclipse development, and I happen to have a copy of it that you can download: tomcatPluginV31.zip. Unzip it into your Eclipse plugins folder, and the installation instructions are in readme.html inside the zip file.

  • Once the plugin has been installed and you have restarted Eclipse, you'll see 3 new toolbar buttons in Eclipse for stopping, starting, and restarting Tomcat:

    Tomcat Toolbar Buttons

  • Configure Tomcat in Eclipse by selecting the Window --> Preferences menu, and clicking the Tomcat link on the bottom left of the Preferences dialog.  Select Version 5.x and click the browse button to locate your Tomcat installation folder.

    Tomcat Eclipse Preferences

  • Next click the Advanced link and identify the Tomcat base (same as Tomcat home):

    Tomcat Advanced Settings

  • Lastly, click the JVM Settings link and ensure that a Java SDK is selected as the JRE (and not a JRE).  This is a very important step, and the preferences dialog is a little misleading about what is required.  It really should say "Java SDK" instead of "JRE".

    Tomcat Eclipse JVM

Verify the Tomcat plug-in installation by clicking the Start Tomcat tool button, or by selecting Tomcat -> Start Tomcat from the main menu.

Once Tomcat is running, enter the URL http://localhost:8080 in a web browser and verify that you can see the Tomcat home page.

Creating a Web Application

Open Eclipse and create a Tomcat Project in Eclipse called assign02.

  • Select the File --> New --> Project menu, and then select the Tomcat Project option.

    Tomcat Project

  • Enter a project name and location, and click Finish to accept the remaining default settings.  Your Tomcat project contents should contain the following folders:

    Tomcat Project Contents

Once your Tomcat project has been created, you can view/modify your project settings by right-clicking on your project (assign02) and selecting "Properties" from the short cut menu.

The general Tomcat project settings should not need to be modified, but select the Export to WAR settings tab and specify the location and file name for the "WAR file for export".  Also make sure the "Export .java files" option is selected if you want your .java source files will get bundled with the .war file.

Tomcat Project Settings

Add a web.xml deployment descriptor to the WEB-INF folder by selecting the File --> New --> File menu (hint: you can also copy the web.xml file from the Tomcat examples web application to use as a starting point, but be sure to remove everything between the opening <webapp> and closing </webapp> tags).  We will add more content to this file in later courses, but it is mandatory to at least have a web.xml file with <webapp> tags in order to successfully run a web application in Tomcat.

Tomcat deployment descriptor

Build an HTML page named YourName.html (with YourName replaced by your actual name), located in the root assign02 folder, that displays the following information:

New Web Page

Your Tomcat project should now contain an html page in the assign02 root folder, and a web.xml page in the WEB-INF folder as shown below (click image for full-screen view):

Tomcat Project Files

Exercises

Here are some tasks that will familiarize you with Tomcat and help you learn how to create a J2EE web application:

  • Configure your web application so that your HTML page is the default welcome page (requires editing web.xml in Eclipse).

  • Configure Tomcat so that it runs on port 80 instead of the default 8080 (requires editing server.xml).  IMPORTANT: any time server.xml or web.xml are modified, Tomcat must be restarted before it will pick up these changes.

  • Run your web application from a web browser by pointing it to http://localhost/YourWebAppName/.
  • Package your web application into a WAR file using the "Tomcat project" --> "Export to the WAR file sets in project properties" menu.  You can also package it from the command line using the jar cvf YourWebAppName.war * command in the root project folder.

As a final test before patting yourself on the back for a job well done - stop Tomcat and close Eclipse.  Deploy the web application by copying the .war file into Tomcat's webapps folder and starting Tomcat stand-alone (select the Start --> Programs --> Apache Tomcat 5.0 --> Configure Tomcat menu, and click the Start button).  Run your web application from a web browser by pointing it to http://localhost/YourWebAppName/

Start Tomcat Stand-Alone

Congratulations - you've successfully created a J2EE web application using Tomcat, a J2EE-compliant web server used by thousands of companies worldwide!

Check your work: assign02Solution.war


SystemSuite 7 Professional Box - Box

SystemSuite 7 Professional

Complete Protection for Your PC! Get the only solution that offers complete PC maintenance, diagnostics, and protection for up to 3 PCs! VCOM SystemSuite Professional is a comprehensive collection of essential PC utilities designed to maintain and protect your PC to keep it running just like the day you bought it. With VCOM SystemSuite Professional you can improve system performance, protect your system against viruses and from hackers, diagnose computer problems, recover lost data, protect against email spam, and much, much more.

Special Offer: Save 50% with Coupon Code AFSS50

Debugging in Eclipse Needed


Following this tutorial I created I successfully created war file and deployed. Can any help me out for debugging in eclipse?

What do I enter in the browser address line?


When I try and run my application from my web browser, I get the error message "The requested source is not available". I'm not exactly sure what to enter in the address line of the web browser, but I'm entering the entire path name.... http://localhost/assign02/WEB-INF/web.xml

Am I entering the wrong information on the address line of my browser?

Enter the name of your war file minus the .war extension


The name of your war file is also the name of your web application (meaning what you'd enter after the localhost/ part of the url), but you don't need the WEB-INF/web.xml part. For a war file named assign02.war, just enter http://localhost/assign02 in your browser address line.

Nothing under the WEB-INF folder is directly accessible from a browser, which makes a nice and secure place to store sensitive files. You can still link to those files internally from within your Java servlets or JSP pages, but users will never be able to directly link to them by typing a URL containing WEB-INF.

Also, if you rename the .war file to something else like mytestapp.war and then redeploy it to Tomcat, you'll have a new web app that can be accessed using http://localhost/mytestapp. To deploy it, just drop it into Tomcat's webapps folder (you may also need to restart Tomcat).

Hope this helps.

Is port 80 some kind of default?


How come "http://localhost" automatically goes to port 80?
Is this some kind of default for Apache or for all web servers?
Is it possible to change this configuration? If so, how?

Port 80 is the standard HTTP port


Port 80 is the standard HTTP port for all web servers and browsers, just
as 443 is the standard for HTTPS (for secure communications). So if no
port is specified, then 80 is assumed.

You can change the port in server.xml which is located under Tomcat's
conf folder. Look for a Connector element with "Define a non-SSL Coyote
HTTP/1.1 Connector on the port specified during installation" as the
comment.

You'll see port="80" or port="8080" (depending on how you
installed Tomcat). You just have to replace the 80 with whatever other port
number you desire, and then restart Tomcat to pick up the change.