Java 9: HTTP/2 Improvements

DZone 's Guide to

Java 9: HTTP/2 Improvements

The long-awaited update to HTTP/1.1 is coming soon! Let's see what's new with HTTP/2, its drawbacks, and how it will impact your work.

Free Resource

Java 9 isn't just focusing on modularity. There is a large number of additional features and enhancements that are being released through the JEP process. This article describes the new HTTP 2 Support.

HTTP/2 is the newest version of the HTTP Protocol. The existing version's (HTTP 1.1) problems are eliminated in this newer version. HTTP/2 focuses on how data is framed and transported between server and client.

Advantages of HTTP/2

  1. In HTTP/1.1, we cannot have more than six connections open at a time, so every request has to wait for the others to complete.To avoid this, developers are used to doing workaround as a best practice. Those best practices include minifying, compressing, and zipping the files together, sprite images, etc. This can be eliminated by multiplexing in HTTP /2. This means that HTTP/2 can send multiple requests for data in parallel over a single TCP connection. 

  2. In HTTP/1.1, every request sent to the server will have the header's additional data, which increases bandwidth.This can be eliminated in HTTP/2.0 by having headers packed into one compressed block that will be sent as a unit and, once transmission is finished, the header blocks are decoded. It uses HPack for header compression.

  3. In an HTTP/1.1 environment, an HTML page is sent to the browser. The browser has to parse it and decide which assets are required, then request those assets from the server. This can be eliminated by Server Push in HTTP/2. It allows servers to push responses proactively to the client instead of waiting for the new request to process.

  4. Text is replaced by Binary in HTTP/2.0

  5. Domain sharding and asset concatenation are no longer needed with HTTP/2.

Drawbacks of HTTP/2.0

Many of us expected more features in the HTTP 2.0 protocol, but many were not included in the final version to support the backward compatibility.

  1. It is not super fast.

  2. It is vulnerable to attacks. (BREACH and CRIME).

  3. HTTP/2.0 decided to leave the encryption as it is in HTTP/1.1.

  4. HTTP/2.0 isn't very careful about cookie security.

Changes in Java 9 with HTTP/2.0

The original HTTP-handling API in Java was written back when HTTP/1.1 was a new, shiny thing. It was written to be agnostic, supporting many different types of connections using the same API. 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 has been introduced that is cleaner and clearer to use and that also adds support for HTTP/2.

The new API handles HTTP connections through three classes.

  1. HttpClient handles the creation and sending of requests.

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

  3. HttpResponse holds the response from the request that has been sent.
    java.net has introduced the HttpClient class as well as HttpRequest and HttpResponse

The new API helps HTTP connections be more easily maintained. It's faster and allows for a more responsive application without the need for third-party libraries.

import jdk.incubator.http.*;
import java.net.URI;
import java.io.IOException;
import java.util.Map;
public class HTTP2Demo {
    public static void main(String[] args) throws IOException {
        try {
            HttpClient httpClient = HttpClient.newHttpClient(); //Create a HttpClient
            HttpRequest httpRequest = HttpRequest.newBuilder().uri(new URI("https://www.google.com/")).GET().build(); //Create a GET request for the given URI
            Map < String, List < String >> headers = httpRequest.headers().map();
            headers.forEach((k, v) - > System.out.println(k + "-" + v));
            HttpResponse < String > httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandler.asString());
        } catch (Exception e) {
            System.out.println("message " + e);

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.

CompletableFuture > httpResponse = httpClient.sendAsync(httpRequest, HttpResponse.BodyHandler.asString());  

The above snippet will allow the code to continue with other tasks. The returned CompletableFuture object can be used to determine whether the request has been completed yet and provides access to the HttpResponse once it is complete. 

if(httpResponse.isDone()) {
} else {

The above snippet is used to check whether the response is completed. There are other utility methods for easy handling.

jdk 9 ,http 2 ,java ,tutorial ,asynchronous programming

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}