Apache Http Server Java Example

Posted on  by admin

To run the Java example code you will need to install the Java version appropriate for your OS and web server Apache Tomcat: Java (download from the Oracle official web site); Apache Tomcat (download from the official website). Running Apache Tomcat. Registration of handler using org.apache.http.localserver.LocalTestServer (with ElementalHttpServer it is similar - you also have HttpRequestHandler implementation above). Apache is a remarkable piece of application software. It is the most widely used Web Server application in the world with more than 50% share in the commercial web server market. Apache is the most widely used Web Server application in Unix-like operating systems but can be used on almost all platforms such as Windows, OS X, OS/2, etc. Thank you for the answer it seems that 's fixed I got as displayed message: http response = HTTP/1.1 200 OK Content-length: 30, Date: Fri, 22 Jul 2016 08:45:13 GMT do you have an idea how I can the message sent by the server from response? For more information on installing certificates for Apache, see To Create a Security Certificate for Apache. In httpd.conf, for ServerName www.example.com:80, replace www.example.com with the hostname of the local system where Apache is installed. Without this change, you see warnings when you start Apache that the system could not determine the server's fully qualified domain name,.

The package com.sun.net.httpserver provides Http server API, which can be used to build embedded HTTP servers. To get started with this API, we need to get familiar with the following classes:

  • HttpServer

    This class implements a simple HTTP server. It has factory methods create() to create its instance. We need to bind the server to an IP address and port number while initializing.
  • HttpContext

    It represents a mapping between the root URI path to a HttpHandler.
  • HttpHandler

    It is an interface, which needs to be implemented by the application to handle the Http requests. It has only one method:
  • HttpExchange

    An instance of this class is passed to HttpHandler#handle(). It has methods to access Http request information, and to prepare and send the response.

This is a very simple example to create the HttpServer:


Running the above main class and accessing the application in the browser:

Accessing Request Information

Https apache server


On the server console:

Example Project

Dependencies and Technologies Used:

  • JDK 10
  • Maven 3.3.9

HttpCore is a set of low level HTTP transport components that can be used to build custom client and server side HTTP services with a minimal footprint. HttpCore supports two I/O models: blocking I/O model based on the classic Java I/O and non-blocking, event driven I/O model based on Java NIO.

  1. Some examples of HttpCore components in action can be found here

  2. Javadocs

  3. API compatibility reports

Standards Compliance

What Is An Apache Server

HttpCore components strive to conform to the following specifications endorsed by the Internet Engineering Task Force (IETF) and the internet at large:

Apache 2 Web Server

  • RFC 7540 - Hypertext Transfer Protocol Version 2 (HTTP/2)
  • RFC 7541 - HPACK: Header Compression for HTTP/2
  • RFC 7230 - Hypertext Transfer Protocol (HTTP/1.1): Message Syntax and Routing
  • RFC 7231 - Hypertext Transfer Protocol (HTTP/1.1): Semantics and Content
  • RFC 1945 - Hypertext Transfer Protocol – HTTP/1.0
  • RFC 3986 - Uniform Resource Identifier (URI): Generic Syntax