Spring 5 and Servlet 4 – The PushBuilder – Spring 5和Servlet 4 – The PushBuilder

最后修改: 2018年 1月 24日

1. Introduction


The Server Push technology — part of HTTP/2 (RFC 7540) — allows us to send resources to the client proactively from the server-side. This is a major change from HTTP/1.X pull-based approach.

服务器推送技术–HTTP/2的一部分(RFC 7540)- 允许我们从服务器端主动向客户端发送资源。这是对HTTP/1.X基于拉动的方法的一个重大改变。

One of the new features that Spring 5 brings – is the server push support that comes with Jakarta EE 8 Servlet 4.0 API. In this article, we’ll explore how to use server push and integrate it with Spring MVC controllers.

Spring 5带来的新功能之一–是Jakarta EE 8 Servlet 4.0 API中的服务器推送支持。在这篇文章中,我们将探讨如何使用服务器推送并将其与Spring MVC控制器集成

2. Maven Dependency


Let’s start by defining dependencies we’re going to use:



The most recent versions of spring-mvc and servlet-api can be found on Maven Central.


3. HTTP/2 Requirements


To use server push, we’ll need to run our application in a container that supports HTTP/2 and the Servlet 4.0 API. Configuration requirements of various containers can be found here, in the Spring wiki.

要使用服务器推送,我们需要在支持 HTTP/2 和 Servlet 4.0 API 的容器中运行我们的应用程序。各种容器的配置要求可在Spring wiki中找到。

Additionally, we’ll need HTTP/2 support on the client-side; of course, most current browsers do have this support.


4. PushBuilder Features


The PushBuilder interface is responsible for implementing server push. In Spring MVC, we can inject a PushBuilder as an argument of the methods annotated with @RequestMapping.

PushBuilder接口负责实现服务器推送。在Spring MVC中,我们可以注入一个PushBuilder作为@RequestMapping注释的方法的参数。

At this point, it’s important to consider that – if the client doesn’t have HTTP/2 support – the reference will be sent as null.


Here is the core API offered by the PushBuilder interface:


  • path (String path) – indicates the resource that we’re going to send
  • push() – sends the resource to the client
  • addHeader (String name, String value) – indicates the header that we’ll use for the pushed resource

5. Quick Example


To demonstrate the integration, we’ll create the demo.jsp page with one resource — logo.png:


<%@ page language="java" contentType="text/html; charset=UTF-8"
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>PushBuilder demo</title>
    <span>PushBuilder demo</span>
    <img src="<c:url value="/resources/logo.png"/>" alt="Logo" 
      height="126" width="411">

We’ll also expose two endpoints with the PushController controller — one that uses server push and another that doesn’t:


public class PushController {

    @GetMapping(path = "/demoWithPush")
    public String demoWithPush(PushBuilder pushBuilder) {
        if (null != pushBuilder) {
        return "demo";

    @GetMapping(path = "/demoWithoutPush")
    public String demoWithoutPush() {
        return "demo";

Using the Chrome Development tools, we can see the differences by calling both endpoints.


When we call the demoWithoutPush method, the view and the resource is published and consumed by the client using the pull technology:


Demo without Push
When we call the demoWithPush method, we can see the use of the push server and how the resource is delivered in advance by the server, resulting in a lower load time:

Demo without Push

Demo with Push
The server push technology can improve the load time of the pages of our applications in many scenarios. That being said, we do need to consider that, although we decrease the latency, we can increase the bandwidth – depending on the number of resources we serve.

Demo with Push

It’s also a good idea to combine this technology with other strategies such as Caching, Resource Minification, and CDN, and to run performance tests on our application to determine the ideal endpoints for using server push.


6. Conclusion


In this quick tutorial, we saw an example of how to use the server push technology with Spring MVC using the PushBuilder interface, and we compared the load times when using it versus the standard pull technology.

在这个快速教程中,我们看到了一个如何使用PushBuilder接口在Spring MVC中使用服务器推送技术的例子,我们还比较了使用它与标准拉动技术时的加载时间。

As always, the source code is available over on GitHub.