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:

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

2. Save this Servlet class file, HelloServlet.java, 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

C:\local\apache-tomcat-7.0.32\webapps\ROOT\WEB-INF\classes>
   \progra~1\java\jdk1.7.0_07\bin\javac HelloServlet.java

C:\local\apache-tomcat-7.0.32\webapps\ROOT\WEB-INF\classes>dir
   791 HelloServlet.class
   576 HelloServlet.java

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="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_3_0.xsd"
  version="3.0" metadata-complete="true">

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

<!-- Add the following 8 lines to map a Servlet class to a URL -->
    <servlet>
        <servlet-name>Hello</servlet-name>
        <servlet-class>HelloServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Hello</servlet-name>
        <url-pattern>/Hello.servlet</url-pattern>
    </servlet-mapping>

</web-app>

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

 References

 PDF Printing Version