Installing Tomcat
Mục lục bài viết
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
- Go to the download page of
J2SE Version 1.4.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. - Download and run the EXE installation
program. - You will need to accept the
license agreement again. - Use the suggested directory
for installing java (C:\j2sdk1.4.2_01). - 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.
- Open the control panel
under the start menu. - Double-click on System.
- Click on the Advanced tab.
- Click on the Environment
Variables button. - Under System Variables,
click on the New button. - For variable name, type:
JAVA_HOME
- For variable value, type:
C:\j2sdk1.4.2_01
- Continue to click OK to
exit the dialog windows.
Installing Tomcat
After setting the JAVA_HOME environment variable, you can install tomcat.
- Go to the Tomcat Web page.
- Click on Binaries under the
Download label on the left side of the page. - Scroll down until you see
Tomcat 4.1.x. (x will be some number greater than 10). - Click on the link ending
with exe (e.g. 4.1.27 exe). - Download and run the exe
file. - I suggest you install
Tomcat at c:\tomcat4 - Use the default settings
and provide a password that you will remember. - now assume that your tomcat
are installed at c:\tomcat4
Running Tomcat
Here are the steps to see if Tomcat has been successfully installed
- 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”. - Open a Web browser and type
in the following URL:
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
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.
-
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 theCLASSPATH
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.
- Goto the following location
on your computer:C:\Tomcat 4\webapps
- Create a directory “webdir”
under C: \Tomca4\webapps, which will be your home directory for
your assignment. - Crate a directory “WEB-INF”
under C: \Tomcat4\webapps \webdir - Crate a directory “classes”
under C: \Tomcat4\webapps\webdir\WEB-INF\, which will hold your
servlet class files.
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
- Open a “Command Prompt”
windows, and goto c:\ Tomcat4\webapps\webdir\WEB-INF\classes\ - try “javac HelloWorld.java”
- you will get a
HelloWorld.class - Stop and restart Tomcat.
- You can access the first
example with the following URL:
http://localhost:8080/webdir/servlet/HelloWorld
You can work at your own project based on webdir
directory, and use some IDEs for programming, such as Jcreator or
EditPlus.
Note: this note is copied partly from http://facweb.cs.depaul.edu/cmiller/ect433/tomcatInstall.html
From someone who want to how to config tomcat, please refer
to:
http://www.coreservlets.com/Apache-Tomcat-Tutorial/
<![if !supportEmptyParas]> <![endif]>
<![if !supportEmptyParas]> <![endif]>