In Java 9, Oracle has released a new version of HTTP client API which will support HTTP/2 protocol and WebSocket features. As existing HTTP Client API has numerous issues like it supports HTTP/1.1 protocol and does not support HTTP/2 protocol and WebSocket, works only in blocking mode and lot of performance issues.

What is HTTP/2 ?

HTTP was originally proposed by Tim Berners-Lee, the pioneer of the World Wide Web who designed the application protocol with simplicity in mind to perform high-level data communication functions between Web-servers and clients.

In February 2015, the Internet Engineering Task Force (IETF) HTTP Working Group revised HTTP and developed the second major version of the application protocol in the form of HTTP/2. In May 2015.

Advantage of HTTP/2

  1. HTTP/2 can send multiple requests for data in parallel over a single TCP connection. This is the most advanced feature of the HTTP/2 protocol because it allows you to download web files via ASync mode from one server. Most modern browsers limit TCP connections to one server.

  2. Multiplexing
  3. It uses header compression HPACK to reduce overhead, Hence it uses less network bandwidth.
  4. It allows servers to “push” responses proactively into client caches instead of waiting for a new request for each resource.
  5. It reduces additional round trip times (RTT), making our website load faster without any optimization.
  6. Domain sharding and asset concatenation are no longer needed with HTTP/2.
  7. Domain sharding is a technique used to increase the amount of simultaneously downloaded resources for a particular website by using multiple domains. This allows websites to be delivered faster to users as they do not have to wait for the previous set of resources to be downloaded before beginning the next set.

    Domain Sharding
    Domain Sharding

The original HTTP handling API in Java was written back when HTTP/1.1 was a new. Over time the uses of HTTP have evolved but the Java API has not kept pace with it. So for Java 9 a new API been introduced that is cleaner and clearer to use and which also adds support for HTTP/2.

HTTP API in Java 9

There are 3 new classes introduced to handle HTTP communication.


HttpClient handles the creation and send of requests.


HttpRequest is used to construct a request to be sent via the HttpClient.


HttpResponse holds the response from the request that has been sent.

Let’s create simple request.

Above three classes are from jdk.incubator.httpclient module, As we already know that we can not use internal APIs directly in Java 9, We have to create entry in module descriptor file. For more details on Java 9 modularity refer Java 9 Modularity and Jigsaw Project.



Output :

Output : Status Code : 200

In the above example, we are using the synchronous feature (i.e httpClient.send(…)), which will block until it has completed. This is not always desirable. So, we can make it asynchronous.


Calling… Performing Other Task Status Code:200

The request continues in the background allowing the code to continue on to other tasks. The returned CompletableFuture object can be used to determine whether the request has been completed yet and provide us access to the HttpResponse once it is complete.

That’s it for now in new HTTP2 API in Java 9, Hope you like it. Keep Learning and Sharing! :)

Leave a Reply

Your email address will not be published. Required fields are marked *