HTTP 
2 
Comes 
to 
Java 
What 
Servlet 
4.0 
Means 
to 
You 
Ed 
Burns 
and 
Shing 
Wai 
Chan 
Java 
EE 
Specifica=on 
Team 
Oracle 
Copyright 
© 
2014, 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
|
Safe 
Harbor 
Statement 
The 
following 
is 
intended 
to 
outline 
our 
general 
product 
direc=on. 
It 
is 
intended 
for 
informa=on 
purposes 
only, 
and 
may 
not 
be 
incorporated 
into 
any 
contract. 
It 
is 
not 
a 
commitment 
to 
deliver 
any 
material, 
code, 
or 
func=onality, 
and 
should 
not 
be 
relied 
upon 
in 
making 
purchasing 
decisions. 
The 
development, 
release, 
and 
=ming 
of 
any 
features 
or 
func=onality 
described 
for 
Oracle’s 
products 
remains 
at 
the 
sole 
discre=on 
of 
Oracle. 
Copyright 
© 
2014, 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
3
Our 
Plan 
for 
Your 
Time 
Investment 
Why 
HTTP 
2? 
HTTP 
2 
Big 
Features 
How 
Servlet 
Might 
Expose 
These 
Features 
Java 
SE 
9 
Support 
for 
HTTP 
2 
Summary 
and 
Current 
Status 
Copyright 
© 
2014, 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
1 
2 
3 
4 
5 
S 
4
Our 
Plan 
for 
Your 
Time 
Investment 
Why 
HTTP 
2? 
HTTP 
2 
Big 
Features 
How 
Servlet 
Might 
Expose 
These 
Features 
Java 
SE 
9 
Support 
for 
HTTP 
2 
Summary 
and 
Current 
Status 
Copyright 
© 
2014, 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
1 
2 
3 
4 
5 
S 
5
. 
. 
. 
Copyright 
© 
2014, 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
Why 
HTTP 
2? 
A 
Real 
Life 
Example 
S 
index.html 
style1.css 
style2.css 
script1.js 
. 
. 
. 
script9.js 
pic1.jpg 
pic8.jpg 
photo1.png 
photo2.png 
. 
. 
.
Copyright 
© 
2014, 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
Why 
HTTP 
2? 
Problems 
in 
HTTP 
1.1 
• HTTP 
Pipelining 
• Head-­‐of-­‐Line 
blocking 
S 
style1.css 
style2.css 
Client 
Server 
index.html 
index.html 
style1.css 
style2.css 
script1.js 
script2.js 
. 
. 
. 
script1.js 
script2.js 
. 
. 
.
Copyright 
© 
2014, 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
Why 
HTTP 
2? 
Problems 
in 
HTTP 
1.1 
• Inefficient 
use 
of 
TCP 
sockets 
S 
Client 
Server 
Client 
Server 
Client 
Server 
Client 
Server 
Client 
Server 
Client 
Server
Copyright 
© 
2014, 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
Why 
HTTP 
2? 
What 
is 
an 
opEmizaEon? 
• Much 
of 
what 
we 
do 
in 
web-­‐apps 
is 
a 
hack 
to 
work 
around 
shortcomings 
in 
HTTP 
1.1 
– File 
concatena=on 
and 
image 
sprites 
– Domain 
sharding 
– Inlined 
assets 
E 
9
File 
Concatena=on 
and 
Image 
Sprites 
TCP 
Efficiency 
Improves 
with 
Larger 
Files 
Copyright 
© 
2014, 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
E 
10
File 
Concatena=on 
and 
Image 
Sprites 
TCP 
Efficiency 
Improves 
with 
Larger 
Files 
Copyright 
© 
2014, 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
E 
11 
• Modern 
web 
page 
now 
consists 
of 
more 
than 
90 
resources 
fetched 
from 
15 
dis=nct 
hosts 
• Solu=on: 
– Just 
work 
around 
it 
by 
shoving 
more 
than 
one 
logical 
file 
into 
one 
physical 
file. 
– Seminal 
ar=cle: 
A 
List 
Apart 
hPp://alistapart.com/arEcle/sprites 
– Useful 
tool: 
SpritePad 
hPp://spritepad.wearekiss.com/
File 
Concatena=on 
and 
Image 
Sprites 
TCP 
Efficiency 
Improves 
with 
Larger 
Files 
Copyright 
© 
2014, 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
E 
12 
.ic-AerospaceAndDefense-wht-on-gray, .ic-AerospaceAndDefense-wht-on-red, 
.ic-Airline-wht-on-gray, .ic-Airline-wht-on-red{ 
background: url(sprites.png) no-repeat; 
} 
.ic-AerospaceAndDefense-wht-on-gray{ 
background-position: 0 0; 
width: 80px; 
height: 80px; 
} 
.ic-AerospaceAndDefense-wht-on-red{ 
background-position: -81px 0; 
width: 80px; 
height: 80px; 
} 
.ic-Airline-wht-on-gray{ 
background-position: 0 -80px ; 
width: 80px; 
height: 80px; 
} 
.ic-Airline-wht-on-red{ 
background-position: -81px -79px ; 
width: 80px; 
height: 80px; 
}
Domain 
Sharding 
Split 
page 
resources 
across 
several 
hosts 
to 
work 
around 
browser 
limits 
Copyright 
© 
2014, 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
13
Copyright 
© 
2014, 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
Inlined 
Assets 
Base64 
Encoding 
Will 
Never 
Die 
• data 
URLs 
• <img 
src="data:image/gif;base64,R0lGODlhEAAOALMAAOazToeHh0tLS/ 
7LZv/0jvb29t/f3//Ub/ 
/ge8WSLf/rhf/3kdbW1mxsbP//mf/// 
yH5BAAAAAAALAAAAAAQAA4…" 
/> 
E 
14
Our 
Plan 
for 
Your 
Time 
Investment 
Why 
HTTP 
2? 
HTTP 
2 
Big 
Features 
How 
Servlet 
Might 
Expose 
These 
Features 
Java 
SE 
9 
Support 
for 
HTTP 
2 
Summary 
and 
Current 
Status 
Copyright 
© 
2014, 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
1 
2 
3 
4 
5 
E 
15
• Request/Response 
Copyright 
© 
2014, 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
mul=plexing 
• Binary 
Framing 
• Stream 
Priori=za=on 
• Server 
Push 
• Header 
Compression 
• Upgrade 
from 
HTTP 
1.1 
• ALPN 
(or 
NPN) 
• 101 
Switching 
Protocols 
16 
HTTP/2 
Big 
Ticket 
Feature 
Review 
E
HTTP/2 
Request 
Response 
Mul=plexing 
Copyright 
© 
2014, 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
• Fully 
bi-­‐direc=onal 
• Enabled 
by 
defining 
some 
terms 
– Connec&on 
A 
TCP 
socket 
– Stream 
A 
“channel” 
within 
a 
connec=on 
– Message 
A 
logical 
message, 
such 
as 
a 
request 
or 
a 
response 
– Frame 
The 
smallest 
unit 
of 
communica=on 
in 
HTTP/2. 
17 
Lets 
you 
do 
more 
things 
with 
a 
single 
TCP 
connecEon 
S
HTTP/2 
Request 
Response 
Mul=plexing 
Copyright 
© 
2014, 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
18 
ConnecEons, 
Streams, 
Messages, 
Frames 
S
HTTP/2 
Request 
Response 
Mul=plexing 
Copyright 
© 
2014, 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
• Once 
you 
break 
the 
communica=on 
down 
into 
frames, 
you 
can 
interweave 
the 
logical 
streams 
over 
a 
single 
TCP 
connec=on. 
• Yet 
another 
idea 
from 
the 
1960s 
is 
new 
again. 
19 
ConnecEons, 
Streams, 
Messages, 
Frames 
S
Copyright 
© 
2014, 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
HTTP/2 
Binary 
Framing 
• Solves 
Head-­‐Of-­‐Line 
(HOL) 
blocking 
problem 
• Type 
field 
can 
be 
DATA, 
HEADERS, 
PRIORITY, 
RST_STREAM, 
SETTINGS, 
PUSH_PROMISE, 
PING, 
GOAWAY, 
WINDOW_UPDATE, 
CONTINUATION 
20 
Enabled 
by 
dumping 
newline 
delimited 
ASCII 
S 
Length 
(24) 
Type 
(8) 
Flags 
(8) 
R Stream 
IdenEfier 
(31) 
Frame 
Payload 
(0 
…)
Copyright 
© 
2014, 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
HTTP/2 
Binary 
Framing 
GET 
/index.html 
HTTP/1.1 
Host: 
example.com 
Accept: 
text/html 
21 
Example 
1 
S 
HEADERS 
+ 
END_STREAM 
+ 
END_HEADERS 
:method: 
GET 
:scheme: 
htp 
:path: 
/index.html 
:authority: 
example.org 
accept: 
text/html
Copyright 
© 
2014, 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
HTTP/2 
Binary 
Framing 
HTTP/1.1 
200 
OK 
Content-­‐Length: 
11 
Content-­‐Type: 
text/html 
Hello 
World 
22 
Example 
2 
S 
HEADERS 
-­‐ 
END_STREAM 
+ 
END_HEADERS 
:status: 
200 
content-­‐length: 
11 
content-­‐type: 
text/html 
DATA 
+ 
END_STREAM 
Hello 
World
HTTP/2 
Stream 
Priori=za=on 
Copyright 
© 
2014, 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
• Stream 
Dependency 
in 
HEADERS 
Frame 
• PRIORITY 
frame 
type 
• An 
addi=onal 
40 
bytes 
– Stream 
id 
(31) 
– Weight 
(8): 
[1, 
256] 
– Exclusive 
bit 
(1) 
• Only 
an 
advice 
23 
S 
A 
B 
C 
4 
12 
A 
B 
D 
C 
4 
16 
12 
exclusive 
= 
0 
A 
D 
16 
B 
C 
4 
12 
exclusive 
= 
1
Copyright 
© 
2014, 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
HTTP/2 
Server 
Push 
• Eliminates 
the 
need 
for 
resource 
inlining. 
• Lets 
the 
server 
populate 
the 
browser’s 
cache 
in 
advance 
of 
the 
browser 
asking 
for 
the 
resource 
to 
put 
in 
the 
cache. 
• No 
corresponding 
JavaScript 
API, 
but 
can 
be 
combined 
with 
SSE 
– Server 
pushes 
stuff 
into 
the 
browser’s 
cache. 
– Server 
uses 
SSE 
to 
tell 
the 
browser 
to 
go 
fetch 
it 
(but 
we 
know 
it’s 
already 
in 
the 
browser’s 
cache). 
24 
E
HTTP/2 
Header 
Compression 
Copyright 
© 
2014, 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
• Observa=on: 
most 
of 
the 
headers 
are 
the 
same 
in 
a 
given 
stream 
– Host: 
Accept: 
user-­‐agent: 
etc. 
• Why 
send 
them 
every 
=me? 
• Have 
the 
server 
and 
the 
client 
keep 
tables 
of 
headers, 
then 
just 
send 
references 
and 
updates 
to 
the 
tables. 
25 
Known 
as 
HPACK 
E
HTTP/2 
Upgrade 
from 
HTTP 
1.1 
Copyright 
© 
2014, 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
• Not 
secure 
– We 
have 
to 
use 
port 
80 
– Use 
exis=ng 
101 
Switching 
Protocols 
from 
HTTP 
1.1 
• Secure 
– Next 
Protocol 
Nego=a=on 
(NPN) 
– Applica=on 
Layer 
Protocol 
Nego=a=on 
26 
Secure 
or 
not-­‐secure? 
E
Our 
Plan 
for 
Your 
Time 
Investment 
Why 
HTTP 
2? 
HTTP 
2 
Big 
Features 
How 
Servlet 
Might 
Expose 
These 
Features 
Java 
SE 
9 
Support 
for 
HTTP 
2 
Summary 
and 
Current 
Status 
Copyright 
© 
2014, 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
1 
2 
3 
4 
5 
E 
27
Servlet 
4.0 
Big 
Ticket 
New 
Features 
Copyright 
© 
2014, 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
• Exis=ng 
API 
is 
designed 
for 
One 
Request 
== 
One 
Response. 
• HTTP/2 
destroys 
this 
assump=on. 
• It 
will 
be 
challenging 
to 
do 
jus=ce 
to 
the 
new 
reality 
of 
One 
Request 
== 
One 
or 
More 
Responses. 
• We 
must 
not 
simply 
bolt 
the 
“One 
or 
More 
Responses” 
concept 
onto 
some 
convenient 
part 
of 
the 
exis=ng 
API. 
28 
Challenges 
in 
Exposing 
HTTP/2 
Features 
in 
Servlet 
API 
S
Servlet 
4.0 
Big 
Ticket 
New 
Features 
Copyright 
© 
2014, 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
• Request/Response 
mul=plexing 
• Binary 
Framing 
• Stream 
Priori=za=on 
• Server 
Push 
• Header 
Compression 
• Upgrade 
from 
HTTP 
1.1 
– ALPN 
or 
(NPN) 
– 101 
Switching 
Protocols 
29 
HTTP/2 
Features 
PotenEally 
Exposed 
in 
Servlet 
API 
S
Servlet 
4.0 
Big 
Ticket 
New 
Features 
Copyright 
© 
2014, 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
• Add 
method 
HttpServletRequest 
and 
HttpServletResponse 
– int 
getStreamId() 
30 
HTTP/2 
Features 
PotenEally 
Exposed 
in 
Servlet 
API 
S
Servlet 
4.0 
Big 
Ticket 
New 
Features 
Copyright 
© 
2014, 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
• Add 
a 
new 
class 
Priority 
– boolean 
exclusive 
– int 
streamId 
– int 
weight 
• Add 
method 
to 
HttpServletRequest 
– Priority 
getPriority() 
• Add 
methods 
to 
HttpServletResponse 
– Priority 
getPriority() 
– void 
setPriority(Priority 
p) 
31 
Stream 
PrioriEzaEon 
S
Servlet 
4.0 
Big 
Ticket 
New 
Features 
Copyright 
© 
2014, 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
• Push 
resource 
to 
client 
for 
a 
given 
url 
and 
headers 
• May 
add 
callback 
for 
comple=on 
or 
error 
of 
a 
push 
• Not 
at 
all 
a 
replacement 
for 
WebSocket 
• Really 
useful 
for 
frameworks 
that 
build 
on 
Servlet, 
such 
as 
JSF 
32 
Server 
Push 
E
synthetic GET /style.css 
Copyright 
© 
2014, 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
33 
Servlet 
4.0 
Big 
Ticket 
New 
Features 
Server 
Push 
Browser 
Server Thread A 
servlet.service() 
GET /index.html 
Server 
discovers 
browser will 
need style.css 
and script.js 
Server Thread B 
servlet.service() 
request.dispatchPushRequest("style.css") 
request.dispatchPushRequest("script.js") 
synthetic GET /script.js 
Server Thread C 
servlet.service() 
style.css 
script.js 
index.html 
E
Copyright 
© 
2014, 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
Server 
Push 
Example 
of 
PotenEal 
Use 
from 
JSF 
public 
class 
FacesServlet 
implements 
Servlet 
{ 
public 
void 
service(ServletRequest 
req, 
ServletResponse 
resp) 
throws 
IOException, 
ServletException 
{ 
//.. 
HttpServletRequest 
request 
= 
(HttpServletRequest) 
req; 
try 
{ 
ResourceHandler 
handler 
= 
context.getApplication().getResourceHandler(); 
if 
(handler.isResourceRequest(context) 
|| 
request.isPushRequest()) 
{ 
handler.handleResourceRequest(context); 
} 
else 
{ 
lifecycle.attachWindow(context); 
lifecycle.execute(context); 
lifecycle.render(context); 
} 
} 
} 
E 
34
Copyright 
© 
2014, 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
Server 
Push 
Example 
of 
PotenEal 
Use 
from 
JSF 
public 
class 
ExternalContextImpl 
extends 
ExternalContext 
{ 
//… 
public 
String 
encodeResourceURL(String 
url) 
{ 
if 
(null 
== 
url) 
{ 
String 
message 
= 
MessageUtils.getExceptionMessageString 
(MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID, 
"url"); 
throw 
new 
NullPointerException(message); 
} 
Map 
attrs 
= 
getResourceAttrs(); 
((HttpServletRequest) 
request).dispatchPushRequest(url, 
attrs); 
return 
((HttpServletResponse) 
response).encodeURL(url); 
} 
//… 
} 
E 
35
Elas=c 
Resilient 
Copyright 
© 
2014, 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
Reac=ve 
Programming 
36 
S 
Responsive 
Message 
Driven 
image 
credit: 
reac=vemanifesto.org
Servlet 
4.0 
and 
Reac=ve 
Programming 
Copyright 
© 
2014, 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
• Non-­‐blocking 
IO 
in 
Servlet 
3.1 
– ServletInputStream 
• #setReadListener, 
#isReady 
– ServletOutputStream 
• #setWriteListener, 
#isReady 
– ReadListener 
• #onDataAvailable, 
#onAllDataRead, 
#onError 
– WriteListener 
• #onWritePossible, 
#onError 
S 
37
Servlet 
4.0 
and 
Reac=ve 
Programming 
Copyright 
© 
2014, 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
• Asynchronous 
in 
Servlet 
3.0 
– ServletRequest#startAsync 
– AsyncContext 
• #addListener, 
#dispatch, 
#complete 
– AsyncListener 
• #onComplete, 
#onError, 
#onStartAsync, 
#onTimeout 
• Event-­‐driven 
– Server-­‐Sent 
Events 
S 
38
Our 
Plan 
for 
Your 
Time 
Investment 
Why 
HTTP 
2? 
HTTP 
2 
Big 
Features 
How 
Servlet 
Might 
Expose 
These 
Features 
Java 
SE 
9 
Support 
for 
HTTP 
2 
Summary 
and 
Current 
Status 
Copyright 
© 
2014, 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
1 
2 
3 
4 
5 
E 
39
Java 
SE 
9 
Support 
for 
HTTP/2 
• JEP 
110 
htp://openjdk.java.net/jeps/110 
Copyright 
© 
2014, 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
• Easy 
to 
use 
API 
• Covers 
only 
the 
most 
common 
use 
cases 
• Supports 
both 
HTTP 
1.1 
and 
2 
E 
40
Java 
SE 
9 
Support 
for 
HTTP/2 
Copyright 
© 
2014, 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
Small 
footprint 
• Two 
classes 
(working 
=tles) 
– HtpRequestGroup 
• configura=on 
for 
mul=ple 
requests 
– HtpRequest 
• one 
request/response 
interac=on 
E 
41
Java 
SE 
9 
Support 
for 
HTTP/2 
Copyright 
© 
2014, 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
Small 
footprint 
• Blocking 
mode: 
one 
thread 
per 
request/response 
– send 
request 
– get 
response 
• Non-­‐blocking 
mode: 
handle 
mul=ple 
request/response 
interac=ons 
in 
single 
thread 
using 
non-­‐blocking 
API 
– analogous 
to 
NIO 
selectors 
E 
42
Java 
SE 
9 
Support 
for 
HTTP/2 
Copyright 
© 
2014, 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
• Responses 
handled 
through 
lambda 
style 
callback 
handlers 
– invoked 
on 
the 
calling 
thread 
– Must 
allocate 
threads 
to 
responses 
manually 
• Request/Response 
bodies 
handled 
as 
InputStream/OutputStream 
Callback 
handlers 
E 
43
Java 
SE 
9 
Support 
for 
HTTP/2 
HttpRequestGroup 
group 
= 
HttpRequestGroup.create(); 
HttpRequest 
req 
= 
group.createRequest() 
.setRequestMethod("POST") 
.setRequestURI(new 
URI("http://www.foo.com/a/b") 
.setRequestBody("Param1=1,Param2=2") 
.onResponseHeader("X-­‐Foo", 
(request, 
name, 
value) 
-­‐> 
{ 
System.out.printf(" 
received 
an 
X-­‐Foo 
header"); 
}) 
.sendRequest() 
.waitForCompletion(); 
Copyright 
© 
2014, 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
E 
44
Java 
SE 
9 
Support 
for 
HTTP/2 
HttpRequestGroup 
group 
= 
HttpRequestGroup.create(); 
HttpRequest 
req 
= 
group.createRequest() 
… 
.onResponseBody((HttpRequest 
request, 
InputStream 
in) 
-­‐> 
{ 
if 
(request.getResponseCode() 
== 
200) 
{ 
Path 
out 
= 
Paths.get("/tmp/out"); 
try 
{ 
Files.copy(in, 
out); 
} 
finally 
{ 
in.close(); 
} 
} 
}) 
.sendRequest() 
.waitForCompletion(); 
Copyright 
© 
2014, 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
E 
45
Java 
SE 
9 
Support 
for 
HTTP/2 
Copyright 
© 
2014, 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
HTTP/2 
features 
• Nego=a=on 
of 
HTTP/2 
from 
1.1 
– ALPN 
or 
plaintext 
• Server 
Push 
– Support 
for 
PUSH_PROMISE 
frames 
• HPACK 
parameters 
E 
46
Our 
Plan 
for 
Your 
Time 
Investment 
Why 
HTTP 
2? 
HTTP 
2 
Big 
Features 
How 
Servlet 
Might 
Expose 
These 
Features 
Java 
SE 
9 
Support 
for 
HTTP 
2 
Summary 
and 
Current 
Status 
Copyright 
© 
2014, 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
1 
2 
3 
4 
5 
S 
47
Summary 
and 
Current 
Status 
• Servlet 
4.0 
brings 
HTTP 
2 
to 
Java 
EE 
Copyright 
© 
2014, 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
– 100% 
compliant 
implementa=on 
of 
HTTP 
2 
– Expose 
key 
features 
to 
the 
API 
• Server 
Push 
• Stream 
Priori=za=on 
• Request/Response 
mul=plexing 
S 
48
Summary 
and 
Current 
Status 
• JSR-­‐369 
just 
formed 
on 
22 
September 
Copyright 
© 
2014, 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
• Tenta=ve 
Delivery 
Schedule 
– Q3 
2014: 
expert 
group 
formed 
– Q1 
2015: 
early 
dra{ 
– Q3 
2015: 
public 
review 
– Q4 
2015: 
proposed 
final 
dra{ 
– Q3 
2016: 
final 
release 
S 
49
Safe 
Harbor 
Statement 
The 
preceding 
is 
intended 
to 
outline 
our 
general 
product 
direc=on. 
It 
is 
intended 
for 
informa=on 
purposes 
only, 
and 
may 
not 
be 
incorporated 
into 
any 
contract. 
It 
is 
not 
a 
commitment 
to 
deliver 
any 
material, 
code, 
or 
func=onality, 
and 
should 
not 
be 
relied 
upon 
in 
making 
purchasing 
decisions. 
The 
development, 
release, 
and 
=ming 
of 
any 
features 
or 
func=onality 
described 
for 
Oracle’s 
products 
remains 
at 
the 
sole 
discre=on 
of 
Oracle. 
Copyright 
© 
2014, 
Oracle 
and/or 
its 
affiliates. 
All 
rights 
reserved. 
| 
50
CON5898 What Servlet 4.0 Means To You

CON5898 What Servlet 4.0 Means To You

  • 2.
    HTTP 2 Comes to Java What Servlet 4.0 Means to You Ed Burns and Shing Wai Chan Java EE Specifica=on Team Oracle Copyright © 2014, Oracle and/or its affiliates. All rights reserved. |
  • 3.
    Safe Harbor Statement The following is intended to outline our general product direc=on. It is intended for informa=on purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or func=onality, and should not be relied upon in making purchasing decisions. The development, release, and =ming of any features or func=onality described for Oracle’s products remains at the sole discre=on of Oracle. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | 3
  • 4.
    Our Plan for Your Time Investment Why HTTP 2? HTTP 2 Big Features How Servlet Might Expose These Features Java SE 9 Support for HTTP 2 Summary and Current Status Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | 1 2 3 4 5 S 4
  • 5.
    Our Plan for Your Time Investment Why HTTP 2? HTTP 2 Big Features How Servlet Might Expose These Features Java SE 9 Support for HTTP 2 Summary and Current Status Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | 1 2 3 4 5 S 5
  • 6.
    . . . Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | Why HTTP 2? A Real Life Example S index.html style1.css style2.css script1.js . . . script9.js pic1.jpg pic8.jpg photo1.png photo2.png . . .
  • 7.
    Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | Why HTTP 2? Problems in HTTP 1.1 • HTTP Pipelining • Head-­‐of-­‐Line blocking S style1.css style2.css Client Server index.html index.html style1.css style2.css script1.js script2.js . . . script1.js script2.js . . .
  • 8.
    Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | Why HTTP 2? Problems in HTTP 1.1 • Inefficient use of TCP sockets S Client Server Client Server Client Server Client Server Client Server Client Server
  • 9.
    Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | Why HTTP 2? What is an opEmizaEon? • Much of what we do in web-­‐apps is a hack to work around shortcomings in HTTP 1.1 – File concatena=on and image sprites – Domain sharding – Inlined assets E 9
  • 10.
    File Concatena=on and Image Sprites TCP Efficiency Improves with Larger Files Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | E 10
  • 11.
    File Concatena=on and Image Sprites TCP Efficiency Improves with Larger Files Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | E 11 • Modern web page now consists of more than 90 resources fetched from 15 dis=nct hosts • Solu=on: – Just work around it by shoving more than one logical file into one physical file. – Seminal ar=cle: A List Apart hPp://alistapart.com/arEcle/sprites – Useful tool: SpritePad hPp://spritepad.wearekiss.com/
  • 12.
    File Concatena=on and Image Sprites TCP Efficiency Improves with Larger Files Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | E 12 .ic-AerospaceAndDefense-wht-on-gray, .ic-AerospaceAndDefense-wht-on-red, .ic-Airline-wht-on-gray, .ic-Airline-wht-on-red{ background: url(sprites.png) no-repeat; } .ic-AerospaceAndDefense-wht-on-gray{ background-position: 0 0; width: 80px; height: 80px; } .ic-AerospaceAndDefense-wht-on-red{ background-position: -81px 0; width: 80px; height: 80px; } .ic-Airline-wht-on-gray{ background-position: 0 -80px ; width: 80px; height: 80px; } .ic-Airline-wht-on-red{ background-position: -81px -79px ; width: 80px; height: 80px; }
  • 13.
    Domain Sharding Split page resources across several hosts to work around browser limits Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | 13
  • 14.
    Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | Inlined Assets Base64 Encoding Will Never Die • data URLs • <img src="data:image/gif;base64,R0lGODlhEAAOALMAAOazToeHh0tLS/ 7LZv/0jvb29t/f3//Ub/ /ge8WSLf/rhf/3kdbW1mxsbP//mf/// yH5BAAAAAAALAAAAAAQAA4…" /> E 14
  • 15.
    Our Plan for Your Time Investment Why HTTP 2? HTTP 2 Big Features How Servlet Might Expose These Features Java SE 9 Support for HTTP 2 Summary and Current Status Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | 1 2 3 4 5 E 15
  • 16.
    • Request/Response Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | mul=plexing • Binary Framing • Stream Priori=za=on • Server Push • Header Compression • Upgrade from HTTP 1.1 • ALPN (or NPN) • 101 Switching Protocols 16 HTTP/2 Big Ticket Feature Review E
  • 17.
    HTTP/2 Request Response Mul=plexing Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | • Fully bi-­‐direc=onal • Enabled by defining some terms – Connec&on A TCP socket – Stream A “channel” within a connec=on – Message A logical message, such as a request or a response – Frame The smallest unit of communica=on in HTTP/2. 17 Lets you do more things with a single TCP connecEon S
  • 18.
    HTTP/2 Request Response Mul=plexing Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | 18 ConnecEons, Streams, Messages, Frames S
  • 19.
    HTTP/2 Request Response Mul=plexing Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | • Once you break the communica=on down into frames, you can interweave the logical streams over a single TCP connec=on. • Yet another idea from the 1960s is new again. 19 ConnecEons, Streams, Messages, Frames S
  • 20.
    Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | HTTP/2 Binary Framing • Solves Head-­‐Of-­‐Line (HOL) blocking problem • Type field can be DATA, HEADERS, PRIORITY, RST_STREAM, SETTINGS, PUSH_PROMISE, PING, GOAWAY, WINDOW_UPDATE, CONTINUATION 20 Enabled by dumping newline delimited ASCII S Length (24) Type (8) Flags (8) R Stream IdenEfier (31) Frame Payload (0 …)
  • 21.
    Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | HTTP/2 Binary Framing GET /index.html HTTP/1.1 Host: example.com Accept: text/html 21 Example 1 S HEADERS + END_STREAM + END_HEADERS :method: GET :scheme: htp :path: /index.html :authority: example.org accept: text/html
  • 22.
    Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | HTTP/2 Binary Framing HTTP/1.1 200 OK Content-­‐Length: 11 Content-­‐Type: text/html Hello World 22 Example 2 S HEADERS -­‐ END_STREAM + END_HEADERS :status: 200 content-­‐length: 11 content-­‐type: text/html DATA + END_STREAM Hello World
  • 23.
    HTTP/2 Stream Priori=za=on Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | • Stream Dependency in HEADERS Frame • PRIORITY frame type • An addi=onal 40 bytes – Stream id (31) – Weight (8): [1, 256] – Exclusive bit (1) • Only an advice 23 S A B C 4 12 A B D C 4 16 12 exclusive = 0 A D 16 B C 4 12 exclusive = 1
  • 24.
    Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | HTTP/2 Server Push • Eliminates the need for resource inlining. • Lets the server populate the browser’s cache in advance of the browser asking for the resource to put in the cache. • No corresponding JavaScript API, but can be combined with SSE – Server pushes stuff into the browser’s cache. – Server uses SSE to tell the browser to go fetch it (but we know it’s already in the browser’s cache). 24 E
  • 25.
    HTTP/2 Header Compression Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | • Observa=on: most of the headers are the same in a given stream – Host: Accept: user-­‐agent: etc. • Why send them every =me? • Have the server and the client keep tables of headers, then just send references and updates to the tables. 25 Known as HPACK E
  • 26.
    HTTP/2 Upgrade from HTTP 1.1 Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | • Not secure – We have to use port 80 – Use exis=ng 101 Switching Protocols from HTTP 1.1 • Secure – Next Protocol Nego=a=on (NPN) – Applica=on Layer Protocol Nego=a=on 26 Secure or not-­‐secure? E
  • 27.
    Our Plan for Your Time Investment Why HTTP 2? HTTP 2 Big Features How Servlet Might Expose These Features Java SE 9 Support for HTTP 2 Summary and Current Status Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | 1 2 3 4 5 E 27
  • 28.
    Servlet 4.0 Big Ticket New Features Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | • Exis=ng API is designed for One Request == One Response. • HTTP/2 destroys this assump=on. • It will be challenging to do jus=ce to the new reality of One Request == One or More Responses. • We must not simply bolt the “One or More Responses” concept onto some convenient part of the exis=ng API. 28 Challenges in Exposing HTTP/2 Features in Servlet API S
  • 29.
    Servlet 4.0 Big Ticket New Features Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | • Request/Response mul=plexing • Binary Framing • Stream Priori=za=on • Server Push • Header Compression • Upgrade from HTTP 1.1 – ALPN or (NPN) – 101 Switching Protocols 29 HTTP/2 Features PotenEally Exposed in Servlet API S
  • 30.
    Servlet 4.0 Big Ticket New Features Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | • Add method HttpServletRequest and HttpServletResponse – int getStreamId() 30 HTTP/2 Features PotenEally Exposed in Servlet API S
  • 31.
    Servlet 4.0 Big Ticket New Features Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | • Add a new class Priority – boolean exclusive – int streamId – int weight • Add method to HttpServletRequest – Priority getPriority() • Add methods to HttpServletResponse – Priority getPriority() – void setPriority(Priority p) 31 Stream PrioriEzaEon S
  • 32.
    Servlet 4.0 Big Ticket New Features Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | • Push resource to client for a given url and headers • May add callback for comple=on or error of a push • Not at all a replacement for WebSocket • Really useful for frameworks that build on Servlet, such as JSF 32 Server Push E
  • 33.
    synthetic GET /style.css Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | 33 Servlet 4.0 Big Ticket New Features Server Push Browser Server Thread A servlet.service() GET /index.html Server discovers browser will need style.css and script.js Server Thread B servlet.service() request.dispatchPushRequest("style.css") request.dispatchPushRequest("script.js") synthetic GET /script.js Server Thread C servlet.service() style.css script.js index.html E
  • 34.
    Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | Server Push Example of PotenEal Use from JSF public class FacesServlet implements Servlet { public void service(ServletRequest req, ServletResponse resp) throws IOException, ServletException { //.. HttpServletRequest request = (HttpServletRequest) req; try { ResourceHandler handler = context.getApplication().getResourceHandler(); if (handler.isResourceRequest(context) || request.isPushRequest()) { handler.handleResourceRequest(context); } else { lifecycle.attachWindow(context); lifecycle.execute(context); lifecycle.render(context); } } } E 34
  • 35.
    Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | Server Push Example of PotenEal Use from JSF public class ExternalContextImpl extends ExternalContext { //… public String encodeResourceURL(String url) { if (null == url) { String message = MessageUtils.getExceptionMessageString (MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID, "url"); throw new NullPointerException(message); } Map attrs = getResourceAttrs(); ((HttpServletRequest) request).dispatchPushRequest(url, attrs); return ((HttpServletResponse) response).encodeURL(url); } //… } E 35
  • 36.
    Elas=c Resilient Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | Reac=ve Programming 36 S Responsive Message Driven image credit: reac=vemanifesto.org
  • 37.
    Servlet 4.0 and Reac=ve Programming Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | • Non-­‐blocking IO in Servlet 3.1 – ServletInputStream • #setReadListener, #isReady – ServletOutputStream • #setWriteListener, #isReady – ReadListener • #onDataAvailable, #onAllDataRead, #onError – WriteListener • #onWritePossible, #onError S 37
  • 38.
    Servlet 4.0 and Reac=ve Programming Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | • Asynchronous in Servlet 3.0 – ServletRequest#startAsync – AsyncContext • #addListener, #dispatch, #complete – AsyncListener • #onComplete, #onError, #onStartAsync, #onTimeout • Event-­‐driven – Server-­‐Sent Events S 38
  • 39.
    Our Plan for Your Time Investment Why HTTP 2? HTTP 2 Big Features How Servlet Might Expose These Features Java SE 9 Support for HTTP 2 Summary and Current Status Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | 1 2 3 4 5 E 39
  • 40.
    Java SE 9 Support for HTTP/2 • JEP 110 htp://openjdk.java.net/jeps/110 Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | • Easy to use API • Covers only the most common use cases • Supports both HTTP 1.1 and 2 E 40
  • 41.
    Java SE 9 Support for HTTP/2 Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | Small footprint • Two classes (working =tles) – HtpRequestGroup • configura=on for mul=ple requests – HtpRequest • one request/response interac=on E 41
  • 42.
    Java SE 9 Support for HTTP/2 Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | Small footprint • Blocking mode: one thread per request/response – send request – get response • Non-­‐blocking mode: handle mul=ple request/response interac=ons in single thread using non-­‐blocking API – analogous to NIO selectors E 42
  • 43.
    Java SE 9 Support for HTTP/2 Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | • Responses handled through lambda style callback handlers – invoked on the calling thread – Must allocate threads to responses manually • Request/Response bodies handled as InputStream/OutputStream Callback handlers E 43
  • 44.
    Java SE 9 Support for HTTP/2 HttpRequestGroup group = HttpRequestGroup.create(); HttpRequest req = group.createRequest() .setRequestMethod("POST") .setRequestURI(new URI("http://www.foo.com/a/b") .setRequestBody("Param1=1,Param2=2") .onResponseHeader("X-­‐Foo", (request, name, value) -­‐> { System.out.printf(" received an X-­‐Foo header"); }) .sendRequest() .waitForCompletion(); Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | E 44
  • 45.
    Java SE 9 Support for HTTP/2 HttpRequestGroup group = HttpRequestGroup.create(); HttpRequest req = group.createRequest() … .onResponseBody((HttpRequest request, InputStream in) -­‐> { if (request.getResponseCode() == 200) { Path out = Paths.get("/tmp/out"); try { Files.copy(in, out); } finally { in.close(); } } }) .sendRequest() .waitForCompletion(); Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | E 45
  • 46.
    Java SE 9 Support for HTTP/2 Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | HTTP/2 features • Nego=a=on of HTTP/2 from 1.1 – ALPN or plaintext • Server Push – Support for PUSH_PROMISE frames • HPACK parameters E 46
  • 47.
    Our Plan for Your Time Investment Why HTTP 2? HTTP 2 Big Features How Servlet Might Expose These Features Java SE 9 Support for HTTP 2 Summary and Current Status Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | 1 2 3 4 5 S 47
  • 48.
    Summary and Current Status • Servlet 4.0 brings HTTP 2 to Java EE Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | – 100% compliant implementa=on of HTTP 2 – Expose key features to the API • Server Push • Stream Priori=za=on • Request/Response mul=plexing S 48
  • 49.
    Summary and Current Status • JSR-­‐369 just formed on 22 September Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | • Tenta=ve Delivery Schedule – Q3 2014: expert group formed – Q1 2015: early dra{ – Q3 2015: public review – Q4 2015: proposed final dra{ – Q3 2016: final release S 49
  • 50.
    Safe Harbor Statement The preceding is intended to outline our general product direc=on. It is intended for informa=on purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or func=onality, and should not be relied upon in making purchasing decisions. The development, release, and =ming of any features or func=onality described for Oracle’s products remains at the sole discre=on of Oracle. Copyright © 2014, Oracle and/or its affiliates. All rights reserved. | 50