Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

First Look at HTTP/2 Server Push in Java Servlet 4.0 Specification

DZone's Guide to

First Look at HTTP/2 Server Push in Java Servlet 4.0 Specification

HTTP/2 promises increased performance and functionality for Java web apps. With that in mind, take a dive into Server Pushes and how they can improve your work.

· Java Zone
Free Resource

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

Servlet is one of the most important specifications in the Java EE ecosystem. It is used on millions of applications and hundreds of web frameworks. Because the Java Servlet specification is highly tied to the HTTP protocol, it is updated following HTTP protocol changes.

HTTP 2 comes after the widely used HTTP 1.1 protocol, and its new features bring much better performance to the web. For example, the HTTP/2 Server Push feature gives a way to push web resources from server to client. In that way, server applications are able to send content(s) at the beginning of the initial requests, instead of waiting for clients' requests. It brings an efficient way to transfer resources in HTTP 2-enabled web applications.

The PushBuilder interface is designed for the HTTP/2 Server Push feature in the JSR 369 - Java Servlet 4.0 Specification. It looks like below:

javax.servlet.http.PushBuilder:

public interface PushBuilder {

    public PushBuilder method(String method);
    public PushBuilder queryString(String queryString);
    public PushBuilder sessionId(String sessionId);
    public PushBuilder setHeader(String name, String value);
    public PushBuilder addHeader(String name, String value);
    public PushBuilder removeHeader(String name);
    public PushBuilder path(String path);
    public void push();
    public String getMethod();
    public String getQueryString();
    public String getSessionId();
    public Set getHeaderNames();
    public String getHeader(String name);
    public String getPath();
}


For example, you can push a resource in a Servlet class like below:

@WebServlet(value = {"/http2"})
public class Http2Servlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) {

        PushBuilder pushBuilder = req.newPushBuilder(); (1)
        pushBuilder
                .path("images/kodedu-logo.png")
                .addHeader("content-type", "image/png")
                .push(); (2)

        try(PrintWriter respWriter = resp.getWriter();){
            respWriter.write("<html>" +
                    "<img src='images/kodedu-logo.png'>" + (3)
                    "</html>");
        }

    }
}
1 Returns a new PushBuilder instance if HTTP/2 enabled or null if it is not enabled/supported.
2 Will push kodedu-logo.png to client
3 <img element will not initiate a request for the image


Above, when you do a GET request on this Servlet, HTML content and the pushed image will be accessible on the browser almost at the same time. So, <img src='..'.. would not initiate a new request for the resource because it had already been sent to the client.

If you want to try this feature;

  1. Download JDK-8u131+ http://www.oracle.com/technetwork/java/javase/downloads/index-jsp-138363.html, previous versions will not work.
  2. Download the latest Glassfish build http://download.oracle.com/glassfish/5.0/nightly/latest-glassfish.zip
  3. Clone my simple app and build it:
    git clone https://github.com/rahmanusta/servlet4-push
    cd servlet4-push
    mvn clean install -DskipTests=true
  4. Deploy the app to the Glassfish
    cd target
    // asadmin create-domain domain1
    asadmin start-domain domain1
    asadmin deploy Servlet4Push.war
  5. Open the URL https://localhost:8181/Servlet4Push/http2 and watch the network traffic.
http2 servlet push api

If you want to look into the spec documentation, you can find it here. (See 3.8 HTTP/2 Server Push),

Hope to see you again!

Download Building Reactive Microservices in Java: Asynchronous and Event-Based Application Design. Brought to you in partnership with Red Hat

Topics:
java ,java ee 8 ,servlet 4 ,http 2 ,tutorial

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}