Introduction to Apache Tomcat
Apache Tomcat is an open-source software developed by the Apache foundation. It’s written in Java and was first released in the year 1999. James Duncan Davidson was its original founder, who happened to be working as a software architect at that time. He began developing apache tomcat software with the aim to provide the implementation for servlets. What is a servlet? In simple words, a servlet is a server-side technology that helps handle a client’s http request and response (please note that the in-depth explanation of servlets is beyond the scope of this article). The latest stable release of a tomcat version 9.0.21 was released on June 7th, 2019.
Apache tomcat may be defined as a web server (that is also referred to as a web container/ servlet container), which processes the servlets, JSP’s (by internally converting your JSP’s to servlets internally) and also render JSP’s.
Note that a web server is different from that of an application server. Let us understand how these two differentiate from one another.
Why is Tomcat said to be a Web Server but not an Application Server?
- When packaging an application, it can either be packaged as a .war or .ear Tomcat is categorized as a web server since it can handle only .war files.
- A web server is responsible to process servlets and JSP’s, whereas an application server should be able to process sturts, EJB’s(Enterprise Java Beans), JSF (Java Server Faces) along with servlets and JSP’s. Tomcat can process only servlet’s and JSP’s, thus making it a web server.
- A web server is often said to be a part of an application server since an application server exhibits all the features that a web server possesses and in addition to it also contains attributes such as load balancing, data persistence, messaging to name a few. However, vice versa does not apply.
- Web servers can operate only over HTTP protocol, whereas application servers can operate over various CGI protocols and may also include HTTP protocol. Application servers are mostly used to handle large enterprise edition applications. Since tomcat operates only on the HTTP protocol, it falls under the web server.
Why we need to use Apache Tomcat?
Web pages by itself are static HTML files. Hence a client cannot interact with a static web page. In order to facilitate our web pages with dynamic capabilities, a web server is required. To be able to interface one’s application with the webserver, predefined API’s (Application Programming Interfaces) are provided. Servlet is one such API provided by the Java Platform Enterprise Edition designed to work along with web servers. Monitoring the server for incoming client requests is not the job of a servlet but that of a web server.
How does Apache Tomcat work?
Tomcat is widely used by web developers when working on web application development. From a high-level perspective, apache tomcat is responsible to provide a run-time environment for the servlets. It provides an environment in which one could run their java code.
On a more detailed aspect, tomcat is responsible for:
- Listen to all incoming requests from clients.
- Load the respective servlet classes using the servlet mappings (from web.xml file) to handle incoming client requests.
- Execute the servlet class and.
- Finally, unload the servlet class.
From the point the servlet class is loaded to the point it’s unloaded, the servlet is responsible for handling the client request by carrying out its various life cycle methods and providing the necessary response back to tomcat as JSP pages. Tomcat then returns the response back to the client by rendering the JSP.
What happens in a Servlets life-cycle?
A servlet life cycle consists of three main methods:
1) init(): This method is used to initialize the servlet. A servlet is initialized only once either when the server is being started or on being invoked by the client using the respective URL.
4.5 (2,207 ratings)
2) service(): Once an instance of the servlet is created, then tomcat calls the service method of the servlet. The service method is responsible for generating the response for the incoming request passed on to it by tomcat. This method makes calls to other resources on the server-side required for fetching data from the database and providing a response back to tomcat.
3) destroy(): The destroy method is invoked by tomcat at the end for all clean up related activities, such as the closing of database connections, freeing up resources for garbage collection, etc.
Advantages of Apache Tomcat
- The biggest advantage of apache tomcat is that it’s open-source. There is no need to shell out money in order to use this software. One could easily download it over the internet and configure it, and start working with it.
- The Apache software foundation provides regular updates to make it compatible with other software versions and providing bug fixes thus making it easier for developers to use.
- Tomcat supports SSL( Secure Socket Layer) and therefore can be configured using an SSL certificate to secure sensitive data by providing a secure connection.
- Tomcat can also be configured to run multiple web applications on different ports. For example, it could be running three applications on 8080, 8081, 9090 port numbers. By default, Apache tomcat makes use of port number 8080.
- It’s also cross-platform compatible, that is can be used on Windows, Mac OS, Linux operating systems.
- It’s said to be lightweight. That is it consumes less in terms of memory and resource utilization, thus allowing the application to run smoothly on most systems without specific system requirements.
Apache Tomcat is the most widely and commonly used software among web application developers today. Studies have claimed that more than 60% of java applications make use of apache tomcat. There are numerous documentation and tutorials on how to use and configure apache tomcat, making it easier and more feasible for new web application developers to work with apache tomcat.
This has been a guide to What is Apache Tomcat. Here we discuss How does it work, why do we need to use it, Servlets life-cycle and advantages. You can also go through our other suggested articles to learn more –