Java™ Servlet 3.0: Empowering Your Web Applications With Async E!

tensibility an" #ore Rajiv Mordani, Jan Luehe
Sun Microsystems
Webtide / Jetty

Greg Wilkins

● ● ● ● ● ● ●

vervie! "ase o# $evelo%ment $ynamic registration o# Servlets and &ilters 'luggability Asynchronous su%%ort Security enhancements Miscellaneous


● ●

Java Servlet ()* A'+ , JSR (-. About /* members in the e0%ert grou%

Good mi0 o# re%resentation #rom major Java "" vendors, !eb container vendors and individual !eb #rame!ork authors "ase o# $evelo%ment 'luggability Asynchronous su%%ort Security

Main areas o# #ocus
● ● ● ●

● ●

S%eci#ication in 'ro%osed &inal $ra#t &inal release aligned !ith Java "" 1


● ● ● ● ● ● ●

vervie! "ase o# $evelo%ment $ynamic registration o# Servlets and &ilters 'luggability Asynchronous su%%ort Security enhancements Miscellaneous


"ase o# $evelo%ment 2"o$3 ● &ocus on "ase o# $evelo%ment 2"o$3 in the Servlet ()* A'+ "nhance A'+ to use the ne! language #eatures introduced since J/S" .)* Annotations #or declarative style o# %rogramming ● ● ● 4o !eb)0ml needed ● Generics #or ty%e sa#ety in the A'+ !ithout breaking back!ards com%atibility 5etter de#aults and convention over con#iguration 6 ● .

&ilters. Listeners and security constraints ● @WebServlet – $e#ine a Servlet ● @WebFilter – $e#ine a &ilter ● @WebListener – $e#ine a Listener ● @WebInitParam – $e#ine init %arams ● @MultipartConfig – $e#ine #ileu%load %ro%erties ● 7an use !eb)0ml to override values s%eci#ied in the annotations 7 "ase o# $evelo%ment .6se o# Annotations ● Annotations to declare Servlets.

doPost inherited #rom abstract class 8 ."ase o# $evelo%ment 6se o# Annotations 2contd3 ● 8 @WebServlet #or de#ining a Servlet 9he annotation M6S9 have at a minimum the 6RL %attern #or the Servlet All other #ields o%tional !ith reasonable de#aults &or e0am%le. the de#ault name o# the Servlet is the #ully :uali#ied class name 7lass M6S9 still e0tend HttpServlet 8 8 8 8 Method contracts #or doGet.

servlet+name.Servlet /). M-Servlet ). #####M-Servlet 9 . ). ). e0am%le publi lass SimpleSample e!tends HttpServlet " publi void doGet ####$HttpServlet%e&uest re&.*eb+app. )servlet+name. )servlet. #####HttpServlet%esponse res' ####" ( ( !eb)0ml 2intentionally le#t unreadable3 )*eb+app.servlet+ lass.servlet+name.url+pattern.servlet. )servlet+mapping. /// ). )url+pattern. samples/SimpleSample ).servlet+mapping. ).M-0pp ). )servlet+ lass. . )servlet+name.

foo2' publi lass SimpleSample e!tends HttpServlet " publi " ( ( 10 void doGet$HttpServlet%e&uest re&.Servlet ()* e0am%le @WebServlet$1.HttpServlet%esponse res' .

name32M-Servlet2.HttpServlet%esponse res' . as-n Supported3true' publi lass SimpleSample e!tends HttpServlet " publi " ( ( 11 void doGet$HttpServlet%e&uest re&.foo2.Servlet ()* e0am%le @WebServlet$urlPatterns31.

Agenda ● ● ● ● ● ● ● vervie! "ase o# $evelo%ment $ynamic registration o# Servlets and &ilters 'luggability Asynchronous su%%ort Security enhancements Miscellaneous 12 .

ation ● ServletConte!t4add5Servlet6Filter7 ● verloaded versions take <Servlet=&ilter> name and ● ● &ully :uali#ied <Servlet=&ilter> class name ● R Class)8 e!tends 5Servlet6Filter7.$ynamic registration o# Servlets and &ilters Register ● 'er#ormed during Servlet7onte0t initiali. R <Servlet=&ilter> instance ● 6se returned %egistration handle to con#igure all as%ects o# <Servlet=&ilter> 13 .

$ynamic registration o# Servlets and &ilters 7reate and Register ● ● ServletConte!t4 reate5Servlet6Filter7 9akes Class)8 e!tends 5Servlet6Filter7.ed be#ore it is registered via the ServletConte!t4add5Servlet6Filter> methods 14 ● ● ● . argument 7ontainer res%onsible #or instantiating the 5Servlet 6Filter7 Su%%orts resource injection by container Returned [Servlet6Filter] instance may be #ully customi.

ation %arameters and ma%%ings Any con#licts returned as 9ava/util/Set 15 .$ynamic registration o# Servlets and &ilters Looku% ● ServletConte!t4get5Servlet6 Filter7%egistration ● ● 9akes <Servlet=&ilter> name as argument Returned %egistration handle %rovides subset o# con#iguration methods ● ● May only be used to add initiali.

'< d-nami /set0s-n Supported$true'< d-nami 3 servletConte!t/addServlet$ 16 .'< d-nami /addMapping$.:-nami Servlet.. .d-nami Servlet. om/m.$ynamic registration o# Servlets/&ilters Register "0am%le Servlet%egistration/:-nami

.:e lar edServlet.'< de lared/addMapping$.$ynamic registration o# Servlets/&ilters Looku% "0am%le Servlet%egistration de lared 3 servletConte!t/getServlet%egistration$ laredServlet.'< de lared/setInitParameter$. .'< 17 .value.

Agenda ● ● ● ● ● ● ● vervie! "ase o# $evelo%ment $ynamic registration o# Servlets and &ilters 'luggability Asynchronous su%%ort Security enhancements Miscellaneous 18 .

'luggability ● "nable use o# libraries and #rame!ork !ithout boiler %late con#iguration in de%loyment descri%tors ● 'ut the burden on the #rame!ork develo%er ● Modulari.e *eb/!ml to allo! #rame!orks to be sel#?contained !ithin their o!n JAR #ile 'rogrammatic con#iguration A'+s 6se o# annotations ● ● 19 .

'luggability ● Motivation #or !eb)0ml modulari.ation 6se o# #rame!ork re:uires 2%ossibly com%le03 con#iguration in *eb/!ml &or e0am%le ● ● ● ● $eclare a controller Servlet Logging and security &ilters $eclare Listeners to %er#orm actions at various %oints in the li#ecycle o# the a%%lication ● ● 7an get com%le0 as de%endencies increase &rame!orks also need to document all the con#iguration that needs to be done 20 .

lib considered as #ragments 21 .'luggability ● !eb?#ragment)0ml *eb+fragment/!ml is descri%tor #or #rame!ork / ● ● library +ncluded in M=>0+I?F directory ● 7ontainer res%onsible #or discovering #ragments and assembling the e##ective de%loyment descri%tor Almost identical to *eb/!ml ● rdering related elements di##erent ● nly JAR #iles in W=@+I?F.

)servlet. ).Wel ome).. om/ omeServlet).servlet+name. )url+pattern.*eb+fragment.*el ome).servlet+name. /// ).'luggability )*eb+fragment. )servlet+name. 22 .servlet.servlet+mapping. )servlet+mapping. ).servlet+ lass.url+pattern. )servlet+ lass. !eb?#ragment)0ml e0am%le )servlet+name.*el ome).

&ragments may declare ordering %re#erences relative to other #ragments via )ordering. 8 ● +gnored i# )absolute+ordering. !ith nested )before.'luggability ● ● ● rdering 7om%atible !ith JavaServer™ &aces &ragments identi#ied by )name. and )after.. *eb/!ml may declare absolute ordering o# #ragments via )absolute+ordering. s%eci#ied ● S%ecial )otAers. element moves #ragment to beginning or end o# list o# sorted #ragments 23 .

5B/9ar7.lib. M=>0+I?F.resour es 7ontainer must honor this ne! location !hen %rocessing A99' re:uests and calls to ServletConte!t4get%esour e50sStream7 Resources in document root take %recedence over those in bundled JAR #iles 24 ● ● ● .'luggability ● Resource sharing Static and JavaServer™ 'ages 2JS'3 resources no longer con#ined to !eb a%%lication@s document root May be %laced inside W=@+I?F.

inde!/9sp .sAared/9sp !ill be served #rom: .M=>0+ I?F.sAared/9sp 25 .m-*ebapp.sAared/9sp 'luggability Re:uest #orB sharingB "0am%le m-*ebapp/*ar %ackagingB .resour es.resour es.m-*ebapp.patA.sAared/9arC.lo alAostDEFEF..lib.W=@+ I?F.M=>0+ I?F.

S%ring ● Libraries may %rovide im%lementation o# ServletContainerInitialiGer Looked u% via the JAR Services A'+ in J$D 1 +nvoked be#ore any Listeners during the initiali. JAC?WS.'luggability ● Shared libraries Su%%ort %lugging in o# container installed JAR #iles ● "0am%lesB JS&.ation o# the a%%lication ● ● 26 .

'luggability ● Shared libraries 2contd3 ServletContainerInitialiGer e0%resses interest in 7lasses via @Handles>-pes 7ontainer discovers classes that match @Handles>-pes and %asses them to ServletContainerInitialiGer ServletContainerInitialiGer ins%ects %assed in 7lasses and may register Servlets and &ilters based on them ● ● 27 .

1 om/sun/9a!*s/H0IWSServlet2'< ( ( 28 implements void onStartup$Set)Class) e0am%le @Handles>-pes$WebServi e/ lass' publi lass H0IWSInitialiGer ServletContainerInitialiGer " publi " t!/addServlet$1H0IWSServlet2. ServletConte!t t!' .'luggability Servlet7ontainer+nitiali. .

Agenda ● ● ● ● ● ● ● vervie! "ase o# $evelo%ment $ynamic registration o# Servlets and &ilters 'luggability Asynchronous su%%ort Security enhancements Miscellaneous 29 .

Why Asynchronous ServletsE ● 4ot #or Async + F ● ● ● Re:uests mostly small 2single %acket3 Aard to asynchronously %roduce large res%onses Async + su%%ort !aiting #or 4+ / 2Servlet ()-E3 ● Async Servlets are #orB ● ● ● Waiting #or resources 2eg J$57 connection3 Waiting #or events 2eg 7hat3 Waiting #or res%onses 2eg !eb services. GoS3 30 .

5locking !aiting consumes resources ● Web A%%lication using remote !eb services ● ● ● Aandling -*** re:uests / sec .*H o# re:uests use all .** threads 31 .*H re:uests call remote !eb service .** threads in container thread %ool ● +# remote !eb service is slo! 2-***ms3 ● ● 9hread starvation in .secondF .

Waiting #or Web Services 5locking Asynchronous 9hread blocked WS re:uest +n %arallel 32 .

as-n +supported. @WebServlet$as-n Supported3true' registration/set0s-n Supported$boolean' 33 ● With annotation ● ● 'rogrammatic ● .true).ServletRe:uest ● Servlet%e&uest4is0s-n Supported$' ● Asynchronous A'+ >rue i# ALL <&ilter=Servlet>s su%%ort async in ● ● the &ilter chain the Re:uest$is%atch chain ● 7on#igured in ● *eb/!ml ● )as-n +supported.

response' Filter chain ● 0s-n Conte!t Servlet%e&uest4start0s-n $Servlet%e&uest re&. Servlet%esponse res' ● Iariation that %reserves !ra%%ers 34 .Asynchronous A'+ ServletRe:uest ● 0s-n Conte!t Servlet%e&uest4start0s-n $' ● ● 7alled by <&ilter=Servlet> Res%onse is 4 9 commited on return o#B ● ● Servlet/servi e$re&uest.

"J5s usable ● 0s-n Conte!t4dispat A$String patA' ● Iariation to async dis%atch to s%eci#ic Servlet 35 . J9A. JS& or other #rame!orks usable J4$+.Async7onte0t ● 0s-n Conte!t4dispat A$' ● ● Asynchronous A'+ 7alled by your asynchronous handler Schedule async dis%atchB :ispat Aer>-pe/0SJ?C Res%onse generated by <&ilter=Servlet> usingB ● ● ● ● container thread %ool JS'.

or !ith 0s-n Conte!t4start$%unnable r' ● #or J4$+. classloader 36 .Asynchronous A'+ Async7onte0t ● 0s-n Conte!t4 omplete$' ● ● 7alled by your asynchronous handler Res%onse has been generated asynchronously ● ● !ithout Servlet #eatures.

Asynchronous Web Service Server Weba%% doGet() s t a rtA s y n c () WS call dis pa t c h () doGet() 37 .

Multi%le 6sage Styles ● start0s-n $' ● ● K dispat A$' Retry re:uest a#ter async !ait &ilters re?a%%lied i# on :ispat Aer>-pe/0SJ?C ● start0s-n $' ● K dispat A$patA' 6se s%eci#ic Servlet handling a#ter async !ait ● start0s-n $' ● K omplete$' Generate res%onse asynchronously 38 .

res' K ● omplete$' Generate !ra%%ed res%onse asynchronously 39 .res' K dispat A$' ● ● ● Retry re:uest a#ter async !ait Wra%%ers are ke%t %e&uest:ispat Aer4for*ard target used ● start0s-n $re&.Multi%le 6sage Styles ● start0s-n $re&.res' K dispat A$patA' ● S%eci#ic Servlet handling a#ter async !ait ● start0s-n $re&.

Asynchronous A'+ $etails ● 9imeouts ● Servlet%e&uest4set0s-n >imeout$long ms' ● 5y de#ault error dis%atch on timeout ● Listeners ● 8 0s-n Listener4Ln>imeout 0s-n Listener4LnComplete 40 .

$emonstration J Asynchronous e5ay Web Service "o$ %ackaging 8 M"9A?+4& 8 8 !eb?#ragment)0ml Resources/K J Glass#ish 7ontainer 8 Async Serlvet J Jetty A99' 7lient 8 Async 7lient 41 .

Agenda ● ● ● ● ● ● ● vervie! "ase o# $evelo%ment $ynamic registration o# Servlets and &ilters 'luggability Asynchronous su%%ort Security enhancements Miscellaneous 42 .

Security ● Security constraints via common annotations Su%%ort #or common annotations ● ● ● ● @%oles0llo*ed ?J autA+ onstraint !ith roles @:en-0ll ?J "m%ty autA+ onstraint @Permit0ll ?J 4o autA+ onstraint @>ransportProte ted ?J user+data+ onstraint ● Annotations en#orced on 9ava!/Attp/Servlet class and doIII methods o# HttpServlet Method?targeted annotations take %recedence over class?targeted annotations 43 ● .

Security ● Security constraints via common annotations 2contd3 Security constraints in *eb/!ml override annotations. all other methods3 44 . metdata+ omplete disables annotations *eb+resour e+ olle tion enhanced !ith Attp+ metAod+omission to ● ● Allo! constraints to be s%eci#ied on non? enumerable A99' method subsets 2i)e).

Security ● 'rogrammatic container authentication and logout HttpServlet%e&uest4login$String username. String pass*ord' ● ● ● Re%lacement #or &5L A%%lication su%ervises credential collection HttpServlet%e&uest4autAenti ate$HttpServl et%esponse' ● ● A%%lication initiates container mediated authentication #rom a resource that is not covered by any authentication constraints A%%lication decides !hen authentication must occur 45 .

Security ● 'rogrammatic container authentication and logout 2contd3 HttpServlet%e&uest4logout ● +ntegration o# additional container authentication modules via Servlet 'ro#ile o# JSR -L1 recommended 46 .

Agenda ● ● ● ● ● ● ● vervie! "ase o# $evelo%ment $ynamic registration o# Servlets and &ilters 'luggability Asynchronous su%%ort Security enhancements Miscellaneous 47 .

cookie attribute ● "0am%leB servletConte!t/getSessionCooMieConfig $'/setHttpLnl-$true' ● $e#ault error %age 48 .Miscellaneous &eatures / A'+s ● Session tracking cookie con#iguration ● ● Iia *eb/!ml 'rogrammatic via 9ava!/servlet/SessionCooMieConfig ● Su%%ort #or HttpLnl.

Miscellaneous &eatures / A'+s 2contd3 Servlet%e&uest4getServletConte!t Servlet%e&uest4get:ispat Aer>-pe Servlet5%e&uest6 %esponse7Wrapper4isWrapperFor HttpServlet%esponse4getStatus HttpServlet%esponse4getHeader HttpServlet%esponse4getHeaders HttpServlet%esponse4getHeader?ames 49 .

Miscellaneous &eatures / A'+s 2contd3 &ile u%load A'+s Servlet%e&uest4getParts Servlet%e&uest4getPart @MultipartConfig 7hanges to *eb/!ml 50 .

Summary ● ● Major revision since Servlet /)M 7om%rehensive set o# ne! #eatures enable modern style o# !eb a%%lications and greatly increases develo%er %roductivity Sim%li#ies assembly o# large a%%lications #rom reusable com%onents ● 51 .

deploy and distribute 52 . 'ython and $jango O Tools Integration ONetBeans and Eclipse GlassFish V3 Preview Available now! ● ● ● ● ● GlassFish V2 – Production Ready ● ● ● 5est %rice/%er#ormance o%en source A%% server !ith 7lustering. Aigh Availability. Load 5alancing Secure. Su%%ort #or Ruby?on?Rails. easy to develo% N de%loy Critical Support Runs in?%rocess and easy to e0tend Osun.Glass&ish 7ommunity ● %en Source and "nter%rise Ready O 24x7 Enterprise and Mission Java "" 1 re#erence im%lementation Modular SGi architecture . )4"9?intero% Web svcs Su%%ort #or Aja0 and 7omet S A and 5usiness +ntegration %lat#orm S+' servlet technology #or converged services glassfish. Groovy and Grails. ● GlassFish ES ● ● GlassFish !o""unications A## Server ● Al a!s free to download.

Webtide N Jetty J J Status u%date htt%B//ecli%se)org/jetty 53 .

Rajiv Mordani rajiv)mordaniPsun)com Jan Luehe jan)luehePsun)com Greg Wilkins greg!P!ebtide)com .