DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Frameworks
  4. Introducing Servlet 4.0 Server Push Using Spring Boot 2.1

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.

Naveen Katiyar user avatar by
Naveen Katiyar
·
Oct. 04, 18 · Tutorial
Like (11)
Save
Tweet
Share
20.25K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Spring Framework Spring Boot Push technology

Published at DZone with permission of Naveen Katiyar, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Top 5 PHP REST API Frameworks
  • The Importance of Delegation in Management Teams
  • Beginners’ Guide to Run a Linux Server Securely
  • Asynchronous HTTP Requests With RxJava

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: