Java EE 8: What Servlet 4 and HTTP2 Mean
-
Upload
alex-theedom -
Category
Technology
-
view
199 -
download
6
Transcript of Java EE 8: What Servlet 4 and HTTP2 Mean
Alex Theedom @readlearncode
Java EE 8: What Servlet 4.0 and
HTTP/2 mean?
@readlearncode readlearncode.com
Who am I?
Alex Theedom
Author Trainer
Blogger
Speaker
What’s on?
@readlearncode readlearncode.com
• Why Do We Need HTTP/2• Work-Arounds to HTTP1.1• HTTP Sockets• Topline HTTP/2 Features• Servlet 4.0 Features• Server Support• Status Update• Q&A
What’s on?
Why Do We Need HTTP/2?
@readlearncode readlearncode.com
• Increase perceived performance of web• HTTP protocol not suitable• Since April 2011 average web page size • increased by over 300%
• The problem with HTTP/1.1
300%Source: HTTPArchive.com
Why Do We Need HTTP/2? The Goal of HTTP/2
@readlearncode readlearncode.com
• Requests resources in parallel HTTP 1.0
• One request per TCP connection
• HTTP1.1 Pipelining: multiple requests
• Responds in sequence
• Delay causes head-of-line blocking
How a browser loads a webpage?
open
close
client serverno pipelining
index.html
style_1.css
logo.jpg
open
close
client serverpipelining
time
index.html
style_1.csslogo.jpg
Why Do We Need HTTP/2?
Work-Arounds
@readlearncode readlearncode.com
• Multiple connections
• Acceptable but has issues
• TCP sockets expensive
• Browser max connections
Work-Arounds to HTTP1.1Solution to Head-Of-Line Blocking
open
close
client server
connection 1style_1.css
open
close
client server
connection 2javaScript_1.js
open
close
client server
connection 3image_1.png
@readlearncode readlearncode.com
Work-Arounds to HTTP1.1CSS/JavaScript File Concatenation
background.cssheader.cssmenu.cssstyle.css
@readlearncode readlearncode.com
Work-Arounds to HTTP1.1CSS and JavaScript Inlining
…
@readlearncode readlearncode.com
• Embed image in web page
• Base 64 encoded
• Time spent decoding
• Caching difficult
Work-Arounds to HTTP1.1Inlined Assets
@readlearncode readlearncode.com
• One image file consists of many smaller images
Work-Arounds to HTTP1.1Image Sprite Sheet
Image sprites from Amazon, Google and Facebook.
@readlearncode readlearncode.com
Work-Arounds to HTTP1.1Domain Sharding
web pagey.example.com
x.example.com
server 2
server 1
logo.jpg
icon.jpg
header.css
menu.css
HTTP Sockets
@readlearncode readlearncode.com
• Not much specified
• Throw away resources
• No maximum open sockets
HTTP SocketsWhat HTTP1.1 Says About Sockets
@readlearncode readlearncode.com
• Much is specified
• Scares resources
• Ideally only open one socket
HTTP SocketsWhat HTTP/2 Says About Sockets
@readlearncode readlearncode.com
HTTP SocketsAll connections now operate as one connection
open
close
client server
open
close
client server
open
close
client server
open
close
client server open
close
client serveropen
close
client server
Topline HTTP/2 Features
@readlearncode readlearncode.com
• HTTP/2 is comprised of two specifications
• Hypertext Transfer Protocol version 2 - RFC7540
• HPACK - Header Compression for HTTP/2 - RFC7541
• Binary Protocol Based on Frames
Topline HTTP/2 FeaturesWhat’s new
@readlearncode readlearncode.com
• Request/Response Multiplexing
• Binary Framing
• Header Compression
• Stream Prioritization
• Server Push
Topline HTTP/2 FeaturesFeatures
@readlearncode readlearncode.com
• Most important feature• Request and response is multiplexed • Fully bi-directional communication
• Concepts• Connection - A TCP socket• Stream – A channel of communication• Message – A request/response and control message• Frame –The smallest unit within a communication
• Resolves head-of-line blocking• Communication broken down into frames• Frames facilitate interweaving the logical stream
Topline HTTP/2 FeaturesRequest/Response Multiplexing
@readlearncode readlearncode.com
• Hierarchical structure of logical communication blocks
Topline HTTP/2 FeaturesRequest/Response Multiplexing
connectionstream
frame frame frameframe frame frame
message
stream
frame frame frameframe frame frame
messageframe frame frameframe frame frame
message
stream
frame frame frameframe frame frame
messageframe frame frameframe frame frame
messageframe frame frameframe frame frame
message
@readlearncode readlearncode.com
• Request/response multiplexing
• Interweave the logical stream over a single TCP
• Stream 3 send header then body and server responds with stream 2 before it receives completed stream 3
Topline HTTP/2 FeaturesRequest/Response Multiplexing
STREAM 1HEADERS
STREAM 3DATA
STREAM 1DATA
STREAM 2HEADERS
STREAM 3HEADERS
STREAM 2DATA
browser
server
@readlearncode readlearncode.com
• Decomposition of the frame
• Type fields can be• HEADERS corresponds to the HTTP headers• DATA corresponds to the HTTP request body• PUSH_PROMISE server notifies of push intent • RST_STREAM notifying error, client rejects push• PRIORITY specifies stream priority• SETTING, PING, GOAWAY, WINDOW_UPDATE, CONTINUATION
Topline HTTP/2 FeaturesBinary Framing
LENGTH (24)TYPE (8) FLAGS (8)
R STREAM IDENTIFIER (31)FRAME PAYLOADS (0.. n)
@readlearncode readlearncode.com
• Mapping the HTTP Request to Frames
Topline HTTP/2 FeaturesHeader Compression
HTTP request Header FrameGET /index.html HTTP/1.1Host: example.comAccept: text/html
HEADERS - END_STREAM + END_HEADERS :method: GET :scheme: http :path: /index.html :authority: example.com accept: text/html
@readlearncode readlearncode.com
• Mapping the HTTP Response to Frames
Topline HTTP/2 FeaturesHeader Compression
HTTP response FramesHTTP/1.1 200 OKContent-Length: 11Content-Type: text/htmlMay The Force Be With You
HEADERS - END_STREAM + END_HEADERS :status: 200 content-length: 11 content-type: text/htmlDATA + END_STREAMMay The Force Be With You
@readlearncode readlearncode.com
Topline HTTP/2 FeaturesHPACK header compression
HTTP Request 1:method GET:scheme https:host example.com:path /index.html:authority example.org:accept text/htmluser-agent Mozilla/5.0
HTTP Request 2:method GET:scheme https:host example.com:path /info.html:authority example.org:accept text/htmluser-agent Mozilla/5.0
HEADERS frame (Stream 1):method GET:scheme https:host example.com:path /index.html:authority example.org:accept text/htmluser-agent Mozilla/5.0
HEADERS frame (Stream 3):path /info.html
@readlearncode readlearncode.com
• Attach priority information to streams
• Priority located in the header frame or the priority frame
• Only a suggestion to the server
Topline HTTP/2 FeaturesStream Prioritization
B D C
A
2 14 10
B C
A
4 8
@readlearncode readlearncode.com
• Eliminate the need for resource inlining
• The sever can proactively send resources to the client
• Client can reject PUSH_PROMISE by responding RST_STREAM
Topline HTTP/2 FeaturesServer Push
Servlet 4.0 Features
@readlearncode readlearncode.com
Servlet 4.0 FeaturesServer Push
• Servlet 4.0 appropriate abstraction
• Most visible improvements in servlets
• Best place to know what resources a request needs
• Not a replacement for websockets
@readlearncode readlearncode.com
Servlet 4.0 FeaturesPushBuilder
@readlearncode readlearncode.com
• Browser requests index.html• Server discovers need for css and js• Get PushBuilder from HTTP request• Set path to css and invoke push• Set path to js and invoke push• Then responds with index.html• The PushBuilder can be reused
Servlet 4.0 FeaturesTypical Journey
@readlearncode readlearncode.com
Servlet 4.0 FeaturesPushBuilder Dive Deeper
• Constructed with request method set to GET by default• Conditional, range, expectation, authorization and request
headers are removed• Cookies are only added if the maxAge has not expired• Only required setting is the URI path to the resource• Must be set before every call to push()
@readlearncode readlearncode.com
• Simple usage
Servlet 4.0 FeaturesServlets and ServerPush
@readlearncode readlearncode.com
Servlet 4.0 FeaturesFilters and Server Push
@readlearncode readlearncode.com
• SETTINGS_ENABLE_PUSH clients disable server push
• RST_STREAM rejects cached resources
• Servlet containers must honour request to not receive
Servlet 4.0 FeaturesDisable/Reject Server Push
@readlearncode readlearncode.com
• Perfectly backward compatible
• Rework to take advantage of Server Push
• Consider removing frontend HTTP 1.1 work-arounds
Servlet 4.0 FeaturesBackward Compatible
Server Support
@readlearncode readlearncode.com
• GlassFish 5.0 Reference implementation
• Payara 5.0Has a branch for Java EE 8 development
• Jetty Stable-9 (9.4.2.v20170220)org.eclipse.jetty.servlets.PushCacheFilter/
PushBuilder
Server ImplementationServlet Support
@readlearncode readlearncode.com
• WildFly 10 (Undertow)
Initial PushBuilder support implemented in Undertow master
• Tomcat 9.0.0.M17Initial PushBuilder Supports Servlets 4.0's
PushBuilder in the javax.servlets.http package
• Netty 4.1HTTP/2 implementation takes full advantage of
headline features
Server ImplementationServlet Support
Status Update
@readlearncode readlearncode.com
• Currently in the early draft review
• Public review draft in Quarter 3 2017
• Issue Tracker: java.net/jira/browse/SERVLET_SPEC
• JCP Specification: jcp.org/en/jsr/detail?id=369
• Twitter: @servlet_spec
Status UpdateJSP 369: Servlet Specification
@readlearncode readlearncode.com
Q & A
Alex Theedom @readlearncode
Java EE 8: What Servlet 4.0 and
HTTP/2 mean?