Introduction to Apache Tomcat

Apache Tomcat is a long-standing open source Java Servlet container, which implements several core Java enterprise specifications, namely Java Servlet, JavaServer Pages (JSP) and WebSockets API.

Tomcat is an Apache Software Foundation project, which was first released in 1998, only four years after Java itself. Tomcat began as the first reference implementation of the Java Servlet API and JSP specifications. Although Tomcat is no longer a reference implementation of these two technologies, Tomcat is still the most widely used Java server. It has a well-tested and validated core engine and has good scalability.
Introduction to Apache Tomcat Introduction to Apache Tomcat

In this brief introduction, you will understand why many software stores choose Tomcat to run Java web applications. You will get an overview of Tomcat and its usage, as well as installation instructions for the latest version at the time of writing.

What kind of server is Tomcat?

The Java ecosystem supports multiple types of application servers, so let's disambiguate them and see where Tomcat is applicable:
A servlet container is an implementation of the Java Servlet specification, mainly used for hosted Java servlets.
A web server is an Apache server designed to provide file services from the local system.
A Java enterprise application server is a fully mature implementation of the Java EE (now Jakarta EE) specification.

Essentially, Tomcat is a Servlet and JSP container. A Java servlet encapsulates code and business logic, and defines how requests and responses should be processed on the Java server. JSP is a server-side view rendering technology. As a developer, you write a servlet or JSP page, and then let Tomcat handle the routing.

Tomcat also includes the Coyote engine, which is a web server. Thanks to Coyote, Tomcat can be extended to include various Java enterprise specifications and features, including Java Persistence API (JPA).
Tomcat also has an extended version called TomEE, which contains more enterprise features. I will briefly introduce TomEE later in this article.

Let's start with using Tomcat to host the servlet and JSP.

Download and install Tomcat

As the oldest of the software world, there are many Tomcat versions available. Information about version differences can be found on the Tomcat homepage. Usually, you can choose the latest stable version.

For our purposes, download the latest version of Tomcat, the current version is Tomcat 9. You can choose to download Tomcat as an archive file (.zip or tar.gz) or as an installed service. The best choice is up to you-unless of course you are not running on Windows, archive will be used. We will use archives in this article.

Tomcat Windows installation

If you are running Windows and want to use the installer, just download the .exe file and run it. Tomcat will install itself as a service with reasonable defaults. It will then inform you of the installation location, and you can proceed as if you unzipped the archive in it.

Step 1. Command line installation

Go to the command line and type gunzip apache-tomcat-9.0.19.tar.gz and then tar -xf apache-tomcat-9.0.19.tar. This will create the following directory:
/bin contains the script used to execute Tomcat .
/webapps is where you will deploy the application.
/logs is the location where Tomcat outputs logs. Please note that /logs/catalina.out will enter the Tomcat log by default. You can use this file together with the application-specific log file to debug the problem.
/lib is where Tomcat looks for JARs. Here, you will store other packages not included with Tomcat, such as JPA.
/conf is Tomcat's configuration XML, where you can perform operations such as adding users and roles to Tomcat.

Step 2. Start Tomcat

If you install Tomcat as a service, it is already running. Otherwise, please go ahead and start it by typing in the command line via ./catalina.sh start. (Type ./catalina.sh without parameters to see all available commands). You should now be able to browse to the Tomcat welcome screen in your browser.

Deploy the application in Tomcat

Tomcat's webapps directory is where you deploy your application. You can put the .war file there, and Tomcat will run it. A WAR file is a standard packaging of web application resources: a JAR file that contains some other files that tell the container (Tomcat in this case) how to run it.

In addition to the standard packaging, there are three other ways to deploy content in Tomcat.

Explosive deployment

An "explosive" web application is an application that is not compressed as a WAR file, which means it still contains all the elements listed in the directories and files. The Tomcat archive you unzipped comes with several examples deployed in this way, which you can find in the /webapps/examples directory. The advantage of expanded deployment is that you can view the files there without worrying about compression.

If you navigate to http://localhost:8080/examples/, you will find a list of links. This page is rendered by Tomcat through the /webapps/examples/index.html file. Tomcat is serving HTML files from the file system, which is an instance of the Tomcat Coyote engine acting as a web server.

Feel free to browse the provided examples, they provide you with a good overview of the capabilities of Tomcat serving servlets, JSP and WebSockets.

Tomcat also contains a management application by default, which is located in the /manager path. Among other things, this application allows you to start, stop and redeploy applications from the web console.

Serve static content

You can provide files from the file system, or forward from Tomcat to another HTTP server (such as Apache). Another common setting is to put a file server (such as Apache or Nginx) in front of Tomcat, and then forward your API requests to Tomcat. In these cases, the mod_JK library is used to configure Tomcat and Apache (or even another web server, such as IIS) to communicate.

In order to improve performance, mainly in terms of delivering static content, Tomcat also provides native packaging for Windows and Linux . This is called Tomcat APR, and more information is available here. These are not necessary for typical use cases, but are easy to understand.

Embedded Tomcat

For a long time, Jetty is the only server that can run as an embedded server. That has changed, and now Tomcat can also run embedded. The idea of ​​using an embedded server is that, so far, you don’t have a server that contains application files, but an application with a main class (ie a standalone Java application) that calls server functions from : In its code base. Overall, this provides a simpler and portable development model, and quickly became the standard. For example, Spring Boot uses an embedded Tomcat instance running in dev mode.

Running an embedded server simplifies operations, because you are now only dealing with a single component (application) instead of dealing with both application and server deployment. On the other hand, the setup of Tomcat running as a standalone host is still common.

TomEE

By adding these libraries to Tomcat itself or application dependencies, more standard Java EE (or Jakarta EE) features can be used with Tomcat. Another option is the TomEE server. TomEE is the same Tomcat engine with additional Java enterprise support, including the popular JPA and CDI (Context and Dependency Injection) APIs. The TomEE specification is based on the Java EE Web configuration file, so it provides you not only Tomcat, but also not mature Java EE application servers like WildFly or Glassfish.

High availability and clustering

Tomcat supports high availability and clustering. Essentially, high availability refers to the ability to fail over to another instance of the server and re-create the session as if nothing went wrong. Clustering is the ability to create multiple versions of the same server to handle large amounts of traffic.

in conclusion

Tomcat maintains active development, keeps pace with changes, and provides a solid and reliable platform for deploying Web applications. Its continued popularity and its choice as the default Java platform for many PaaS systems have proved its continued success.

This article address: https://www.linuxprobe.com/apache-tomcat-intro.html

Guess you like

Origin blog.csdn.net/u014389734/article/details/107755770