Overview
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
- 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.
- It uses header compression HPACK to reduce overhead, Hence it uses less network bandwidth.
- It allows servers to “push” responses proactively into client caches instead of waiting for a new request for each resource.
- It reduces additional round trip times (RTT), making our website load faster without any optimization.
- Domain sharding and asset concatenation are no longer needed with HTTP/2.

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.

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
HttpClient handles the creation and send of requests.
HttpRequest
HttpRequest is used to construct a request to be sent via the HttpClient.
HttpResponse
HttpResponse holds the response from the request that has been sent.
Let’s create simple request.
Above three classes are from
module-info.java
0 1 2 3 4 |
module A { requires jdk.incubator.httpclient; } |
HTTP2Test.java
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import java.io.IOException; import java.net.URI; import jdk.incubator.http.HttpClient; import jdk.incubator.http.HttpRequest; import jdk.incubator.http.HttpResponse; public class HTTP2Test { public static void main(String[] args) throws IOException { try { HttpClient httpClient = HttpClient.newHttpClient(); //Create a HttpClient HttpRequest httpRequest = HttpRequest. newBuilder().uri(new URI("http://www.codenuclear.com")).GET().build(); HttpResponse <String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandler.asString()); System.out.println(httpResponse.statusCode()); } catch (Exception e) { System.out.println("message " + e); } } } |
Output :
In the above example, we are using the synchronous feature (i.e
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
import java.io.IOException; import java.net.URI; import java.util.concurrent.CompletableFuture; import jdk.incubator.http.HttpClient; import jdk.incubator.http.HttpRequest; import jdk.incubator.http.HttpResponse; public class HTTP2Test{ public static void main(String[] args) throws IOException { try { HttpClient httpClient = HttpClient.newHttpClient(); //Create a HttpClient HttpRequest httpRequest = HttpRequest. newBuilder().uri(new URI("http://www.codenuclear.com")).GET().build(); System.out.println("Calling..."); CompletableFuture<HttpResponse<String>> httpResponse = httpClient.sendAsync(httpRequest, HttpResponse.BodyHandler.asString()); //Send the request asynchronously System.out.println("Performing Other Task"); if(httpResponse.isDone()) { System.out.println("Status Code:"+httpResponse.get().statusCode()); } else { httpResponse.cancel(true); } } catch (Exception e) { System.out.println("message " + e); } } } |
Output
The request continues in the background allowing the code to continue on to other tasks. The returned
That’s it for now in new HTTP2 API in Java 9, Hope you like it. Keep Learning and Sharing! :)