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.
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.
Now open the browser to see the static content. Go to the URI http://localhost/example.html and you see the following page.
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.
Choose Dynamic Web Project
Click next and then write the name of the project
Press Finish.
5.1.2 Create the servlet
Right click on the project folder and press New->Servlet
Write the name of the servlet and press next.
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.
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.
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
Choose Tomcat server and press finish.
Open the browser on the link http://localhost:8080/SampleServlet/Sampleservlet
And we get the following 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.
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.
You can download the source files here: Source – Difference Between Apache Web Server and Tomcat
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