Java EE 5 with NetBeans 6.0 ( Minutes)



This demo will give a high level overview of the Java EE 5 support built into NetBeans

Product Versions Supported

  • NetBeans dev build 20071007 or greater (this is newer than Beta 1) with "Web and Java EE" support. The SJSAS 9 must be installed to make use of the "sample" database.

Points to Hit

  • NetBeans supports Java EE 5 annotations
  • NetBeans makes it easy to create and consume web services
  • NetBeans supports JPA.
  • NetBeans supports REST web services.

Demo Prep

  • Make sure the "REST web service" support is installed.


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

Create a Web Service

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:

Explanation Point:
If you use code completion for "@WebService", the import will be added for you.

      package endpoint;

      import javax.jws.WebService;

      public class Hello {
          public String sayHello (String name) {
              return "Hello " + name;
  • click on the Design view and show how you can now visually edit the web service.

3. Deploy and test the web service from NetBeans (expand the Web Services folder and right-click the HelloWorld node).


4. Show the service can also be viewed and tested from the Java EE 5 SDKs admin console.


Create a Client to the Web Service

5. Create a new General Java Application named SpeakToMe

6. 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.

7. Switch to the File tab and show the generated client classes.

8. Add the following to the main method:

      HelloService service = new HelloService();
      Hello port = service.getHelloPort();

9. Set an argument in the project properties and run the client.

EJB 3.0

Convert the Web Service to and EJB

10. Create a new EJB Module named HelloWorldEJB.

11. Copy the Hello POJO from the web module and paste it to the ejb module.

12. Add the Stateless annotation to the Hello POJO.

Explanation Point:
Since the web service will have the same endpoint URL "HelloService/Hello" as the HelloWorldWeb web service, we need to undeploy the HelloWorldWeb web application.

13. Go to the Services window and select Servers->Glassfish v2->Applications->Web Applications->HelloWorldWeb, right-click and select Undeploy.

14. Deploy the HelloWorldEJB project.

15. Right-click on the HelloWorldEJB->Web Services->HelloService node and select Test Web Service.

16. Test the web service by typing a name in.

Java Persistence API

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

17. On the HelloWorldEJB project, right-click and choose "new->Persistence Unit".

18. Select "jdbc/sample" for the "Data Source:" and make sure "Table Generation Strategy" is set to "Create".


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

19. 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.

20. 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++;
Explanation Point:
Now we will update the Web Service to Persist to the Person Entity

21. 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;       

22. Deploy the project and show that the Person table is created (as specified by our persistence unit) during deployment time.

23. Run the web service tester with the same name twice and show the new table and the data in the table.

REST Web Services

24. Create a new web->web application project named "CustomerDB".

25. Right-click on the project and select "new->Entity Classes From Database...".

26. Choose the "jdbc/sample" database for the Data Source and the Customer table for the Selected Tables:.

27. Press "next" and enter entity for the package name.

28. Press the "Create Persistence Unit..." button.

29. Take the defaults and press the "Create" button.

30. Press "Finish".

31. right-click on the "entity" package and choose "__new->RESTful Web Services from Entity Classes" (note that this might be under "Other:->web services" if never used).

32. Click "Next", select "Add Add >>" and select "next", take the defaults and press "Finish".

33. Right-click on the project node and choose "_Test RESTful Web Services".

34. Show the xml GET of the customer table.

Demo Cleanup

  1. Undeploy the HelloWeb and HelloEJB applications from the Sun Java System Application Server.
  2. Delete the HelloWorldWeb, HelloWorldEJB, SpeakToMe projects.
  3. Delete the PERSON table from Derby.
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