上QQ阅读APP看书,第一时间看更新
How it works...
A web application running under HTTP/1.0 sends a request to the server when it finds references for an image file, CSS file, and any other resources needed to render a web page.
With HTTP/2.0 you still can do it, but now you can do better: the server can now push the resources beforehand, avoiding unnecessary new requests, decreasing the server load, and improving performance.
In this recipe, our resources are represented by the following:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="resources/style.css">
<script src="resources/functions.js"></script>
And the push happens at this part of our filter:
HttpServletRequest httpReq = (HttpServletRequest)request;
PushBuilder builder = httpReq.newPushBuilder();
if (builder != null){
builder
.path("resources/javaee-logo.png")
.path("resources/style.css")
.path("resources/functions.js")
.push();
System.out.println("Resources pushed");
}
So when the browser needs those resources to render the web page, they are already available.