CH APB
CH APB
General Exercise
Instructions
772 Appendix B: General Exercise Instructions
T he exercises in the book almost all involve building and deploying web applications. This
appendix tells you what you will need to complete the exercises and covers
Follow Sun’s download and installation instructions for your platform (this is a very
straightforward process).
Creating a Directory Structure for Web Applications 773
1. Create a top-level directory for all your exercise work, for example,
C:\WebCert.
2. Within this, create a directory that represents the chapter and exercise
number within the chapter as you come to it. The convention I follow
throughout (in the solution files as well) is exCCEE, where “ex” is constant,
774 Appendix B: General Exercise Instructions
“CC” is the number of the chapter (01 for Chapter 1, 02 for Chapter 2, etc.),
and “EE” is the number of the exercise (01 for the first exercise in a chapter,
02 for the second, and so on). So the first exercise in the sixth chapter has
a dedicated directory called C:\WebCert\ex0601. This is equivalent to the
context directory in the deployed web application. You can use the same
naming convention for the context directory as I do —but do something
different if you want to deploy your own code and the solution code side by
side on the Tomcat server.
3. Within this, create a WEB-INF subdirectory (e.g., C:\WebCert\ex0601\
WEB-INF).
4. With the WEB-INF subdirectory, create three subdirectories: classes, lib, and
src: C:\WebCert\ex0601\WEB-INF\classes, C:\WebCert\ex0601\lib, and
C:\WebCert\ex0601\src in our example. The following illustration shows
the complete example structure.
C:\WebCert
\ex0601
\WEB-INF
/classes
/lib
/src
You’ll place Java source for servlets and any supporting classes in the WEB-
INF\src subdirectory for a given exercise’s context directory. If you are using
package names, you’ll need to create the relevant subdirectories under WEB-INF\
src to support these. My solution files follow this package structure: webcert.chCC
.exCCEE, where CC is the chapter number and EE is the exercise number within the
chapter (zero prefixed). Therefore, the last element of the package name is the same
as the context directory. You can use the same naming convention or use your own.
When I say “if you are using package names,” it’s not really an option in J2EE
1.4. The so-called “default package” is disallowed — and quite right, too. Whatever
Other Files 775
Java source you’re writing, there’s no excuse for not providing a package structure!
Although some JSP containers will let you get away with using the default package,
I suggest you get into good habits from the start. As a bonus for me telling you this,
note that the package name topic might come up in the exam!
cd C:\WebCert\examp0401\WEB-INF\src
Since most of what you will be doing relies on servlet technology, however,
you will have to ensure that the base J2EE servlet packages are available on your
class path. These will live with your Tomcat installation in <tomcat-installation-
directory>/common / lib/servlet-api.jar. For example, on my Windows machine, I
set the classpath as follows:
set classpath=.;C:\Java\jakarta-tomcat-5.0.27\common\lib\
servlet-api.jar
■ Switch to the src directory under WEB-INF for the context directory for the
exercise or lab.
■ Enter a compilation command in the form
By using -d ..\classes, the compiled class files will be placed in the classes
directory under WEB-INF.
Other Files
Any other files you need to create for the exercise should be placed in the web
application directory structure as indicated by the exercise instructions. For
example,
776 Appendix B: General Exercise Instructions
■ web.xml — the web application deployment descriptor —will live directly in the
WEB-INF directory.
■ JavaServer Pages will normally live in the context directory of your web
application directory structure (the one above WEB-INF, e.g., ex0601).
This creates an archive file called ex0601.war in C:\WebCert, which contains the
full contents of the C:\WebCert\ex0601 directory (including all the sub-directories
and files within them).
Second, your .war file is “un-jarred” (unzipped) into its own directory structure under
<tomcat installation directory>/webapps. The structure will mirror exactly the one
you built in your development area C:\WebCert.
At this point, your application is ready to run. Again by default, Tomcat listens
for your requests on port 8080 on the PC on which it is installed. Assuming you
are running your browser on the same PC, you can access resources in your web
application with a URL such as the following:
Therefore, if the context is ex0601 and the resource you want to access is called
lifecycle.jsp, then the URL would be
http://localhost:8080/ex0601/lifecycle.jsp
If you get an HTTP 404 (page not found) error in your browser, then check the
following:
■ You spelled everything correctly, including using the correct upper- and lowercase
letters.
■ You have an appropriate <servlet-mapping> in web.xml (see Chapter 2).
■ The application deployed correctly: There are no errors in the Tomcat server
console window.
■ The resource you’re after has actually been expanded into the directory.
An HTTP 500 error indicates a Java problem running your servlet or server
page — the error may show up in your browser window, or you may need to look for
errors in the Tomcat server console window.
Tomcat is smart about recognizing when a .war file has been updated. Therefore,
if you re-deploy a new .war to the webapps directory, overwriting one with an earlier
date, Tomcat will elegantly close the current version of the application and deploy
the new one.
In some of the earlier exercises, you bypass the steps of making and deploying
the WAR file. On Tomcat, you can get away with copying the working directory
structure for your application to the webapps directory. After a server restart, Tomcat
will recognize your new application. However, this is more of a workaround until
you are comfortable with WAR file creation —later exercises (from around Chap-
ter 3) expect you to make WAR files from your working directories and deploy them,
instead of copying your working directories directly to the Tomcat webapps directory.
778 Appendix B: General Exercise Instructions
Solution Code
The solution code for each of the exercises and the labs can be found on the CD.
There’s a .war file for almost every one (for the handful where this isn’t true, the
exercise is not directly code based). Deploying the solution code follows the same
pattern as for your own code: Place each .war file as needed in <tomcat-installation-
directory>/webapps. The .war will automatically expand and deploy. Look in the
directory structure (under WEB-INF/src) for the accompanying Java source code,
wherever appropriate.