Write your first Servlet 4.0 using Server Push
This article was originality pushed on my personal site.
If you are familiar with Servlet and Java EE 8, yes Servlet the old but gold implementation of request that handles HTTP requests.
Why HTTP/2?
The JSR 369[3] brings support to HTTP/2 using Servlet 4.0 through the PublishBuilder interface.
The HTTP/2 comes to supply some drawbacks on HTTP/1 such as[2]:
a. Binary instead of textual
b. Fully multiplexed
c. Same connection with the server
d. Header compression
e. Server to push.
This image below belongs to [2] and gives to us an overview of performance of HTTP/2.
Since the communication is fully multiplexed all the resources could be brought once instead of using one request for each item or add inline on the page to solve the problem using HTTP/1.
For it, let’s check item e., Server to push, and how to see the communication between the browser and the server.
2. Environment
In order to start off programming a server to push functionality make sure you have the following environment.
IDE — Eclipse 2020–06, to write and run the Servlet.
Application Server — Wildfly 20, which provides an HTTPS connection to the server.
Browser — Chrome, to see request details.
cURL — Command line tool to URL manipulation, optional
3. Hands-on
3.1 Eclipse plugin
Install the plugin JBoss Tools to take advantage of project configuration and execution.
3.2. Verify server configuration
The majority of browser or even all only runs server push over an HTTPS connection, so check you application server configuration file to be sure about it.
3.3. Create a new Servlet and a dynamic web project
Once it’s ok let’s create the project and a Servlet for it.
Click on Next or Finish since none should be configured, and the class has been created with the methods get and post.
Click on Next or Finish since none should be configure, and the class has been created with the methods get and post.
As-is when the path servlet/ExampleHttp2 is called the message “Served at: servlet” is written on the browser.
3.4. Use the PublishBuilder interface
To-be is using an HTTP/2 to bring a resource, for instance, a css, js, or image.
Firstly, from request use the method newPublishBuilder to instantiate an object of PublishBuilder, line 35.
This object should return null if the current connection doesn’t support server push, line 36.
Add the resources to be pushed, lines 38 and 39, and forward to a JSP page to render a text line 42.
3.5. Calling by browser
Now, when the Servlet receives a secure request, i.e. using the 8443, the Server push will be available otherwise not.
In a simple comparison with the two calls, the resources have been load faster on Server push mode. This is a simple example with few resources, but when it’s applied for a huge amount of requests and resources and other QOS this should be significant.
3.6. By cURL
cURL supports HTPP/2 by default[4], then any request by ports 8080 or 8443 should be works and the commands are
3.6.1. HTTP
curl -I — http2 http://localhost:8080/servlet/ExampleHttp2
3.6.2. HTTPS
curl -I — http2 https://localhost:8443/servlet/ExampleHttp2 -k
4. Code
The code is available on my GitHub.
5. References
[1] https://www.digitalocean.com/community/tutorials/http-1-1-vs-http-2-what-s-the-difference
[2] https://css-tricks.com/http2-real-world-performance-test-analysis/
[3] https://download.oracle.com/otndocs/jcp/java_ee-8-final-spec/index.html
[4] https://ec.haxx.se/http/http-http2
[5] https://github.com/luizgustavocosta/java-ee-8/tree/master/servlet