Prerequisite
Prerequisite
Prerequisite
Verify Installation and setting of Web Container / Web Server / Tomcat and prepare an Installation
report, which contains setting of class path. Server port, starting and shutting down of server etc.
-----------------------------------------------------------------------------------------------------------
Installing Tomcat
This is a brief "how-to" for installing Tomcat on a Windows PC.
Installing Java
Tomcat requires java in order to run. If your computer already has java installed, you can probably
skip this step. However, make sure you have a recent version of java. Here I provide instructions for
installing version 1.4.2 of the Java 2 Platform, Standard Edition (J2SE).
Steps for installing java
1. Go to the download page of J2SE Version 1.4.2.
2. Select the version for Windows and click through the license acceptance. After two pages, you will
be able to download the EXE file for installing java on windows. Look for the SDK version.
3. Download and run the EXE installation program.
4. You will need to accept the license agreement again.
5. Use the suggested directory for installing java (C:\j2sdk1.4.2_01).
6. You may use the remaining default settings for the installation.
Setting the Java Environment Variable
Tomcat will need to know where you have installed java. To do this, you will need to set the
environment variable JAVA_HOME to C:\j2sdk1.4.2_01 (where you installed java).
Here are the steps for setting the environment variable on my computer (Windows XP Professional).
The steps will probably be similar for other Windows computers.
1. Open the control panel under the start menu.
2. Double-click on System.
3. Click on the Advanced tab.
4. Click on the Environment Variables button.
5. Under System Variables, click on the New button.
6. For variable name, type: JAVA_HOME
7. For variable value, type: C:\j2sdk1.4.2_01
8. Continue to click OK to exit the dialog windows.
Installing Tomcat
After setting the JAVA_HOME environment variable, you can install tomcat.
1. Go to the Tomcat Web page.
2. Click on Binaries under the Download label on the left side of the page.
3. Scroll down until you see Tomcat 4.1.x. (x will be some number greater than 10).
4. Click on the link ending with exe (e.g. 4.1.27 exe).
5. Download and run the exe file.
6. I suggest you install Tomcat at c:\tomcat4
7. Use the default settings and provide a password that you will remember.
8. now assume that your tomcat are installed at c:\tomcat4
Running Tomcat
Here are the steps to see if Tomcat has been successfully installed
1. Start Tomcat by finding its start program in the Programs Menu (located in the Start menu). Look
under Apache Tomcat 4.1 and select "Start Tomcat".
2. Open a Web browser and type in the following URL:
o http://localhost:8080/
At this point, you should see the Tomcat home page, which is provided by the Tomcat Web server
running on your computer. Note: if your computer has an internet name or an IP number, you may
access your Tomcat server anywhere on the internet by substituting localhost with the full name or
IP number.
To shut down your server and remove the Console window, select "Stop Tomcat" in the same menu
of where you selected "Stop Tomcat".
. Set Your CLASSPATH
Since servlets and JSP are not part of the Java 2 platform, standard edition, you have to identify the
servlet classes to the compiler. The server already knows about the servlet classes, but the compiler
(i.e., javac) you use for development probably doesn't. So, if you don't set your CLASSPATH,
attempts to compile servlets, tag libraries, or other classes that use the servlet and JSP APIs will fail
with error messages about unknown classes. Here are the standard Tomcat locations:
Tomcat 4: c:\tomcat4\common\lib\servlet.jar
in addition to the servlet JAR file, you also need to put your development directory in the
CLASSPATH. Although this is not necessary for simple packageless servlets, once you gain experience
you will almost certainly use packages. Compiling a file that is in a package and that uses another
class in the same package requires the CLASSPATH to include the directory that is at the top of the
package hierarchy. In this case, that's the development directory I just discussed. Forgetting this
setting is perhaps the most common mistake made by beginning servlet programmers!
Finally, you should include "." (the current directory) in the CLASSPATH. Otherwise, you will only be
able to compile packageless classes that are in the top-level development directory.
Here are two representative methods of setting the CLASSPATH. They assume that your
development directory is C:\Servlets+JSP. Replace install_dir with the actual base installation
location of the server. Also, be sure to use the appropriate case for the filenames, and enclose your
pathnames in double quotes if they contain spaces.
Windows 98/Me. Use the autoexec.bat file.
o Tomcat 4
Sample code: (Note that this all goes on one line with no spaces--it is broken here only for
readability.)
set CLASSPATH=.; C: \Tomcat4\common\lib\servlet.jar
Sample file to download and modify: autoexec.bat
Note that these examples represent only one approach for setting the CLASSPATH. Many Java
integrated development environments have a global or project-specific setting that accomplishes
the same result. But these settings are totally IDE-specific and won't be discussed here. Another
alternative is to make a script whereby -classpath ... is automatically appended onto calls to javac.
Windows NT/2000/XP. You could use the autoexec.bat file as above, but a more common
approach is to use system settings. On WinXP, go to the Start menu and select Control Panel, then
System, then the Advanced tab, then the Environment Variables button. On Win2K/WinNT, go to the
Start menu and select Settings, then Control Panel, then System, then Environment. Either way,
enter the CLASSPATH value from the previous bullet.
Enable the Invoker Servlet
The invoker servlet lets you run servlets without first making changes to your Web application's
deployment descriptor (i.e., the WEB-INF/web.xml file). Instead, you just drop your servlet into WEB-
INF/classes and use the URL http://host/servlet/ServletName (or
http://host/webAppName/servlet/ServletName once you start using your own Web applications).
The invoker servlet is extremely convenient when you are learning and even when you are doing
your initial development. To enable the invoker servlet, uncomment the following servlet-mapping
element in c:\tomcat4\conf\web.xml. Also, do not confuse this Apache Tomcat-specific web.xml file
with the standard one that goes in the WEB-INF directory of each Web application. Finally,
remember to make a backup copy of the original version of this file before you make the changes.
<servlet-mapping>
<servlet-name>invoker</servlet-name>
<url-pattern>/servlet/*</url-pattern>
</servlet-mapping>
Create a simple web application
Here are the steps for running the class examples discussed on the first day.
1. Goto the following location on your computer: C:\Tomcat 4\webapps
o Create a directory webdir under C: \Tomca4\webapps, which will be your home directory for
your assignment.
o Crate a directory WEB-INF under C: \Tomcat4\webapps \webdir
o Crate a directory classes under C: \Tomcat4\webapps\webdir\WEB-INF\, which will hold your
servlet class files.
2. Goto http://127.0.0.1:8080/examples/servlets/helloworld.html, copy the java code and paste it
into a blank file in your editor (such as notepate), and save it as
c:\ Tomcat4\webapps\webdir\WEB-INF\classes\HelloWorld.java
3. Open a Command Prompt windows, and goto c:\ Tomcat4\webapps\webdir\WEB-INF\classes\
4. try javac HelloWorld.java
5. you will get a HelloWorld.class
6. Stop and restart Tomcat.
7. You can access the first example with the following URL:
o http://localhost:8080/webdir/servlet/HelloWorld
8. You can work at your own project based on webdir directory, and use some IDEs for
programming, such as Jcreator or EditPlus.