keepcalculator

06 Numbers and Powers

Apache Tomcat Server: What It Is and How It Works

Card No. 06·73595 · Updated · Standard formula, shown below

Apache Tomcat is an open-source Java servlet container and web server that runs Java-based web applications, implementing the Jakarta Servlet, Jakarta Server Pages (JSP), and WebSocket specifications. You deploy your Java application as a WAR file into Tomcat, and it handles the HTTP requests, manages servlet lifecycles, and returns dynamic responses to clients. It is the most widely used Java application server for production environments because it is free, lightweight, and deeply integrated with the Java ecosystem.

Why Apache Tomcat Exists and How It Started

Tomcat began as a reference implementation for the Java Servlet and JSP specifications, created by James Duncan Davidson at Sun Microsystems in 1999. It was later donated to the Apache Software Foundation, where it remains a top-level project. The name “Tomcat” was chosen because the animal represented something that could stand on its own, and the mascot stuck. Today, Tomcat is maintained by a volunteer community and is the default servlet container embedded in many Java development tools, including Eclipse and IntelliJ IDEA.

How Tomcat Works: The Core Components

Tomcat is built around a set of nested components that handle network connections, request processing, and application execution. The main parts are:

  • Server – the top-level component that represents the entire Tomcat instance.
  • Service – a grouping of one or more connectors and a single engine.
  • Connector – listens for incoming TCP connections on a specific port (default 8080 for HTTP) and translates HTTP requests into Java objects.
  • Engine – the request-processing pipeline that receives requests from connectors and routes them to the appropriate host.
  • Host – represents a virtual host (e.g., www.example.com) and contains one or more contexts.
  • Context – represents a single web application, typically a WAR file or an exploded directory.

When a request arrives, the connector parses it, the engine passes it to the matching host, and the host forwards it to the context that maps to the requested URL path. The context then invokes the servlet or JSP that handles the request, and the response travels back through the same chain.

How to Choose: Tomcat vs. Other Java Servers

Tomcat is not a full Java EE (now Jakarta EE) application server; it does not include EJB, JMS, or full JTA support. For simple web applications that only use servlets, JSP, and JDBC, Tomcat is ideal. If you need enterprise features like distributed transactions, message queues, or remote EJBs, you should consider a full application server such as WildFly, Payara, or Open Liberty. However, many modern microservices and Spring Boot applications embed Tomcat directly, because it provides everything needed for HTTP-based REST APIs without the overhead of a full server.

Common Problems and How to Fix Them

  • Port 8080 already in use – Change the port in conf/server.xml or stop the other process. On Windows, use netstat -ano to find the PID; on Linux, use lsof -i :8080.
  • OutOfMemoryError – Increase the JVM heap by setting JAVA_OPTS="-Xms256m -Xmx512m" in bin/setenv.sh (or setenv.bat on Windows).
  • 404 error for your servlet – Check that your web.xml or annotations map the URL correctly, and that the WAR file is deployed without errors in logs/catalina.out.
  • Slow startup or high CPU – Look for infinite loops in servlet init() or doGet() methods, and ensure you are not creating threads per request.

Security Considerations for Tomcat

Tomcat is secure by default in many ways, but you should take several steps to harden it:

  • Run Tomcat with a non-root user (on Linux) to limit damage if compromised.
  • Remove the default manager and host-manager webapps if you do not need remote administration.
  • Use HTTPS with a valid certificate; Tomcat supports TLS via the server.xml connector configuration.
  • Keep Tomcat and the JVM updated to the latest stable version to patch known vulnerabilities.
  • Set strong passwords for the admin and manager roles in conf/tomcat-users.xml.

Alternatives to Apache Tomcat

If Tomcat does not fit your needs, consider these alternatives:

  • Jetty – a lighter, more embeddable servlet container, often used in embedded devices and microservices.
  • Undertow – a flexible, high-performance web server used by WildFly, also embeddable.
  • Spring Boot with embedded Tomcat – not a separate server, but a way to run Tomcat inside your application, simplifying deployment.
  • Payara / WildFly – full Jakarta EE servers for enterprise applications.

Frequently Asked Questions

Is Apache Tomcat the same as the Apache HTTP Server?

No. The Apache HTTP Server is a C-based web server for static content, while Tomcat is a Java servlet container for dynamic Java applications. They can be used together (e.g., Apache HTTP Server serving static files and proxying requests to Tomcat), but they are separate projects.

What is the default port for Tomcat?

Tomcat listens on port 8080 for HTTP by default, and port 8005 for the shutdown command. You can change these in conf/server.xml.

Do I need to install Java to run Tomcat?

Yes. Tomcat requires a Java Development Kit (JDK) or Java Runtime Environment (JRE) version 8 or later (for Tomcat 9 and 10). Tomcat 10+ requires Java 11 or newer.

How do I deploy a WAR file in Tomcat?

Copy the WAR file to the webapps directory. Tomcat will automatically deploy it by extracting it to a folder with the same name (minus the .war extension). You can also use the Tomcat Manager web application to upload and deploy WAR files.

Can I run multiple applications on one Tomcat instance?

Yes. Each application is a separate context, usually a WAR file in webapps. They share the same JVM and server, but have isolated classloaders to prevent conflicts.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →