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 Video Library
Refcards
Trend Reports

Events

View Events Video Library

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

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

SBOMs are essential to circumventing software supply chain attacks, and they provide visibility into various software components.

Related

  • Actuator Enhancements: Spring Framework 6.2 and Spring Boot 3.4
  • How Spring Boot Starters Integrate With Your Project
  • A Practical Guide to Creating a Spring Modulith Project
  • Structured Logging in Spring Boot 3.4 for Improved Logs

Trending

  • How to Embed SAP Analytics Cloud (SAC) Stories Into Fiori Launchpad for Real-Time Insights
  • Stabilizing ETL Pipelines With Airflow, Presto, and Metadata Contracts
  • Serverless Machine Learning: Running AI Models Without Managing Infrastructure
  • Deploying LLMs Across Hybrid Cloud-Fog Topologies Using Progressive Model Pruning
  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.

By 
Naveen Katiyar user avatar
Naveen Katiyar
·
Oct. 04, 18 · Tutorial
Likes (11)
Comment
Save
Tweet
Share
21.6K 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.

Related

  • Actuator Enhancements: Spring Framework 6.2 and Spring Boot 3.4
  • How Spring Boot Starters Integrate With Your Project
  • A Practical Guide to Creating a Spring Modulith Project
  • Structured Logging in Spring Boot 3.4 for Improved Logs

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: