HTTP/2 server push support

HTTP/2 is the newest version of the HTTP protocol. It offers several advantages over HTTP 1.1. For example, with HTTP/2 there is a single connection between the browser and the server and this connection remains open until the user navigates to another page. HTTP/2 also offers multiplexing, meaning that several concurrent requests from the browser to the server are allowed. Additionally, HTTP/2 features server push, meaning that the server can send resources to the browser without the browser specifically having to request them.

HTTP/2 server push support was added to the servlet specification in version 4.0, released as part of Java EE 8. In this section, we'll see how we can write code to take advantage of HTTP/2's server push functionality. The following example illustrates how this can be done:

package net.ensode.javaee8book.servlet; 
 
import java.io.IOException; 
import java.io.PrintWriter; 
import javax.servlet.ServletException; 
import javax.servlet.annotation.WebServlet; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import javax.servlet.http.PushBuilder; 
 
@WebServlet(name = "ServletPushDemoServlet", urlPatterns = {"/ServletPushDemoServlet"}) 
public class ServletPushDemoServlet extends HttpServlet { 
    @Override 
    protected void doPost(HttpServletRequest request, 
HttpServletResponse response) throws ServletException, IOException { PushBuilder pushBuilder = request.newPushBuilder(); if (pushBuilder != null) { //We know the browser is going to need the image //so we push it before it even requests it. //We could do the same for Javascript files, CSS, etc. pushBuilder.path("images/david_heffelfinger.png").
addHeader("content-type", "image/png").

push();
response.sendRedirect("response.html"); } else { //Gracefully handle the case when the browser does not
support HTTP/2. } } }

We can push resources to the browser via the new PushBuilder interface, introduced in version 4 of the servlet specification. We can obtain an instance of a class implementing PushBuilder by invoking the new PushBuilder() method on the instance of HttpServletRequest we get as a parameter in our doPost() method.

As its name implies, the PushBuilder interface implements the Builder pattern, meaning that most of its methods return a new instance of PushBuilder we can use, allowing us to conveniently chain together method invocations.

We indicate the path of the resource we'd like to push to the browser by invoking the appropriately named path() method from PushBuilder. This method takes a single String argument indicating the path of the resource to push. Paths beginning with a forward slash (/) indicate an absolute path, all other paths indicate a path relative to our application's context root.

Once we have specified the path of our resource, we can optionally set some HTTP headers; in our case, we are pushing an image in PNG format, therefore we set the content type as appropriate.

Finally, we invoke the push() method on our PushBuilder instance to actually push our resource to the browser.

What we accomplished with our example was pushing a resource to the browser before the browser submitted a request for it; this task was impossible before the HTTP/2 protocol was released.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.62.168