Using
Tomcat
Tomcat contains the class
libraries, documentation, and run-time support that you will need to create and
test servlets. At the time of this writing, several versions of Tomcat are
available. The instructions that follow use 7.0.47. You can download Tomcat
from tomcat.apache.org. You should
choose a version appropriate to your environment.
The examples in this chapter
assume a 64-bit Windows environment. Assuming that a 64-bit version of Tomcat
7.0.47 was unpacked from the root directly, the default location is
C:\apache-tomcat-7.0.47-windows-x64\apache-tomcat-7.0.47\
This is the location assumed
by the examples in this book. If you load Tomcat in a different location (or
use a different version of Tomcat), you will need to make appropriate changes
to the examples. You may need to set the environmental variable JAVA_HOME to the top-level directory in
which the Java Development Kit is installed.
Once installed, you start
Tomcat by selecting startup.bat from
the bin directly under the apache-tomcat-7.0.47 directory. To stop
Tomcat, execute shutdown.bat, also
in the bin directory.
The classes and interfaces
needed to build servlets are contained in servlet-api.jar,
which is in the following directory:
C:\apache-tomcat-7.0.47-windows-x64\apache-tomcat-7.0.47\lib
To make servlet-api.jar accessible, update your CLASSPATH environment variable so that it includes
C:\apache-tomcat-7.0.47-windows-x64\apache-tomcat-7.0.47\lib\servlet-api.jar
Alternatively, you can
specify this file when you compile the servlets. For example, the following command
compiles the first servlet example:
javac HelloServlet.java -classpath
"C:\apache-tomcat-7.0.47-windows-x64\apache-tomcat-7.0.47\lib\servlet-api.jar"
Once you have compiled a
servlet, you must enable Tomcat to find it. For our purposes, this means
putting it into a directory under Tomcat’s webapps
directory and entering its name into a web.xml
file. To keep things simple, the examples in this chapter use the directory and
web.xml file that Tomcat supplies
for its own example servlets. This way, you won’t have to create any files or
directories just to experiment with the sample servlets. Here is the procedure
that you will follow.
First, copy the servlet’s
class file into the following directory:
C:\apache-tomcat-7.0.47-windows-x64\apache-tomcat-7.0.47\webapps\
examples\WEB-INF\classes
Next, add the servlet’s name
and mapping to the web.xml file in
the following directory:
C:\apache-tomcat-7.0.47-windows-x64\apache-tomcat-7.0.47\webapps\
examples\WEB-INF
For instance, assuming the
first example, called HelloServlet,
you will add the following lines in the section that defines the servlets:
<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>HelloServlet</servlet-class>
</servlet>
Next, you will add the
following lines to the section that defines the servlet mappings:
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/servlet/HelloServlet</url-pattern>
</servlet-mapping>
Follow this same general
procedure for all of the examples.
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.