Server Push

One highlight feature of HTTP/2 is Server Push. Servlet 4.0 add PushBuilder to handle push.

An example of enabling Servlet Push.

@WebServlet(urlPatterns="")
@ServletSecurity(httpMethodConstraints={
        @HttpMethodConstraint(value="GET", transportGuarantee=CONFIDENTIAL) })
public class PushServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse res)
            throws IOException, ServletException {
        PushBuilder pushBuilder = req.newPushBuilder().
            path("main.css");
        pushBuilder.push();
        res.getWriter().println("<html><head><title>HTTP2 Test</title><link rel=\"stylesheet\" href=\"main.css\"></head><body>Hello Servlet Push!!!</body></html>");
    }
}

Run this application on Glassfish v5 in NetBeans IDE, it will open https://localhost:8181/servlet-push/ in browser instead of http://localhost:8080/servlet-push/.

There is a warning flag on the HTTPS icon of address bar, the SSL certificate in Glassfish v5 is not recognized by Firefox. Click it and add localhost to the exception and make Firefox trust it.

In the Network tab of developer tools, you will the main.css is initialized by Push.

Grab the source codes from my GitHub account, and have a try.

Last updated