Java EE 6 also brought CDI to the platform. As its name implies, CDI is an implementation of the Dependency Injection principle for Java EE - among other things.

To create a new bean that will be associated with each request, the only requirement is to annotate the class with the relevant annotations:

@RequestScoped (1) @Named (2) public class NewThing extends Thing { (3) }

1 @RequestScoped is the magic annotion for the automatic instantiation and injection 2 @Named allows the bean to be accessible under a defined key in the JSP. The key is the simple name of the class, with its first character lower-cased i.e. newThing 3 I’m lazy, and I didn’t want to retype the UUID stuff, so using inheritance is acceptable here

At startup time, the application server will scan the classpath for relevant annotations. The one above will be found, and for every new request, it will create a NewThing instance.

To get access to the bean in the JSP is the matter of just writing the following EL :

${newThing.uid}

Notice there’s no usage of the request scope. That’s because the bean is not stored in the request, but bound to the CDI context! The way to access it is to use the above EL expression, which retrieves from there.