官术网_书友最值得收藏!

Server Push

There's a new Push builder API that can be used for server push features. Armed with the server push ability, a server can push a resource to the client. This doesn't mean that there's no request needed in the first place. You need to obtain a PushBuilder from the request object and then use this for constructing a push request. Thus, there's always a request, based on which, the push feature is enabled.

A sample of this is as follows:

PushBuilder pushBuilder = httpServletRequest.newPushBuilder();

Once a pushBuilder instance is obtained from the request, you can use it to set the required URI path, which is to be used for sending the push request. A sample is shown here:

request.newPushBuilder()
.path(“/assests/images/product.png”)
.push();

Here, the paths beginning with / are considered absolute paths. Without the / prefix, the path would be considered to be relative to the context of the request used to create the instance of PushBuilder. While the short code shown here is handy, it must be used with caution since there's a possibility that the call to newPushBuilder() may return null if push is not supported.

If you are wondering how we put that newPushBuilder method on the request object, remember that Java 8 has default methods. So, the signature of the method looks like the following:

default PushBuilder newPushBuilder()

Building a push request involves setting the request method to GET and setting the path explicitly, as that won't be set by default. Calling the push method generates the push request from the server and sends it to the client, unless the push feature is not available for some reason. You may add headers or query strings to the push request by using the addHeader or queryString methods.

With the preceding code, the server will send a push to the client that made this request. The client may already have the resource and thus can tell the server that it has this cached from a previous request, and in turn will inform the server to not bother sending this resource over the wire. You might have guessed by now that it's the client who can dictate whether a resource should be pushed or not. Thus, the client can explicitly disable the server push.

Let's imagine we need to push the logo to the client from our servlet. Here's how we might write this code:

protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
PushBuilder pushBuilder = request.newPushBuilder();
if (pushBuilder != null) {
pushBuilder.path("images/logo.png")
.addHeader("Content-Type", "image/png")
.push();
}
try (PrintWriter writer = response.getWriter();) {
writer.write(new StringBuilder()
.append("<html><body>")
.append("<img src='images/logo.png'>")
.append("</body></html>").toString());
}
}

The Servlet API already provides Java SE 9 support for HTTP/2. There’s broadly just two classes, HttpRequestGroup and HttpRequest. These are just enough to solve the most common use cases but not exhaustive enough to replace a more established HTTP client library. It will support both the earlier HTTP/1 version along with the newer HTTP/2 version.

主站蜘蛛池模板: 双牌县| 宁陕县| 阳朔县| 江北区| 扎兰屯市| 涞源县| 嘉禾县| 称多县| 和林格尔县| 彰化市| 丰镇市| 绥芬河市| 丰城市| 新宁县| 安多县| 天津市| 崇文区| 宁武县| 弥渡县| 皮山县| 彭州市| 昭觉县| 扎鲁特旗| 康保县| 西充县| 云和县| 东乡| 安康市| 商河县| 祁连县| 海阳市| 奇台县| 罗山县| 水城县| 襄汾县| 平顶山市| 土默特左旗| 申扎县| 正定县| 衡南县| 家居|