Creating and Deploying a Servlet on Tomcat

This section provides a tutorial example on how to create a simple Servlet class, compile it and deploy it on the Tomcat server.

To confirm that Apache Tomcat is a Servlet engine or container, I did the following steps to create my first Servlet page:

1. Write the following simple Servlet example in any text editor:

 * Copyright (c) 2012,, All Rights Reserved.
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloServlet extends HttpServlet { 
   public void doGet(HttpServletRequest req, HttpServletResponse res)
      throws ServletException, IOException {
      PrintWriter out = res.getWriter();
      out.println("<p>Hello World! -- From Servlet</p>");

2. Save this Servlet class file,, to the "classes" folder of the default application folder on the Tomcat server: C:\local\apache-tomcat-7.0.32\webapps\ROOT\WEB-INF\classes\

3. Compile this Servlet class file into Java bytecode in a command window. Note that "servlet-api.jar" is needed to complete the compilation.

C:\>set classpath=\local\apache-tomcat-7.0.32\lib\servlet-api.jar

C:\>cd \local\apache-tomcat-7.0.32\webapps\ROOT\WEB-INF\classes


   791 HelloServlet.class

4. Map the Servlet to a URL path name on the Tomcat server by editing the "\local\apache-tomcat-7.0.32\webapps\ROOT\web.xml" file:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- ... -->
<web-app xmlns=""
  version="3.0" metadata-complete="true">

  <display-name>Welcome to Tomcat</display-name>
     Welcome to Tomcat

<!-- Add the following 8 lines to map a Servlet class to a URL -->


5. Access the Servlet class through the mapped URL "http://localhost:8080/Hello.servlet" with any browser. The output of my first Servlet class shows up:

Hello World! -- From Servlet

Congratulations! I have successfully created and deplyed a Servlet class on the Tomcat server!

Last update: 2012.

Table of Contents

 About This Book

 JSP (JavaServer Pages) Overview

 Tomcat 7 Installation on Windows Systems

 JSP Scripting Elements

Java Servlet Introduction

 What Is Servlet?

Creating and Deploying a Servlet on Tomcat

 Relationship between Servlet and JSP

 Modifying the Servlet Converted from a JSP Page

 JSP Implicit Objects

 Syntax of JSP Pages and JSP Documents

 JSP Application Session

 Managing Cookies in JSP Pages

 JavaBean Objects and "useBean" Action Elements

 Managing HTTP Response Header Lines

 Non-ASCII Characters Support in JSP Pages

 Performance of JSP Pages

 EL (Expression Language)

 Overview of JSTL (JSP Standard Tag Libraries)

 JSTL Core Library

 JSP Custom Tags

 JSP Java Tag Interface

 Custom Tag Attributes

 Multiple Tags Working Together

 File Upload Test Application

 Outdated Tutorials


 PDF Printing Version