- Java EE 8 and Angular
- Prashant Padmanabhan
- 496字
- 2021-07-02 19:22:33
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.
- 高手是如何做產(chǎn)品設(shè)計(jì)的(全2冊)
- 小程序?qū)崙?zhàn)視頻課:微信小程序開發(fā)全案精講
- 計(jì)算機(jī)圖形學(xué)編程(使用OpenGL和C++)(第2版)
- Flink SQL與DataStream入門、進(jìn)階與實(shí)戰(zhàn)
- MongoDB for Java Developers
- INSTANT MinGW Starter
- C語言程序設(shè)計(jì)實(shí)踐教程
- Unity Shader入門精要
- 面向?qū)ο蟪绦蛟O(shè)計(jì)(Java版)
- Python機(jī)器學(xué)習(xí)基礎(chǔ)教程
- AppInventor實(shí)踐教程:Android智能應(yīng)用開發(fā)前傳
- C專家編程
- Learning Material Design
- Access 2010數(shù)據(jù)庫教程(微課版)
- 企業(yè)級DevOps技術(shù)與工具實(shí)戰(zhàn)