Apache Tomcat Server
Apache Tomcat Server
Apache Tomcat Server
The servlet container (Catalina) is based on a completely new architecture and has been developed from the ground up for
flexibility and performance. Another popular servlet container is JRun from Allaire Corporation. JRun is available in three
editions: Developer, Professional, and Enterprise. The Developer edition is free but not licensed for deployment.
Tomcat by itself is a web server. This means that you can use Tomcat to service HTTP requests for servlets, as well as static
files (HTML, image files, and so on). In practice, however, since it is faster for non-servlet, non-JSP requests, Tomcat normally
is used as a module with another more robust web server, such as Apache web server or Microsoft Internet Information
Server. Only requests for servlets or JSP pages are passed to Tomcat.
To write a servlet, you need at least version 1.2 of the Java Development Kit. The reference implementation for both servlets
and JSP are not included in J2SE, but they are included in Tomcat. Tomcat is written purely in Java.
Pre-requisite: jdk has to be installed on the system with all the paths set.
Installing TOMCAT
1. Download the zip file from http://tomcat.apache.org/download
2. Unzip the file and run the exe
3. Install the TOMCAT server to C://tomcat
4. Set 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, set the classpath.
a. Right-click on My Computer, goto the advanced tab, click on the environment variables.
b. Under user variables, create a new variable with the name as CLASSPATH and value as C:\Tomcat
5.5\common\lib\servlet-api.jar
5. Set JAVA_HOME. Set this environment variable to tell Tomcat where to find Java . Set this environment variable to
point at the top-level of Java installation directory.
a. Right-click on My Computer, goto the advanced tab, click on the environment variables.
b. Under user variables, create a new variable with the name as JAVA_HOME and value as C:\Program
Files\Java\jdk1.6.0_21
6. Change the port to 80. Making this change lets you use URLs of the form http://localhost/blah instead of
http://localhost:8080/blah
a. To change the port, edit install_dir/conf/server.xml and change the port attribute of the Connector element
from 8080 to 80, yielding a result similar to that below.
<Connector port="80" protocol="HTTP/1.1"
... >
7. Turn on servlet reloading. Tell Tomcat to check the modification dates of the class files of requested servlets, and
reload ones that have changed since they were loaded into the server's memory. The privileged entry is really to
support the invoker servlet, it can be omitted if the invoker servlet is not used.
a. To turn on servlet reloading, edit Edit install_dir/conf/context.xml and change
<Context>
To
<Context reloadable="true" privileged="true">
8. 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 save your servlet (Eclipse) or
drop your servlet into WEB-INF/classes (manual deployment) and use the URL http://host/servlet/ServletName (or
http://host/webAppName/servlet/packageName.ServletName once you start using your own Web applications and
packages).
a. uncomment the following servlet and servlet-mapping elements in install_dir/conf/web.xml
<servlet>
<servlet-name>invoker</servlet-name>
<servlet-class>
org.apache.catalina.servlets.InvokerServlet
</servlet-class>
...
</servlet>
...
<servlet-mapping>
<servlet-name>invoker</servlet-name>
<url-pattern>/servlet/*</url-pattern>
</servlet-mapping>
To test whether Tomcat was installed properly, open your browser on the same computer you used to
install Tomcat and direct it to the following URL:
http://localhost:8080
For a successful tomcat installation, the browser will open page similar to this:
Tomcat Directories
When you install Tomcat, the installation program creates a number of directories under %CATALINA_HOME% [Tomcat
installation directory]. Understanding the function of each subdirectory is important to configuring Tomcat and deploying
your servlet and JSP applications.
Directory Description
bin Startup and shutdown scripts and other files.
classes Unpacked classes global to web applications.
conf Configuration files including server.xml (Tomcat's main configuration file) and the global web.xml
(deployment descriptor) file.
server Tomcat's archive files.
lib Common classes in .jar files.
logs Tomcat's log files.
common Common classes for both Catalina and web applications.
webapps Servlet and JSP applications.
work Resulting servlets from the JSP pages translation
1. Create a directory called myApp under the webapps directory. The directory name is important because this also
appears in the URL to your servlet.
2. Create the and WEB-INF directories under myApp, and create a directory named classes under WEB-INF. The
directory classes under WEB-INF is for your Java classes. If you have HTML files, put them directly under the myApp
directory. You may also want to create a directory called images under myApp for all your image files.
3. Write the servlet source code
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
To create the deployment descriptor, you now need to create a web.xml file and place it under the WEB-INF directory
under myApp. The web.xml for this example application must have the following content.
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>Testing</servlet-name>
<servlet-class>TestingServlet</servlet-class>
</servlet>
</web-app>
The web.xml file has one element—web-app. You should write all your servlets under <web-app>. For each servlet,
you have a <servlet> element and you need the <servlet-name> and <servlet-class> elements. The <servlet-name> is
the name for your servlet, by which it is known Tomcat. The <servlet-class> is the compiled file of your servlet
without the .class extension.
Having more than one servlet in an application is very common. For every servlet, you need a <servlet> element in
the web.xml file. For example, the following shows you how web.xml looks if you add another servlet called Login:
7. Run tomcat
If you run the web browser from the same computer as Tomcat, you can replace the domain-name part with
"localhost". In that case, the URL for your servlet is
http://localhost:8080/myApp/servlet/Testing
In the deployment descriptor you wrote in Step 4, you actually mapped the servlet class file called TestingServlet with
the name "Testing," so that your servlet can be called by specifying its class file (TestingServlet) or its name (Testing).
Without a deployment descriptor, you must call the servlet by specifying its class name; that is, TestingServlet. This
means that if you did not write a deployment descriptor in Step 4, you need to use the following URL to call your
servlet:
http://localhost:8080/myApp/servlet/TestingServlet