DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. First Look at HTTP/2 Server Push in Java Servlet 4.0 Specification

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.

Rahman Usta user avatar by
Rahman Usta
·
Apr. 27, 17 · Tutorial
Like (15)
Save
Tweet
Share
27.16K Views

Join the DZone community and get the full member experience.

Join For Free

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

On the other hand, you can run the demo with Docker with the following command;

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!

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

Then open the link https://localhost:8181/Servlet4Push/http2 and watch the network traffic.

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!

push Java (programming language)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • AWS Cloud Migration: Best Practices and Pitfalls to Avoid
  • Select ChatGPT From SQL? You Bet!
  • 5 Factors When Selecting a Database
  • Key Considerations When Implementing Virtual Kubernetes Clusters

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: