> For the complete documentation index, see [llms.txt](https://hantsy.gitbook.io/java-ee-8-by-example/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hantsy.gitbook.io/java-ee-8-by-example/servlet/servlet-push.md).

# 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.

```java
@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/](https://localhost:8080/servlet-push/).

![Server push](https://1346068621-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LmZrxBGc7qpcVDunhVV%2Fsync%2F60c9d32fd74f745ffe1968c1acfd426ed45928b1.png?generation=1615114442595570\&alt=media)

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.

![Server push](https://1346068621-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LmZrxBGc7qpcVDunhVV%2Fsync%2F91e6011c74d63c67779ae5aa8801858b44c1f3fa.png?generation=1615114442450610\&alt=media)

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

![Server Push](https://1346068621-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LmZrxBGc7qpcVDunhVV%2Fsync%2Faaea8f5ba3968bc3c497e0fc6161376b0227f023.png?generation=1615114442787620\&alt=media)

Grab the [source codes](https://github.com/hantsy/ee8-sandbox) from my GitHub account, and have a try.
