by Geertjan WielengaThis article provides a tour of the main Java EE 6 specifications and shows how the the NetBeans IDE provides the tools, templates, and code generators to support Java EE 6.
Published March 2012
: NetBeans IDE (Java EE distribution)
When you begin to grasp the breadth and ambition of the Java EE 6 Platform, which covers everything from the model (JPA and Bean Validation), to the controller (EJB and Servlets), to the view (JavaServer Faces), a simple entry point is difficult to find.
Enter NetBeans IDE 7.1, which is Oracle’s IDE for the Java Platform, created by the same group of developers who created the Java EE 6 Platform. Here you find tools, templates, and code generators intended to be used in combination with the set of specifications that the Java EE 6 Platform encompasses.
Let’s take a tour through the main Java EE 6 specifications and see how NetBeans supports them, through a set of short explanatory texts and screenshots.
The Java Persistence specification defines how relational data is mapped to Java objects, standardizing Object/Relational mapping. In the IDE, a wizard is provided that connects to a registered database (which can also be viewed and changed in a database exploration tool in the IDE), lets you select tables, and then generates Java objects from them.
Each generated Java object is annotated as an entity following JPA 2.0 standards, a persistence.xml file defining the database connection is created, and named queries (such as "Customer.findAll", which maps to an SQL query) are optionally inserted into the Java objects.
As a result, your database is accessible from cleanly generated Java POJOs in a matter of seconds. Either you can now inspect the generated entities and extend them for your own applications (for example, code completion is available to help you get started with the new Criteria API) or you can use the generated entities as a learning device while you become comfortable with the JPA specification.
More generally, to integrate persistence solutions into your application, the IDE offers a number of different code generating mechanisms:
The Bean Validation specification is very closely related to the JPA specification. It defines an API for JavaBean validation based on annotations, such as @NotNull and @Size. Aside from the annotations generated into the entities via the above-mentioned tools, the Bean Validation API is supported via additional wizards that let you generate constraint classes, together with their related configuration files:
Earlier releases of the Enterprise JavaBeans specification were complicated and verbose. In contrast, EJB 3.1 is remarkably lightweight, requiring very little coding. Now, EJBs simply help you create a thin and powerful business layer. Think "C" in "MVC" and you've got the idea. In the IDE, when you build them, EJBs are included in the application's WAR file, instead of being archived in separate EAR files, which is one of the great enhancements in EJB 3.1.
Templates are provided by the IDE for creating new beans of various kinds: session bean, timer session bean, message-driven bean, as well as session beans generated from entity classes.
Using the IDE, you quickly see that EJBs are no longer defined by the interfaces they implement. Instead, a session bean is a simple POJO with annotations such as @Stateless and @LocalBean. When Alt-Insert is pressed inside the Java Editor, code snippets can be created, such as code for injecting the persistence.xml file via the @PersistenceContext annotation and referring to it via the JPA EntityManager class.
Further help is provided via code generators, including a wizard for creating new business methods in an EJB, invoked from "Add Business Method" above.
Finally, for each EJB in your application, a new node is added to the application structure in the Projects window, so that you can easily see the artifacts made available by the application.
Especially when the application becomes larger, and manageability concerns become more prominent, such global views into the project structure become more important.
Instead of using JSPs, you can now use XHTML files, defined by Facelets, which are now part of the JavaServer Faces (JSF) specification. One benefit is that you can now use CSS to style your view layer. Another advantage is that Facelets supports templating. In the IDE, you can use a wizard to create a Facelets Template file, after which you can use another wizard to create a client of the generated template file.
When working within these files, tools exist in the IDE for extracting a set of tags into a JSF Composite Component, which is a separate XHTML file that can be reused across multiple Facelets files.
In this way, component-based development is supported by JSF 2.0, supported by the IDE. A separate wizard is also provided in the IDE for creating new JSF composite components.
Moreover, JSF pages can be generated from entity classes. When the "JSF Pages from Entity Classes" wizard is selected, you can choose one or more entity classes, and then the IDE will generate four XHTML files for each selected class. Within the generated files, JSF tags are added, enabling common CRUD operations to be performed on the underlying entity classes.
By default, following the Java Servlets 3.0 specification, the IDE does not register a newly created servlet into the application's web.xml file. Instead, the @WebServlet annotation is added to the top of the Servlet class, with the "urlPatterns" attribute defining the URL pattern. That means you don't need to switch between the web.xml editor and the Java Editor. Modifying the URL pattern can be done very easily by changing the annotation on the class. Should a web.xml still be needed, a template exists for creating one.
Within the servlet, press Alt-Insert and then you can, as always, generate code. However, there are some powerful new additions in this code generator. Notably, a wizard can be started for selecting session beans:
Code for injecting the selected session beans is inserted into the file, providing a new @EJB annotation in the place where the above code generator was invoked.
The new Context and Dependency Injection specification lets you bind the lifecycle and interactions of stateful components to well-defined but extensible lifecycle contexts. In addition, it lets you inject components into an application in a typesafe way, including the ability to choose at deployment time which implementation of a particular interface to inject.
Since an application that uses CDI must have a file named beans.xml, the IDE provides templates for creating this file. If the file does not exist and you annotate EJBs with @Named, for example, which exposes it for injection via expression language into JSF files, the IDE automatically creates a beans.xml file in the META-INF folder for you. In a JSF Facelets file, when you use expression language to refer to your named EJB, the IDE offers code completion for the class names, as well as the methods within the class. In this way, you can very easily connect your view layer (provided by JSF Facelets files) with your business layer (provided by EJBs).
Furthermore, wizards are provided by the IDE for generating new qualifiers, stereotypes, interceptor binding types, and scope types.
These files conform to the deeper aspects of the CDI specification, helping you to get started with them quickly and avoid discouraging errors early in your learning process.
The Java API for RESTful Web Services 1.1 lets you quickly build lightweight web services that conform to the Representational State Transfer (REST) style of software architecture. JAX-RS is a standardized API for building RESTful web services in Java. In REST, you deal with resources, each of which can be referred to with a global identifier, that is, a URI.
These can be created very easily in the IDE. In a session bean, add an @Path annotation, which denotes in JAX-RS 1.1 that the artifact should be exposed as a RESTful resource, and the IDE will immediately pop up a dialog that lets you configure the REST libraries and related resources.
Also, a library of wizards is provided for generating RESTful web services from a number of different sources: entity classes, patterns, and databases.
Once the RESTful web services have been created, another wizard can be used for generating the client that consumes the web service's payload.
Moreover, for SOAP-based web services, additional wizards are available for very specific use cases, such as for generating secure token services, logical handlers, and message handlers.
While code generators and tools such as those described here are great to help you get your feet wet, a danger is that a lot of code is generated that you don't understand and that you therefore do not know how to debug and maintain. The good news is that far less code needs to be generated in Java EE 6 than before, making it far easier to understand and maintain.
Nevertheless, it is advisable to use tools of this kind intelligently. Start small, focusing on specific APIs. Get to know them via the generated code and then slowly extend the application as you become more familiar with the Java EE 6 Platform. Once you are comfortable with the spec, the tools aim to help you become more productive: combining the leanness of the Java EE 6 Platform with the tools in the IDE, you'll be rapidly creating the core of your application.
In short, the 7.1 release of NetBeans, the IDE for the Java Platform, is a great place to start learning the ins and outs of the Java EE 6 Platform.
Geertjan Wielenga began working on the NetBeans project in 2004. Originally a technical writer, today he is a product manager at Oracle.