Tomcat

Apache Tomcat Vs Nginx Comparison

Apache Tomcat and Nginx server, were created for two different tasks. NGINX is a free, open-source, high-performance HTTP server and reverse proxy, as well as an IMAP/POP3 proxy server and Apache Tomcat is an open source implementation of the Java Servlet, JavaServer Pages, Java Expression Language and Java WebSocket technologies. The Java Servlet, JavaServer Pages.
 
 
 
 
 
 
 
 
 

1. The tools

  • Apache Tomcat 8
  • Nginx server 1.10.2

2. Introduction

In this article We are going to compare Tomcat and Nginx servers and we are going to see where it is better to use one instead of the other.

3. Prerequisites

  • Tomcat 8 installed and running

4. Nginx server

Nginx server is a web server, which used to serve static content like HTTP pages and serve dynamic content using FastCGI or as a reverse proxy.
Nginx server is a high-performance HTTP server and reverse proxy, ideal to use as a front end of another web servers. It is possible to use Nginx as a very efficient HTTP load balancer to distribute traffic to several application servers and to improve performance, scalability and reliability of web applications with Nginx.
 
Nginx can be deployed to serve dynamic HTTP content on the network using FastCGI, SCGI handlers for scripts, WSGI application servers or Phusion Passenger modules, and it can serve as a software load balancer. Nginx uses an asynchronous event-driven approach to handling requests. Nginx modular event-driven architecture can provide more predictable performance under high loads.

4.1 Nginx request/response

Static content is meant to use a request/response scheme that is a message exchange pattern. The client makes a request to the server and the server sends a response.

In the case of the Nginx server it’s main purpose is to serve static HTML files that are text files formated with the HTML markup language. Other static content could be served as well, like XML, JSON, images, video files, sound files, etc. Also Nginx server could be used as a reverse proxy and a load balancer to serve as a frontend of other servers.

4.2 Download Nginx

Go to the URL http://nginx.org/en/download.html and download the Nginx server for Windows.

1 Download Nginx
1 Download Nginx

4.3 Install Nginx

Nginx comes in a zip compressed folder, all you need to do is uncompress the folder on your hard disk and you have a working Nginx on windows. Choose a folder and unzip Nginx.

2 Install Nginx
2 Install Nginx

4.4 Nginx serve a simple HTML file

Let’s create a simple HTML file to show the static content.

HTML static content

<DOCTYPE! HTML>
<html>
	<head>
		<title>This is the title</title>
	</head>
	<body>
		<div>
			<p>This is static content</p>
		</div>
	</body>
</html>

Nginx serve its static documents from the html directory.

Save the HTML file as example.html and copy the file in the html directory.

3 Nginx document root
3 Nginx document root

4.5 Start Nginx

Open a terminal window and type start nginx

4 Start Nginx
4 Start Nginx

Now open the browser to see the static content. Go to the URI http://localhost/example.html and you see the following page.

4 Static content
4 Static content

The browser, in this case Firefox, makes a request to the Nginx server and then the server as a response sends the page we created before.

4.6 Stop Nginx

To stop the nginx server type nginx -s stop

5 Stop Nginx
5 Stop Nginx

5. Tomcat server

Tomcat server is a servlet container, which is designed to serve Java servlets. Tomcat is a server that is meant to run applications that were written in Java.
The servlets are Java classes that are used to serve dynamic web content. You can use Tomcat to serve static content as well. Recent versions of Tomcat have an improved performance to serve static content.

5.1 Example servlet

5.1.1 Create the project

We are going to create an example servlet to see how this works.

Open eclipse.

Click File->New Project.

6 New Project
6 New Project

Choose Dynamic Web Project

7 Dynamic Web Project
7 Dynamic Web Project

Click next and then write the name of the project

8 Name The project
8 Name The project

Press Finish.

5.1.2 Create the servlet

Right click on the project folder and press New->Servlet

9 New Servlet
9 New Servlet

Write the name of the servlet and press next.

10 Name the servlet
10 Name the servlet

Look at the URL Mapping automatically created by eclipse and then press next. This URL Mapping is the relative URI where you can find the servlet.

11 URL Mapping
11 URL Mapping

Choose doGet, for this simple servlet we are only going to create the GET request/response. As you can see a Servlet could have a much rich interface than a static web page to process the requests.

12 Method Stubs
12 Method Stubs

Inside the doGet method, write the following code.

doGet Method

		
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		try (PrintWriter writer = response.getWriter()) {

			writer.println("");
			writer.println("");
			writer.println("");
			writer.println("Example Servlet");
			writer.println("");
			writer.println("");
			writer.println("<h1>Sample Servlet</h1>");
	 		writer.println("");
			writer.println("Sample Servlet."); writer.println("");
			writer.println("");
			writer.println(""); } }

With this code we are doing the following.
response.setContentType("text/html"); Sets the response to be HTML.
response.setCharacterEncoding("UTF-8"); Sets the character encoding
PrintWriter writer = response.getWriter() Gets the writer to write in the standard servlet output.

Then we write each line of html in the writer to create our response html page.
using writer.println

5.1.3 Run the servlet

Right click on the project and press Run as -> Run on Server

13 Run Servlet
13 Run Servlet

Choose Tomcat server and press finish.

14 Run on Server
14 Run on Server

Open the browser on the link http://localhost:8080/SampleServlet/Sampleservlet
And we get the following output:

15 Servlet Output
15 Servlet Output

5.2 Tomcat request/response

Tomcat is a servlet container, you can have many servlets in a Tomcat instance. All JSP pages are compiled into a servlet.
When you use Tomcat and the user request a resource in the server, the servlet container process the request, then it chooses what to do with the request. If the request has a valid URI, Tomcat gets the resource and sends the response to the client.

16 Servlet request response
16 Servlet request response

6. Complete Source code

example.html

<DOCTYPE! HTML>

<html>
	<head>
		<title>This is the title</title>
	</head>
	<body>
		<div>
			<p>This is static content</p>
		</div>
	</body>
</html>

SampleServlet.java

package com.myservlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/SampleServlet")
public class SampleServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public SampleServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		try (PrintWriter writer = response.getWriter()) {

			writer.println("");
			writer.println("");
			writer.println("");
			writer.println("Example Servlet");
			writer.println("");
			writer.println("");
			writer.println("<h1>Sample Servlet</h1>");
			writer.println("");
			writer.println("Sample Servlet.");
			writer.println("");
			writer.println("");
			writer.println(""); } } }

7. Conclusion

As you can see Nginx and Tomcat serve different purposes and you can combine them to make a good solution to serve web content, you can use tomcat to serve your dynamic content using servlets and use Nginx as a frontend reverse proxy and load balancer to serve a Tomcat cluster.

8. Download the Source Code

This was an example of: Apache Tomcat Vs Nginx Comparison.

Download
You can download the source files here: Apache Tomcat Vs Nginx Comparison

Jesus Boadas

I'm a self taught programmer, I began programming back in 1991 using an IBM A10 mainframe with Pascal an Assembler IBM 360/70 emulator and Turbo C on a X86 PC, since that I work for the banking industry with emerging technologies like Fox Pro, Visual Fox Pro, Visual Basic, Visual C++, Borland C++, lately I moved out to the Airline industry, leading designing and programming in-house web applications with Flex, Actionscript, PHP, Python and Rails and in the last 7 years I focused all my work in Java, working on Linux servers using GlassFish, TomCat, Apache and MySql.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
l84lnch
l84lnch
1 year ago

It looks like there may be some truly valuable and interesting content in here, but the web page is so inundated with ads that it is impossible to ferret out the useful information. I understand the need for ad revenue, etc. but this is so extreme as to render your site unusable.

Back to top button