Revision as of 14:28, 5 November 2009 by Admin (Talk | contribs)
(diff) ← Older revision | Current revision (diff) | Newer revision → (diff)

Java EE 5 (50 Minutes)



This NetBeans Day demo script is intended to be used with the associated slide deck - Java EE 5 - Did You Get Your Tools With That?. The focus of this presentation in the technology and the tool is used to support the demos.

Product Versions Supported

NetBeans 5.5

Points to Hit

  • Creating a Java EE 5 component is simply a matter of adding an annotation to a POJO.
  • NetBeans is the only tool today supporting Java EE 5, from Web Services to the Java Persistence API.

Demo Prep

  • Download and install the Java EE 5 SDK (we'll configure it in NetBeans as part of the Demo).
  • Download this style sheet and set up a NetBeans favorites folder that points to its location. We'll use this to copy it into our application.


  • None know at this point.


Demo 1: Web Services 2.0

Jave EE 5 SDK

  1. Add the SJSAS, naming it Java EE 5 SDK, to NetBeans
  2. Start the Server. You can continue with the next steps in the demo while the server is starting.

Create a Web Service
The point to drive home here is that a web service is nothing but a POJO with the @WebService annotation.

  1. Create a new Web Application Project named HelloWorldWeb.
  2. Create a new Java Class named Hello in the endpoint package. Note, you could use the web service wizard to do this, but I think it drives the POJO point home if you just create a class and annotate it with @WebService. Your complete class will look as follows:
      package endpoint;

      import javax.jws.WebService;

      public class Hello {
          public String sayHello (String name) {
              return "Hello " + name;
  1. Deploy and test the web service from NetBeans (expand the Web Services folder and right-click the HelloWorld node).
  2. Show the service can also be viewed and tested from the Java EE 5 SDKs admin console.

Create a Client to the Web Service

  1. Create a new General Java Application named SpeakToMe
  2. Add a Web Service Client, using the WSDL URL to the Hello Service (you can get this from the properties of the HelloWorld Web Services node, or the Test Web Service page). Put it in a package named wsclient. Running the Web Service Client wizard is equivalent to running wsimport from the command line, which creates the client artifacts.
  3. Switch to the File tab ans show the generated client classes.
  4. Add the following to the main method:
      HelloService service = new HelloService();
      Hello port = service.getHelloPort();
  1. Set an argument in the project properties and run the client.

Demo 2: EJB 3.0

Convert the Web Service to and EJB

  1. Create a new EJB Module named HelloWorldEJB.
  2. Copy the Hello POJO from the web module and paste it to the ejb module.
  3. Add the Stateless annotation to the Hello POJO.
  4. Deploy the project.

Update the Client
The WSDL URL has changed, but NetBeans doesn't provide a way to update the URL (See Issue 82757, so we'll quickly delete and re-create the client.

  1. Delete the Web Service Reference from the client
  2. Add a new reference using the new WSDL url
  3. Run the client

Demo 3: Java Persistence API

Here we'll collect and persist statistics on who we've said hello to.

Create the Persistence Unit
Create the Entity Class for Persisting the Statistics

  1. Create a new Java Class named Person in the endpoint package. Like with the Web Service, we're using a POJO to show how easy all this really is.
  2. Add two fields, name and visits. Annotate the class with @Entity and the name field with @Id. Note the warning given by trying to use "name" as a field name. Change it to visitor instead. The final class should look as follows:
public class Person implements Serializable {

    private String visitor;
    private int visits;
    /** Creates a new instance of Person */
    public Person() {        
    public Person(String name) {
        this.visitor = name;
    public int incrementVisit() {
        return visits++;

Update the Web Service to Persist to the Person Entity

  1. Add a persistence context, @PersistenceContext private EntityManager em; and the following code to sayHello, so the class looks like the following:
      public class Hello {
          @PersistenceContext private EntityManager em;
          public String sayHello (String name) {
              Person p = em.find(Person.class, name);
              if (p == null) {
                  p = new Person(name);
              return "Hello " + name;       

Deploy and Test

  1. Deploy the project and show that the Person table is created (as specified by our persistence unit) during deployment time.
  2. Run the client with some different names and show the results.

Demo 4: Java Server Faces

In this part of the demo we'll show off NetBeans' powerful wizards to create a CRUD application.

  1. Create a new Web Application name CustomerEditor and add the Java Server Faces framework.

Create the Entity Classes

  1. Run the Entity Classes from Database wizard
  2. Select the CUSTOMER table (which will also pull over the related DISCOUNT_CODE table).
  3. Set the Package to entity
  4. Use the button in the wizard to create the Persistence Unit. Notice the Table Generation Strategy defaults to None.
  5. Click Create and Finish.
  6. Review the generated enity classes, especially the annotations that we not used in our previous demos (@Table, @NamedQuery, @Column @JoinColumn, @ManyToOne, @OneToMany)

Create the JSP Pages

  1. Run the JSP Pages from Enity Class Wizard
  2. Select the Customer entity
  3. Set the Package to view
  4. Click Finish
  5. Review the generated JSPs, faces-config.xml and managed bean. Show how the managed bean uses dependency injection for the UserTransaction and the PersistenceUnit. Ask the audience if they know why we didn't need to inject a UserTransaction in our HelloWorld example above (Answer, EJBs provide automatic transaction management).

Run the Project

  1. Review all the CRUD features.

Polish the Project

  1. Let's add the discount percent to the customer details page. Open Detail.jsp and add the following, showing off the JSF code completion:
    <h:outputText value="Discount Percent:"/>
    <h:outputText value="#{customer.customer.discountCode.rate}" title="Discount Percent"/>

Save the file, return to the browser and refresh the details page. There's no need to redeploy.

  1. Notice the New and Edit Customer pages show an object string for the location. Change the toString() method of the DiscountCode entity to return the getRate().toString().
  2. For some final polish, add a style sheet reference. Copy it from the favorites folder you've set up to your projects Web Pages folder and then add the following to the <head> section of your JSPs:
      <link rel="stylesheet" href="../cds_NBDemoJavaEE5.css" type="text/css">
  1. Show the pagination support. Open CustomerController.java and set the batchSize to 5.

Demo Cleanup

  1. Undeploy the HelloWeb, HelloWorld and CustomerEditor applications from the Sun Java System Application Server.
  2. Delete the HelloWeb, HelloWorld, SpeakToMe and CustomerEditor projects.
  3. Delete the PERSON table from Derby.
  4. Remove the Sun Java System Application Server from NetBeans.

Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo