My First Servlet Page on CentOS

This section provides a tutorial example on how to create the first Servlet Java class to test the Servlet support of the Tomcat Web server on CentOS systems.

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

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

 * Copyright (c) 2002 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. Compile this Servlet class file into Java bytecode. Note that "servlet-api.jar" is needed to complete the compilation.

# javac -cp /usr/share/tomcat/lib/tomcat-servlet-3.0-api.jar \

# ls -l HelloServlet.*
-rw-r--r--. 1 root root 791 Jan  8 19:07 HelloServlet.class
-rwxr-xr-x. 1 root root 549 Jan  8 19:06

3. Create the "WEB-INF\classes" sub-directory under my application directory on the Tomcat server. Make sure that it has "read" permission for all users.

# cd /usr/share/tomcat/webapps/herong
# mkdir WEB-INF
# mkdir WEB-INF/classes

4. Save this Servlet bytecode file, HelloServlet.class, to the "WEB-INF\classes" sub-directory. Make sure that it has "read" permission for all users.

# cp HelloServlet.class /usr/share/tomcat/webapps/herong/WEB-INF/classes

4. Map the Servlet to a URL path name on the Tomcat server by creating the "/usr/share/tomcat/webapps/herong/WEB-INF/web.xml" file. Make sure that it has "read" permission for all users.


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

Hello World! -- From Servlet

Congratulations! I have successfully created and deployed a Servlet class on the Tomcat server on my CentOS system!

Table of Contents

 About This Book

 JSP (JavaServer Pages) Overview

 Tomcat Installation on Windows Systems

 JSP Scripting Elements

 Java Servlet Introduction

 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

Using Tomcat on CentOS Systems

 Download and Install Tomcat on CentOS

 Start/Stop Tomcat Server on CentOS

 My First HTML Page on CentOS

 My First JSP Page on CentOS

My First Servlet Page on CentOS

 Using Tomcat on macOS Systems

 Connecting to SQL Server from Servlet

 Developing Web Applications with Servlet

 Archived Tutorials


 Full Version in PDF/EPUB