rbelfils / servlet4-push

Servlet 4 PushBuilder Demo

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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 more 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 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. Don’t forget to use HTTPS, it won’t work if you use HTTP (details here: https://http2.github.io/faq/#what-does-http2-do-to-improve-security).

http2 servlet push api

Also you can run the demo with Docker

docker run -it --rm -d -p 8181:8181 rahmanusta/servlet4-push

Here is the Dockerfile that just uses the official Glassfish Docker image and moves the Servlet4Push.war to Glassfish’s autodeploy folder. After container run, it will deploy the demo app in just seconds!

Dockerfile
FROM oracle/glassfish:nightly-web
MAINTAINER Rahman Usta<rahmanusta@kodedu.com>
COPY ./target/Servlet4Push.war /glassfish5/glassfish/domains/domain1/autodeploy
EXPOSE 8080:8080
EXPOSE 8181:8181
EXPOSE 4848:4848

If you want to look into the spec documentation, you can find it here http://download.oracle.com/otndocs/jcp/servlet-4-pr-spec/index.html (See 3.8 HTTP/2 Server Push)

Hope to see you again.

About

Servlet 4 PushBuilder Demo


Languages

Language:Java 100.0%