Learn about Server Push in Java

Server Push is a new performance enhancement feature for Web applications defined in HTTP/2 standards. It allows us to push images, CSS files, Javascript files, and other resources to the client before requests from the client are processed. So, as soon as the response from the server to the client, these resources are available in the browser cache and can be used. Server Push is included in the release of Java Servlet 4.x. In this tutorial, we will learn together about how Push Server in Java.

First, I will create a project for a web application in Eclipse as an example:

Learn about Server Push in Java

To see the benefits of Server Push in Java Servlet 4.x, now we will create a normal Servlet first:

Learn about Server Push in Java

with the following content:

In the HelloServlet above, as you see, in addition to the “Hello World from Huong Dan Java”, I also return to the client a picture is the logo of the Huong Dan Java. This logo is located in the WebContent folder of the project:

Learn about Server Push in Java

Now, if you run the application with Tomcat 9 or above, you will see the following results:

Learn about Server Push in Java

If you use Chrome Developer Tools by selecting More Tools, then select Developer Tools, then select the Networks tab. You will see the following:

Learn about Server Push in Java

Now, let’s take a look at the Server Push feature of Java Servlet 4.x.

In the Java Servlet 4.x, the PushBuilder interface is added to implement the HTTP/2 Server Push feature. The content of this interface is as follows:

We can derive the PushBuilder object in our Servlet from the HttpServletRequest object using this object’s newPushBuilder() method:

Once we have the PushBuilder object, we can use it to push all the resources in our web application such as images, CSS files, Javascript files to the client.

First, you need to declare the path of the resource that you want to push to the client using the path() method of the PushBuilder object:

You can call multiple path() methods to push multiple resources at the same time.

After declaring all the resources that need to be pushed, you should call the push() method of the PushBuilder object to start pushing the resource to the client.

After you have pushed the resources, you can continue processing to return the entire content of the site. The HelloServlet instance can be rewritten as follows:

Before running our example with Server Push, one more thing I want to talk with you guys is that the Server Push only works with secure connections like HTTPS connection and if the client disables the server push the server, Server Push will not work. In these cases, if you use the HttpServletRequest object to get the PushBuilder object, the result will be null.

So, to run this example, you have to configure Tomcat to support HTTP/2 with the HTTPS protocol. To do this, open the server.xml file in the Servers project:

Learn about Server Push in Java

then add a Connector with the following content:

See also Configure Tomcat support HTTP/2.

Now, just run the example again to see the result.

Result:

Learn about Server Push in Java

As you can see, at this time, the protocol is h2 (HTTP/2) and if you notice the Initiator column of the logo.png file, you will see it is using Server Push. My example uses only a small image so you will not see the difference, but for a real website, there are hundreds of thousands of resources, you will clearly see the benefits of Server Push.

 

Chia sẽ bài viết này ...Share on Facebook
Facebook
0Tweet about this on Twitter
Twitter
Share on LinkedIn
Linkedin

Add Comment