Você está na página 1de 21

A Servlet is a Java class which conforms to the Java Servlet API, a protocol by

which a Java class may respond to http requests. Thus, a software developer
may use a servlet to add dynamic content to a Web server using the Java
platform. ...

a Java program, running in a Internet server, that generates web content


dynamically depending on the content of a request from a client

A java program that runs on a web server.

Stands for Java Server Page. It is a server side scripting language. Instead of
running on the viewer's computer, as with Java Script, it runs on the server
before the page reaches the viewer's computer.

Java Server Page (JSP) is a technology for controlling the content or appearance
of web pages through the use of servlets, small programs that are specified in
the web page and run on the Web server to modify the web page before it is sent
to the user who requested it.

As mentioned below -J2SE 6 JRE is

Java Platform, Standard Edition or Java SE is a widely used platform for programming in
the Java language. It is the Java Platform used to deploy portable applications for general use.
In practical terms, Java SE consists of a virtual machine, which must be used to run Java
programs, together with a set of libraries (or "packages") needed to allow the use of file
systems, networks, graphical interfaces, and so on, from within those programs.

Apache Tomcat Versions


Different versions of Apache Tomcat are available for different versions of the Servlet and JSP
specifications. The mapping between the specifications and the respective Apache Tomcat
versions is:

Servlet/JSP Spec Apache Tomcat Actual release revision Minimum Java Version
version
3.0/2.2 7.0.x 7.0.2 (beta) 1.6
2.5/2.1 6.0.x 6.0.29 1.5
2.4/2.0 5.5.x 5.5.31 1.4
2.3/1.2 4.1.x (archived) 4.1.40 (archived) 1.3
2.2/1.1 3.3.x (archived) 3.3.2 (archived) 1.1

The releases are described in more detail below to help you determine which one is right for
you. More details about each release can be found in the associated release notes.

Please note that although we offer downloads and documentation of older releases, such as
Apache Tomcat 4.x, we strongly encourage users to use the latest stable version of Apache
Tomcat whenever possible. We recognize that upgrading across major versions may not be a
trivial task, and some support is still offered on the mailing list for users of old versions.
However, because of the community-driven support approach, the older your version the less
people would be interested or able to support you.

Alpha / Beta / Stable


Almost every Tomcat release is initially released as an Alpha release. After a week or so of
testing a vote is held to gather views on the stability of the release. If a major issue is identified
during testing, then the vote may not take place and the release will remain as an Alpha
release.

The download pages will always show the latest stable release and any newer Alpha or Beta
release if one exists. Alpha and beta releases are always clearly marked on the download
pages.

Stability is a subjective judgement and you should always read carefully the release notes for
any version you intend to make use of. If you are an early adopter of a release, we would love
to hear your opinion about its stability as part of the vote: it takes place on the development
mailing list.

Alpha releases may contain large amounts of untested/missing functionality required by the
specification and/or significant bugs and are not expected to run stably for any length of time.

Beta releases may contain some untested functionality and/or a number of relatively minor
bugs. Beta releases are not expected to run stably.

Stable releases may contain a small number of relatively minor bugs. Stable releases are
intended for production use and are expected to run stably for extended periods of time.

Apache Tomcat 7.x


Apache Tomcat 7.x is the current focus of development. It builds upon the improvements
made in Tomcat 6.0.x and implements the Servlet 3.0, JSP 2.2 and EL 2.2 specifications. In
addition to that, it includes the following improvements:

• Web application memory leak detection and prevention


• Improved security for the Manager and Host Manager applications
• Generic CSRF protection
• Support for including external content directly in a web application

• Refactoring (connectors, lifecycle) and lots of internal code clean-up

Apache Tomcat 6.x


Apache Tomcat 6.x builds upon the improvements made in Tomcat 5.5.x and implements the
Servlet 2.5 and JSP 2.1 specifications. In addition to that, it includes the following
improvements:

• Memory usage optimizations


• Advanced IO capabilities

• Refactored clustering

Apache Tomcat 5.x


Apache Tomcat 5.5.x supports the same Servlet and JSP Specification versions as Apache
Tomcat 5.0.x. There are significant changes in many areas under the hood, resulting in
improved performance, stability, and total cost of ownership. Please refer to the Apache
Tomcat 5.5 Changelog for details.

Apache Tomcat 5.0.x improves on Apache Tomcat 4.1 in many ways, including:

• Performance optimizations and reduced garbage collection


• Refactored application deployer, with an optional standalone deployer
allowing validation and compilation of a web application before putting it in
production
• Complete server monitoring using JMX and the manager web
application
• Scalability and reliability enhancements
• Improved Taglibs handling, including advanced pooling and tag plugins
• Improved platform integration, with native Windows and Unix wrappers
• Embedding using JMX
• Enhanced Security Manager support
• Integrated session clustering

• Expanded documentation

Introduction

For administrators and web developers alike, there are some important bits of information you
should familiarize yourself with before starting out. This document serves as a brief
introduction to some of the concepts and terminology behind the Tomcat container. As well,
where to go when you need help.
Terminology

In the course of reading these documents, you'll run across a number of terms; some specific to
Tomcat, and others defined by the Servlet or JSP specifications.

• Context - In a nutshell, a Context is a web application.


• Term2 - This is it.

• Term3 - This is it!


Directories and Files

Throughout the docs, you'll notice there are numerous references to $CATALINA_HOME.
This represents the root of your Tomcat installation. When we say, "This information can be
found in your $CATALINA_HOME/README.txt file" we mean to look at the README.txt
file at the root of your Tomcat install. Optionally, Tomcat may be configured for multiple
instances by defining $CATALINA_BASE for each instance. If multiple instances are not
configured, $CATALINA_BASE is the same as $CATALINA_HOME.

These are some of the key tomcat directories:

• /bin - Startup, shutdown, and other scripts. The *.sh files (for Unix systems)
are functional duplicates of the *.bat files (for Windows systems). Since the
Win32 command-line lacks certain functionality, there are some additional
files in here.
• /conf - Configuration files and related DTDs. The most important file in here
is server.xml. It is the main configuration file for the container.
• /logs - Log files are here by default.

• /webapps - This is where your webapps go.

This site provides useful information regarding installation of Tomcat server. The steps by step of the installation are
shown properly. Every step will followed by a capture of screen that enable user to better understand what they are
doing currently and what they will be doing after. This will attract new user of Tomcat that are using Window platform to
further reading on this site before continuing into installation. In the subtitle 'Deployment of jsp or servlet in tomcat',
user will be provided with example of creating first servlet. This is helpful for new user to test the example given and get
knowledge from that before going in depth.

Installation of JDK Java

Download JDK or JRE from sun website


http://java.sun.com/javase/downloads/index.jsp
Sun provides different executable file for Windows and Linux operating system version. Sun provide JDK for Linux in
.bin or tar.gz format, and for Windows .exe format. After downloading file from sun, install JDK simple double click or
through command prompt .

Default installation directory for java in Windows is Program file à Java, and Linux /opt. Tomcat 5.5.17 version requires
at least java1.5 version.
When jdk is installed in default location, need to configuration the environment variable

Environment Variable Setting

Environment variable is next to set java home in system environment. This will help to know others application where is
java is installed and where java file have to compile. JAVA_HOME variable name needs to set in environment variable as
variable name and variable value as the java home directory c:\ jdk1.5.0.
Step to do this setting, go to my computer icon and right click and go to properties

My computer->right click->properties->
Advance tab->environment variables ->

User variables –> New


Click on new button and new pop up will open.
In variable value, enter your java path where java is installed in system.

Tomcat installation

Tomcat installation is of two types. Apache provides self installer for tomcat installation.This tomcat is in executable
binary file. Download,and install, self installer do all work automatically.Second is source code extractor. This tomcat
installation is also easy as self installer. Before tomcat installation, check all jvm, java, and environment path set
properly.

Apache tomcat is freely available from apache jakarta site


http://archive.apache.org/dist/tomcat/tomcat-5/
According to per requirement, tomcat is downloaded in form of binaries or source code.
We are using zipped pack for windows installation. Download zipped binaries code latest one from this site.Unzip this
code and c:\ drive and anywhere desired directory
After unzipped directory system will like that
This is means that tomcat is installed properly and ready to run web server. Open bin directory and double click on
startup.bat file. Startup.bat will open in dos command prompt
After starting tomcat, check tomcat in web browser by typing localhost:8080 in address bar
http://localhost:8080/

This page shows Tomcat is running properly in your system.

Setting up tomcat port

Apache Tomcat Configuration Tomcat use 8080 port instead of 80 port. User can easily change default port from
tomcat. Open c:/tomcat/config/server.xml folder in tomcat installed and open server.xml file.

<!-- Define a non-SSL HTTP/1.1 Connector on port 8080 -->


<Connector port="8080" maxHttpHeaderSize="8192"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
connectionTimeout="20000" disableUploadTimeout="true" />
<!-- Note : To disable connection timeouts, set connectionTimeout value
to 0 -->

In connector port change 8080 to 80, now save this save and shutdown.bat tomcat and startup.bat tomcat.This time
user need not to write 8080 port in http://localhost/

Servlet invoker

Servlet container of tomcat engine doesn’t start automatically. WEB-INF deployment descriptor needs to tell servlet
engine, where your servlet and mapping of servlet.This is is can be done without any setting through servlet invoker.
We just need to uncommented the code in web.xml in config folder.Servlet invoker automatically load servlet in context
and map it.
Open c:/tomcat/config/web.xml and edit this code as shown.
First uncommented invoker then servlet mapping

<!-- -->
<!-- debug Debugging detail level for messages logged -->
<!-- by this servlet. [0] -->

<servlet>
<servlet-name>invoker</servlet-name>
<servlet-class>
org.apache.catalina.servlets.InvokerServlet
</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>0</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>

also uncommented the servlet mapping

<!-- The mapping for the invoker servlet -->


<servlet-mapping>
<servlet-name>invoker</servlet-name>
<url-pattern>/servlet/*</url-pattern>
</servlet-mapping>

<!-- The mapping for the JSP servlet -->

If you are using tomcat 6, need to define privileges for context. In tomcat 5.5.x version no need to define privileges for
context. Tomcat 5.5.x take automatically.

Privileges setting can be done in tomcat/conf/context.xml file. Add this line

<Context reloadable="true" privileged="true">

Deployment of jsp or servlet in tomcat

All jsp and html page should be kept in C:\tomcat\webapps and create your own folder or keep jsp file under default
directory ROOT. We would like to create new folder in webapps as home and keep all files in this folder. In home folder
create WEB-INF folder and inside this also create two folder named as classes and lib and keep web.xml in WEB-INF.
In classes folder, all servlets and javaBeans should keep,Classes folder generally contains class file and
java file and when servlet engine starts, all java and class file loaded in tomcat through bootstrap.jar which is in bin
folder. Create a first servlet example see example

Deployment of servlet
Servlet deployment needs to map in deployment descriptor, if you have not enabled servlet invoker. Descriptor give us
more feature of deploying servlet with user defined URL context path.

All servlet needs to compile manually and copy to WEB-INF/classes folder.

Descriptor file is web.xml which keeps all information of servlet and taglib’s tld

Suppose we are deploying TestServlet servlet at default package.

WEB-INF/classes/TestServlet.class

Make a file
WEB-INF/web.xml
web.xml

<?xml version="1.0" encoding="UTF-8"?>


<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<servlet>
<servlet-name>TestServlet</servlet-name>
<servlet-class>TestServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/servlet/TestServlet</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
Apache 6 Installation And Configuration

These directions allow you to install and configure Apache Tomcat 6 for use as a
standalone Web server (for development) that supports servlets 2.5 and JSP 2.1.

You can to install Apache Tomcat 6 in one of two ways . Either download a
preconfigured version of the software from the Apache Software Foundation or,
if you want more control of its settings, you can manually install it.

For a preconfigured approach, you download Apache Tomcat 6 from site at


(where "x" represents the latest version_ from the Apache Software Foundation
site. In this case - which assumes you are using Microsoft Windows - you follow
these steps:

1.Unzip Tomcat. Unzip tomcat-6.0.x-preconfigured.zip into the top level of the C


drive. This should result in C:apache-tomcat-6.0.x. This version of Tomcat has
the following settings already in place. For details on customizing this
configuration, please see the detailed configuration guide.

- The port is changed from 8080 to 80. When you download Tomcat from the
Apache site, the port is 8080 in case you already have another server running on
port 80.

- Servlet reloading is enabled. This lets you deploy a modified servlet .class file
without restarting the server..

- When you download Tomcat from the Apache site, servlet reloading is disabled
for performance reasons. You might want it disabled on a server used for a
deployed application, but you definitely want it enabled during development.

- The invoker servlet is enabled. This lets you drop a servlet .class file into
webapps/appName/WEB-INF/classes/packageName and immediately run it
with a URL of the form. That is, the invoker servlet saves you from editing
web.xml to give a servlet-mapping to your servlet.

- When you download Tomcat 5.5 or a newer version from the Apache site, the
invoker servlet is disabled. You definitely want the invoker servlet disabled on a
server used for a deployed application, but having it enabled on your
development server is very convenient for quick testing.

- Tomcat monitors struts-config.xml and faces-config.xml and tracks tomcat


performance. Whenever either of these files changes, Tomcat reloads the Web
application. This saves you from restarting the server when you change these
files.

- If you do not use Struts or JSF, this change will not be beneficial to you. But it
does not hurt either way.

- Directory listings are turned on. If you type a URL ending in / and there is no
welcome file, Tomcat shows a directory listing.

- Directory listings were on by default in previous Tomcat versions. They are not
required but are convenient, especially during development.

2. Set JAVA_HOME. Set this environment variable to point at the top-level of your
Here are more details on using the preconfigured Tomcat version for direct
(manual) execution. Please note that I strongly recommend that you use Tomcat
within Eclipse instead of executing Tomcat by hand. Whether you run Tomcat
within Eclipse or do it manually, you can also customize the Tomcat configuration.

1. Unzip Tomcat. Unzip tomcat-6.0.28-preconfigured.zip into the top level of


the C drive. This should result in C:\apache-tomcat-6.0.28\. This
preconfigured version of Tomcat has the following settings already in place.
For details on customizing this configuration, please see the detailed
configuration guide.
o The port is changed from 8080 to 80. This lets you enter URLs of the
form http://localhost/... instead of http://localhost:8080/....
 When you download Tomcat from the Apache site, the port is
8080 in case you have another server running on port 80.
o Servlet reloading is enabled. This lets you deploy a modified
servlet .class file without restarting the server.
 When you download Tomcat from the Apache site, servlet
reloading is disabled for performance reasons. You might want
it disabled on a server used for a deployed application, but you
definitely want it enabled during development.
o The invoker servlet is enabled. This lets you drop a servlet .class file
into webapps/appName/WEB-INF/classes/packageName and
immediately run it with a URL of the form
http://localhost/appName/servlet/packageName.ServletName. That is,
the invoker servlet saves you from editing web.xml to give a servlet-
mapping to your servlet.
 When you download Tomcat from the Apache site, the invoker
servlet is disabled. You DEFINITELY want the invoker servlet
disabled on a server used for a deployed application, but
having it enabled on your development server is very
convenient for quick testing.
o Tomcat monitors struts-config.xml and faces-config.xml. Whenever
either of these files changes, Tomcat automatically reloads the Web
application. This saves you from restarting the server when you
change these files.
 If you do not use Struts or JSF, this change will not be
beneficial to you. But it does not hurt either way.
o Directory listings are turned on. If you type a URL ending in / and
there is no welcome file in that folder, Tomcat shows a directory
listing.
 Directory listings were on by default in previous Tomcat
versions. They are not required but are convenient during
development.
2. Set JAVA_HOME. Set this environment variable to point at the top-level of
your Java installation directory (e.g., C:\Program Files\Java\jdk1.6.0_21.) Use
the Control Panel, see the sample autoexec.bat file, or read this section of
the page on configuring your development environment.
3. Set CLASSPATH. Set this environment variable to include ".", C:\Servlets+JSP,
and the servlet/JSP JAR files. For details, see the sample autoexec.bat file or
this section of the page on configuring your development environment.
For a very quick test, open C:\Servlets+JSP, double click on the shortcut to startup.bat, and
open http://localhost/ in your browser. If you see something about Tomcat, the setup was
successful. But be sure to do the more complete server tests to verify that you have your
development and deployment environment also set up properly.

Apache Tomcat 6 Tutorial


Configuration Details

For most people, the easiest way to get started with Tomcat is to use a preconfigured version as
described in these links:

• With Eclipse
• For manual execution (no IDE)

However, if you want to customize the configuration, this page provides more detail.

Executive Summary

Configuration Details

1. Download the Apache Tomcat Software

2. Set the JAVA_HOME Variable

3. Change the Port to 80

4. Turn on Servlet Reloading

5. Enable the Invoker Servlet

6. Turn on Directory Listings (Optional)


7. Using the Windows .exe Installer

Executive Summary

• Install the Java JDK. Make sure JDK 1.5 or 1.6 is installed. Download from
http://java.sun.com/javase/downloads/. If you plan to execute Tomcat
manually, you also need to set your PATH environment variable so that both
"java -version" and "javac -help" give a result. The PATH variable is not
needed if you use Tomcat within Eclipse. Warning: JDK 1.6.0_21. Eclipse
has a bug when used with JDK 1.6.0_21 that causes it to run out of memory
and crash. If you use 1.6.0_21, please see the PermGen bug fix page.
• Configure Tomcat. Even if you use Eclipse, you may want to change some
of the Tomcat configuration settings. But it is probably simplest to start with
the preconfigured version (with Eclipse or for manual deployment), then
change the settings shown below later once you have experience with the
most basic setup. If you use Eclipse, you need to re-register the server once
you change the settings.
1. Download the software. Go to http://tomcat.apache.org/download-
60.cgi and download and unpack the zip file for the current release
build of Tomcat 6.0. This tutorial uses Tomcat 6.0.28, but the
directions are very similar for any recent Tomcat 6 release.
2. Set the JAVA_HOME variable. Set it to refer to the base JDK directory,
not the bin subdirectory. Not needed if you use Tomcat within
Eclipse.
3. Change the port to 80. Edit install_dir/conf/server.xml and change
the port attribute of the Connector element from 8080 to 80.
4. Turn on servlet reloading. Edit install_dir/conf/context.xml and
change <Context> to
<Context reloadable="true" privileged="true">.
5. Enable the invoker servlet. Go to install_dir/conf/web.xml and
uncomment the servlet and servlet-mapping elements that map the
invoker servlet to /servlet/*.
6. Turn on directory listings. Go to install_dir/conf/web.xml, find the
init-param entry for listings, and change the value from false to
true.
• Test the server. Eclipse users should skip this section and instead follow the
much simpler instructions in the Tomcat with Eclipse page.
1. Verify that you can start the server. Double-click
install_dir/bin/startup.bat and try accessing http://localhost/.
2. Check that you can access your own HTML & JSP pages. Drop some
simple HTML and JSP pages into install_dir/webapps/ROOT and access
them with http://localhost/filename.
• Set up your development environment. Eclipse users should skip this
section and instead follow the much simpler instructions in the Tomcat with
Eclipse page. This part is only for those that want to edit and deploy
manually.
1. Create a development directory. Put it anywhere except within the
Tomcat installation hierarchy.
2. Make shortcuts to the Tomcat startup & shutdown Scripts. Put
shortcuts to install_dir/bin/startup.bat and
install_dir/bin/shutdown.bat in your development directory and/or on
your desktop.
3. Set your class path. Use the CLASSPATH environment variable. Include
the current directory ("."), the servlet/JSP JAR files
(install_dir/lib/servlet-api.jar, and install_dir/lib/jsp-api.jar, and
install_dir/lib/el-api.jar), and your main development directory from
Step 1.
4. Bookmark the servlet & JSP javadocs. Add the servlet 2.5 API and the
JSP 2.1 API to your bookmarks/favorites list.
• Compile and test some simple servlets. Eclipse users should skip this
section and instead follow the much simpler instructions in the Tomcat with
Eclipse page.
1. Test a packageless servlet. Compile a simple servlet, put the .class
file in install_dir/webapps/ROOT/WEB-INF/classes, and access it with
http://localhost/servlet/ServletName.
2. Test a servlet that uses packages. Compile the servlet, put the .class
file in install_dir/webapps/ROOT/WEB-INF/classes/packageName, and
access it with http://localhost/servlet/packageName.ServletName.
3. Test a servlet that uses packages and utility classes. Compile a
servlet, put both the servlet .class file and the utility file .class file in
install_dir/webapps/ROOT/WEB-INF/classes/packageName, and
access the servlet with
http://localhost/servlet/packageName.ServletName. This third step
verifies that the CLASSPATH includes the top level of your development
directory.

Configuration Details

For most people, the easiest way to get started with Tomcat is to use a preconfigured version as
described in these links:

• With Eclipse
• For manual execution (no IDE)

However, if you want to customize the configuration, this page provides more detail.

Executive Summary
Configuration Details

Configuring Tomcat involves four main steps and three optional steps:

1. Downloading the Tomcat software.


2. Setting the JAVA_HOME variable.
3. Changing the port from 8080 to 80.
4. Telling Tomcat to reload servlets when they are modified.
5. Enabling the invoker servlet.
6. Turning on directory listings.
7. Using the Windows .exe installer instead of the .zip file. (Not
Recommended)

Details of each step are given below. If you use Eclipse, re-register the server after changing
the configuration files.

1. Download the Apache Tomcat Software

Go to http://tomcat.apache.org/download-60.cgi and download and unpack the zip file for the
current release build of Tomcat 6. You specify the top-level directory (e.g., C:\) and the zip file
has embedded subdirectories (e.g., apache-tomcat-6.0.28). Thus, C:\apache-tomcat-6.0.28 is a
common resultant installation directory. Note: from this point forward, I'll refer to that location
as install_dir. For Windows, there is also a .exe installer; I prefer the .zip file, but see the .exe
installer section for notes on the differences between the two.

Alternatively, you can use my preconfigured Tomcat version:

• With Eclipse
• For manual execution (no IDE)

This preconfigured version already has the port changed to 80, servlet reloading enabled, the
invoker servlet turned on, and directory listings allowed. It also comes with a sample
development directory, sample autoexec.bat file, startup/shutdown shortcuts, and shortcuts for
deploying applications.

2. Set the JAVA_HOME Variable

If you use Tomcat with Eclipse, you can skip this step. However, if you compile and deploy
manually, you must set the JAVA_HOME environment variable to tell Tomcat where to find Java.
Failing to properly set this variable prevents Tomcat from compiling JSP pages. This variable
should list the base JDK installation directory, not the bin subdirectory. For example, on older
Windows versions, you might put the following line in your C:\autoexec.bat file.

set JAVA_HOME=C:\Program Files\Java\jdk1.6.0_21


On recent Windows versions, it is generally better to use the Control Panel than to use
autoexec.bat. Go to the Start menu, select Control Panel, choose System, click on the
Advanced tab, press the Environment Variables button at the bottom, and enter the JAVA_HOME
variable and value directly. On Windows 2000 and NT, you do Start, Settings, Control Panel,
System, then Environment

3. Change the Port to 80

Assuming you have no other server already running on port 80, you'll find it convenient to
configure Tomcat to run on the default HTTP port (80) instead of the out-of-the-box port of
8080. Making this change lets you use URLs of the form http://localhost/blah instead of
http://localhost:8080/blah. Note that you need admin privileges to make this change on
Unix/Linux. Also note that some versions of Windows automatically start IIS on port 80. So, if
you use XP and want to use port 80 for Tomcat, you may need to disable IIS (see the
Administrative Tools section of the Control Panel).

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"


... >

You can also:

• Use my preconfigured Tomcat version. Apache Tomcat 6.0.28 with all


server.xml, context.xml, and web.xml changes, plus the sample HTML, JSP,
and Java files. Right-click or shift-click on the link to download the file.
• Download my modified server.xml for Tomcat 6.0. From Apache Tomcat
6.0.28, but should work on most versions of Tomcat 6.0.

4. Turn on Servlet Reloading

The next step is to 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.
This slightly degrades performance in deployment situations, so is turned off by default.
However, if you fail to turn it on for your development server, and you compile/deploy
manually, you'll have to restart the server every time you recompile a servlet that has already
been loaded into the server's memory. Since this tutorial discusses the use of Tomcat for
development, this change is strongly recommended. Note that Eclipse takes care of this for you
automatically.

To turn on servlet reloading, edit Edit install_dir/conf/context.xml and change

<Context>

to

<Context reloadable="true" privileged="true">


Note that the privileged entry is really to support the invoker servlet (see the following
section), so you can omit that entry if you do not use the invoker.

You can also:

• Use my preconfigured Tomcat version. Apache Tomcat 6.0.28 with all


server.xml, context.xml, and web.xml changes, plus the sample HTML, JSP,
and Java files. Right-click or shift-click on the link to download the file.
• Download my modified context.xml for Tomcat 6.0. From Tomcat 6.0.18,
but should work on most versions of Tomcat 6.0.

5. 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). The invoker servlet is extremely convenient when you are
learning and testing out various APIs. You probably want to enable it when learning, but you
should disable it again before deploying any real applications.

To enable the invoker servlet, uncomment the following servlet and servlet-mapping
elements in install_dir/conf/web.xml. 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.

<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>

In Tomcat 6 (but not Tomcat 5.5), you also need the privileged="true" entry in the Context
element of context.xml. See the previous section for an example.

You can also:

• Use my preconfigured Tomcat version. Apache Tomcat 6.0.28 with all


server.xml, context.xml, and web.xml changes, plus the sample HTML, JSP,
and Java files. Right-click or shift-click on the link to download the file.
• Download my modified web.xml for Tomcat 6.0. From Tomcat 6.0.28, but
should work on most versions of Tomcat 6.0.
6. Turn on Directory Listings (Optional)

In previous Tomcat versions, if you entered a URL ending in a slash (/) and there was no
welcome-file in the directory (or servlet-mapping that matched the URL), Tomcat displayed a
directory listing. In Tomcat 6, the default was changed from true to false for these directory
listings. Many developers find it convenient to turn directory listings back on so that when
practicing or during the early project development phases they can simply type in a directory
and then click on an HTML or JSP page. To make this change, edit install_dir/conf/web.xml
and change the init-param value of listings for the default servlet, as below. 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.

<servlet>
<servlet-name>default</servlet-name>
<servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-
class>
<init-param>
<param-name>debug</param-name>
<param-value>0</param-value>
</init-param>
<init-param>
<param-name>listings</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

You can also:

• Use my preconfigured Tomcat version. Apache Tomcat 6.0.28 with all


server.xml, context.xml, and web.xml changes, plus the sample HTML, JSP,
and Java files. Right-click or shift-click on the link to download the file.
• Download my modified web.xml for Tomcat 6.0. From Tomcat 6.0.28, but
should work on most versions of Tomcat 6.0.

7. Using the Windows .exe Installer

If you are using Microsoft Windows and do not use Eclipse or another IDE, you can download
a .exe Tomcat installer instead of the .zip file discussed in this tutorial. In my opinion, it is not
worth the bother to do so, but some non-Eclipse users like it. If you use it, note these
differences:

• It will prompt you for the desired port. It will ask you what port it
should run on, and make the changes in server.xml for you. You will still
need to manually edit context.xml and web.xml, however.
• It will set JAVA_HOME for you. The installer hunts for your Java installation
and tries to set JAVA_HOME appropriately. This is a convenience, albeit a
minor one.
• It will set up Start Menu entries. In particular, instead of using
startup.bat and shutdown.bat, you can go to the Start Menu, select Apache
Tomcat 6.0, select Monitor Tomcat, and select Start or Stop, as shown
below. I prefer startup.bat and shutdown.bat so that I can put shortcuts in
my development directory (easier to invoke) and/or the desktop (where I
can associate keyboard shortcuts).

Você também pode gostar