Home » Enterprise Java » Tomcat » Difference Between Apache Web Server and Tomcat

About Jesus Boadas

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.

Difference Between Apache Web Server and Tomcat

Apache Tomcat and the Apache httpd server, were created for two different tasks but sometimes We can confuse the functionality of each other just because they could solve the same problems.

1. The tools

  • Apache Tomcat 8
  • Apache Httpd server 2.4

2. Introduction

In this article we are going to discover some key differences between Tomcat and httpd servers and where is better to use one instead of the other.

3. Prerequisites

  • Httpd server 2.4 installed
  • Tomcat 8 installed and running

4. Httpd server

Apache HTTP server is a web server, which used to serve static content like HTTP pages and serve dynamic content with the help of scripting languages like PHP, PERL, PYTHON, etc.
Apache HTTP server is a general-purpose HTTP server, which supports a number of advanced options like a proxy, load balancing, URL rewriting, virtual hosts, SSL/TLS encryption, CGI content and a huge list of modules that involves everything a web server can do.

4.1 Apache HTTP server 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 httpd server.
One of the main purposes of the Apache web server is to serve 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.

1 Apache Web Server Request-Response

1 Apache Web Server Request-Response

4.2 Apache HTTP server serving 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>

Apache serve documents from the DocumentRoot directory. Open the file httpd.conf to see where the DocumentRoot is.

xml

Define SRVROOT "C:\Java\Apache24"
ServerRoot "${SRVROOT}"
DocumentRoot "${SRVROOT}/htdocs"
<Directory "${SRVROOT}/htdocs">
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

As we can see the DocumentRoot is on C:\Java\Apache24\htdocs

Save the HTML file as example.html and copy the file in the htdocs directory. Copy the file in the Apache HTTP server public directory.

2 htdocs directory

2 htdocs directory

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

3 Static content

3 Static content

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

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.

4 New Project

4 New Project

Choose Dynamic Web Project

5 Dynamic Web Project

5 Dynamic Web Project

Click next and then write the name of the project

6 Name The project

6 Name The project

Press Finish.

5.1.2 Create the servlet

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

7 New Servlet

7 New Servlet

Write the name of the servlet and press next.

8 Name the servlet

8 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.

9 URL Mapping

9 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.

10 Method Stubs

10 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

11 Run Servlet

11 Run Servlet

Choose Tomcat server and press finish.

12 Run on Server

12 Run on Server

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

13 Servlet Output

13 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 get the resource and send the response to the client.

14 Servlet request response

14 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. Download the Source Code

This was an example of: Difference Between Apache Web Server and Tomcat.

Download
You can download the source files here: Source – Difference Between Apache Web Server and Tomcat
0 0 vote
Article Rating

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Subscribe
Notify of
guest
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.

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
BJR
8 months ago

Great post! Thank you!
As an addendum to this post could you please provide a stack diagram similar to the diagram in section 5.2 that shows relationship between the clients and Apache httpd, Apache Tomcat, and PostgreSQL + PostGIS extension + Geoserver (http://geoserver.org/) and that gets dynamic spatial data (in geoJSON format) to the clients (browsers)?
Thank you in advance

1
0
Would love your thoughts, please comment.x
()
x