Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Introducing Servlet 4.0 Server Push Using Spring Boot 2.1

DZone's Guide to

Introducing Servlet 4.0 Server Push Using Spring Boot 2.1

Want to learn more about the new Servlet 4.0? Check out this post to learn more about using the Server Push in Spring Boot 2.1.

· Java Zone ·
Free Resource

How do you break a Monolith into Microservices at Scale? This ebook shows strategies and techniques for building scalable and resilient microservices.

In this article, we will be talking about Server Push Technology that is actually part of the HTTP/2 spec.

The most important feature of Servlet 4.0, due to HTTP/2, is the implementation of the server push capability. The concept behind this technique is that if the client/browser requests a certain resource, the server assumes, in advance, that some other related resources may also be requested soon. Because of this assumption, it pushes them into the cache (called 'cache push') before they are actually needed. For example, it is very much likely that when a webpage is loaded, it may eventually request a CSS file or another image. The server proactively starts pushing the bytes of these assets simultaneously, without the need for the client to make an explicit request.

Servlet 4.0 is part of Java EE 8, and hence, it would require Java 9+ along with Spring 5.x. Tomcat 9 supports HTTP/2, but it must be configured to use TLS. Tomcat 9 would be available only in Spring Boot 2.1.0, but it has not released yet, and we would be using milestone version in our article.

Enabling TLS support in Spring Boot is just a matter of few properties in the application.properties file. Just use the code below to enable it:

#enable/diable https
server.ssl.enabled=true
server.ssl.key-store: classpath:keystore.jks
server.ssl.key-store-password: tomcatssl
server.ssl.keyStoreType: JKS
server.ssl.keyAlias: tomcatssl
server.port=8443


In case, you are not aware of how to generate keystore.jks, please follow this link

To enable HTTP/2 support in Tomcat, the following property needs to be added.

server.http2.enabled=true


After configuring our Server with TLS, we are good to go for exposing our endpoint, which would be powered by HTTP/2 Push Technology.

@GetMapping(path = "/serviceWithPush")
public String serviceWithPush(HttpServletRequest request,PushBuilder pushBuilder) {
if (null != pushBuilder) {
    pushBuilder.path("resources/OnlineJavaPapers.png")
        .push();
}
return "index";
}


We will also configure another endpoint similar to the above that would basically use traditional pull technology and try to figure the difference on the client browser.

 @GetMapping(path = "/serviceWithoutPush")
    public String serviceWithoutPush() {
      return "index";
    }


Using the Firefox dev tool, we can confirm that for the serviceWithPush endpoint, only one request was initiated from the browser:

Whereas, when we call serviceWithoutPush, then there would be two requests triggered.

To conclude,  when using Server Push technology combined with proper caching technique, we can greatly enhance page load time and the overall responsiveness of our website.

Sample code for this can be found here.

How do you break a Monolith into Microservices at Scale? This ebook shows strategies and techniques for building scalable and resilient microservices.

Topics:
servlet 4.0 ,spring boot 2.0 ,server push ,performance and scalability ,java ,spring boot ,http/2 ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}