Read an Excerpt
Chapter 1: Starting Web ProgrammingJava has enjoyed a tremendous growth in use among web developers over the past few years to become one of the Web's pre-eminent development platforms. In fact, Java has become such a major player so rapidly that many in the industry aren't really sure exactly what Java does or how it works; they just know they need to learn it fast. If you fall into this category, keep reading - you're about to get some answers.
The goal of this chapter is to get all the tools installed on your machine that you'll need to develop Java web applications. We'll also go through the process of writing a simple Java web application as well.
At the end of the chapter, I've also included some optional material that explains a little about how the web itself works, and Java's role in the web.
Installing the SoftwareThe first step to writing Java web applications is installing the necessary software; let's get to it!
Installing JavaThe first thing you need is the Java software development kit:
- Download Java. Point your web browser to
http://java.sun.com/j2se/. You're looking for the latest version of the "Java 2 SDK, Standard Edition", disregarding any beta versions that may be available. As of this writing, the latest version is 1.3.
- Install Java. After your browser finishes sucking down Java, execute the file that you've downloaded. For version 1.3 on Windows the installer's default folder (or directory) for the Java is
C:\jdk1.3, but feel free to install it wherever you want (however, substitute your custom path for
C:\jdk1.3wherever you see it in this book).
Note that in these instructions and throughout the rest of the chapter I use the Windows notation for paths. To make these into Unix paths, substitute forward slashes (/) instead of back slashes (\) and remove the drive prefix at the beginning of the path - for example,
Installing TomcatYou've now got Java, but for creating web applications, we'll need one more tool: Tomcat. Tomcat is what's known as a servlet container. In the Java world, a servlet container is responsible for receiving web requests and passing them to Java web applications. (For the curious, see the history lesson at the end of this chapter to learn exactly why it's called a servlet container.)
- Download Tomcat. Tomcat's home is
http://jakarta.apache.org/tomcat/. Surf on over there and grab the latest version of Tomcat 4.x (as of this writing, it's Tomcat 4.0 beta 5).
- Extract Tomcat. You may extract the Tomcat archive into any location you desire; we suggest
that you extract it into
C:\, which will place the files in a folder called something like
C:\jakarta-tomcat-4.0-b5. (The exact folder will depend on which version of Tomcat 4.x you downloaded; this is the correct folder for beta 5.)
Configuring Your EnvironmentThat's the software installed; we're nearly done now. However, you do need to set up your computer's environment so that Tomcat knows where it can find the various bits of software we need such as the Java software development kit.
To do this you need to set or modify several environment variables. We'll look at how to do this in a moment; first, let's see which variables we need to set:
JAVA_HOMEtells Tomcat where it can find the Java software development kit; you must set it to the folder where you installed this, such as:
CATALINA_HOMEneeds to point to the folder where you installed Tomcat, such as:
CLASSPATHis used to help the Java software find extra bits of program code; it contains a list of places to look, separated by semicolons (
;) on Windows or colons (
:) on Unix. We need to include a file called
servlet.jar, which can be found in Tomcat's
common\libfolder this contains various bits of useful code that we'll need in later chapters. So, if you're on Windows and have been following our recommendations so far you've set
PATHis used by your operating system to find programs you want to run; like
CLASSPATH, it consists of a list of places to look separated by semicolons (on Windows) or colons (on Unix). You'll find that
PATHis already set but you need to add the
C:\jdk1.3\binfolder to the list. For example, on Windows your
PATHmight currently be:
which would need to be changed to:
Setting Environment Variables On Windows 2000Let's start by seeing how to do so on Windows 2000:
- Go into the Control Panel and open up the System control panel application.
- You should have a window entitled System Properties on your screen; select the Advanced tab, and click on the Environment Variables button:
- A new window named Environment Variables should have opened. Click on the New button in the System Variables section.
- The New System Variable window should now be on your screen. Enter JAVA_HOME for as the name and the path to your JDK (such as
c:\jdk1.3) for the value, as shown below...