Java EE 6
All Platform, no clutter
@greyfairer
Agenda
Servlet 3.0, JAX-RS 1.1, JSF 2.0
JSR-330 a.k.a. javax.inject
JSR-299 a.k.a. CDI
Arquillian
CDI Extensions
Servlets 3.0
@WebServlet(name=”MyServlet”,
urlPatterns={”/myApp/*”})
public class MyServlet extends HttpServlet {
public void doGet(HttpServletRequest req,
HttpServletResponse res)
{
...
@WebFilter( filterName="PaymentFilter",
urlPatterns={"/*"},
servletNames={"PaymentServlet"},
)
public class PaymentFilter implements Filter {
...
Modular war's
● Jar's in WEB-INF/lib can also contain annotated
servlet/filter/listener classes
● Jar's can also contribute META-INF/web.xml
● <web-fragment>...</web-fragment>
● Jar's can also contribute jsp's, static html
● In META-INF/resources
Servlet 3.0 and CDI
● If you have a WEB-INF/beans.xml, Web
archives will be scanned for Managed Beans
● Can be injected in servlets, JAX-RS, JSF beans
EJB CDI JPAJSF ...
@Stateful
@Stateless
@Singleton
@Produces
@Named @Entity
@Managed
Bean ...
EJB
Asynch Servlets
@WebServlet(name="AsyncServlet", asyncSupported=true)
public class AsyncServlet extends HttpServlet {
protected void doGet(HttpServletRequest request,
HttpServletResponse response) {
final AsyncContext ac = request.startAsync();
ac.setTimeout(...)
ac.start( new Runnable(){
ac.getResponse().getWriter()...;
ac.complete();
});
Demo: Chat servlet
● Glassfish: asynch-request-war sample
JAX-RS 1.1
● RESTfull Web Services
● Typical use: javascript clients
● GET/POST/PUT/DELETE resources
● URL encodes resource identification
● Encoding defined by MIME headers
● Class and/or method annotated with @Path
● Automatic JSON/XML encoding using JAXB
Demo: novashop
● From Adam Bien hacks
● http://kenai.com/projects/javaee-patterns/sources/hg/show/hacks/NovaShop
● @Path, @PathParam, @Produces, @Consumes annotation on WorkshopRegistration
● Use Chrome Advanced Rest Client
JSF 2.0
● .xhtml view technology
● Uses JEE6 managed POJO’s
● @RequestScoped, @ConversationScoped, @Named
● New: call any method, even with parameters.
● Controllers: method calls on managed beans
● Bind form fields to bean properties
– Typically @RequestScoped beans
● Implicit navigation using method’s returned value
● Redirect after post: ?faces-redirect=true
JSF 2.0 in Java EE 6
● Supports javax.validation
● All @Named CDI beans are accessible
● Custom Components will use JAX-RS
Demo: novashop 2
● From Adam Bien hacks
● http://kenai.com/projects/javaee-patterns/sources/hg/show/hacks/NovaShop
● Index.xhtml, registered.xhtml
JSR-330 a.k.a. javax.inject
● @Inject, @Qualifier, @Scope, etc.
● Standardized Dependency Injection API
● Reference Implementation: Google Guice 2.0
● Also supported by Spring since 3.0
● Defines API, not injector implementation or
configuration
JSR-299 a.k.a. CDI
● Standardized dependency injection behaviour
and configuration
● Required feature of any Java EE 6 container,
including Web profile
● javax.enterprise.* package
● @Produces (evt. with InjectionPoint)
● Instance<> and @Alternative
● Event<> and @Observes
Demo: novashop 3
● Configurator for maxNumberOfRegistrations
CDI Extensions
● CDI has Service Provider Interfaces
● Allows extensions drop-in
● http://groups.diigo.com/group/cdi-extensions
● Seam 3 and others
● https://github.com/CDISource/cdisource
● @Spring Spring to CDI bridge
● CDI to Spring: use JNDI Lookup.
Q & A

Java EE 6

  • 1.
    Java EE 6 AllPlatform, no clutter @greyfairer
  • 2.
    Agenda Servlet 3.0, JAX-RS1.1, JSF 2.0 JSR-330 a.k.a. javax.inject JSR-299 a.k.a. CDI Arquillian CDI Extensions
  • 3.
    Servlets 3.0 @WebServlet(name=”MyServlet”, urlPatterns={”/myApp/*”}) public classMyServlet extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) { ... @WebFilter( filterName="PaymentFilter", urlPatterns={"/*"}, servletNames={"PaymentServlet"}, ) public class PaymentFilter implements Filter { ...
  • 4.
    Modular war's ● Jar'sin WEB-INF/lib can also contain annotated servlet/filter/listener classes ● Jar's can also contribute META-INF/web.xml ● <web-fragment>...</web-fragment> ● Jar's can also contribute jsp's, static html ● In META-INF/resources
  • 5.
    Servlet 3.0 andCDI ● If you have a WEB-INF/beans.xml, Web archives will be scanned for Managed Beans ● Can be injected in servlets, JAX-RS, JSF beans EJB CDI JPAJSF ... @Stateful @Stateless @Singleton @Produces @Named @Entity @Managed Bean ... EJB
  • 6.
    Asynch Servlets @WebServlet(name="AsyncServlet", asyncSupported=true) publicclass AsyncServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) { final AsyncContext ac = request.startAsync(); ac.setTimeout(...) ac.start( new Runnable(){ ac.getResponse().getWriter()...; ac.complete(); });
  • 7.
    Demo: Chat servlet ●Glassfish: asynch-request-war sample
  • 8.
    JAX-RS 1.1 ● RESTfullWeb Services ● Typical use: javascript clients ● GET/POST/PUT/DELETE resources ● URL encodes resource identification ● Encoding defined by MIME headers ● Class and/or method annotated with @Path ● Automatic JSON/XML encoding using JAXB
  • 9.
    Demo: novashop ● FromAdam Bien hacks ● http://kenai.com/projects/javaee-patterns/sources/hg/show/hacks/NovaShop ● @Path, @PathParam, @Produces, @Consumes annotation on WorkshopRegistration ● Use Chrome Advanced Rest Client
  • 10.
    JSF 2.0 ● .xhtmlview technology ● Uses JEE6 managed POJO’s ● @RequestScoped, @ConversationScoped, @Named ● New: call any method, even with parameters. ● Controllers: method calls on managed beans ● Bind form fields to bean properties – Typically @RequestScoped beans ● Implicit navigation using method’s returned value ● Redirect after post: ?faces-redirect=true
  • 11.
    JSF 2.0 inJava EE 6 ● Supports javax.validation ● All @Named CDI beans are accessible ● Custom Components will use JAX-RS
  • 12.
    Demo: novashop 2 ●From Adam Bien hacks ● http://kenai.com/projects/javaee-patterns/sources/hg/show/hacks/NovaShop ● Index.xhtml, registered.xhtml
  • 13.
    JSR-330 a.k.a. javax.inject ●@Inject, @Qualifier, @Scope, etc. ● Standardized Dependency Injection API ● Reference Implementation: Google Guice 2.0 ● Also supported by Spring since 3.0 ● Defines API, not injector implementation or configuration
  • 14.
    JSR-299 a.k.a. CDI ●Standardized dependency injection behaviour and configuration ● Required feature of any Java EE 6 container, including Web profile ● javax.enterprise.* package ● @Produces (evt. with InjectionPoint) ● Instance<> and @Alternative ● Event<> and @Observes
  • 15.
    Demo: novashop 3 ●Configurator for maxNumberOfRegistrations
  • 16.
    CDI Extensions ● CDIhas Service Provider Interfaces ● Allows extensions drop-in ● http://groups.diigo.com/group/cdi-extensions ● Seam 3 and others ● https://github.com/CDISource/cdisource ● @Spring Spring to CDI bridge ● CDI to Spring: use JNDI Lookup.
  • 17.