Products Docs & Support Community

NetBeans 5.5 End-2-End Demo

Overview

This demo is intended to be used in conjunction with NetBeans 5.5. If you are using NetBeans 5.0, please use this version.

This demo is designed to showcase NetBeans 5.5's end-2-end application development support - Java EE to Java SE to Java ME. Along the way, we'll showcase many of the useful features found in NetBeans, such as refactoring, debugging, HTTP monitoring and profiling. We've taken the time to provide this script as a resource for anyone interested in showing off NetBeans to the rest of the world.

We're going to build a little application that determines the discount rate we give to our various customers. Our application is going to have three client interfaces: a web interface, a rich client interface and a MIDlet interface for our mobile device. We start with the Java DB (Derby) sample database that's included with the Java EE 5 SDK . We use a NetBeans wizard to create entity beans against the CUSTOMER table. Then we create a session bean facade to the entity bean that has a single method to provide a given customer's discount rate and we then expose that method via a web service operation. It is from this web service that we then create our 3 client interfaces.

Demo Prep

NetBeans 5.5 and the Java EE 5 Reference Implementation

The Java EE support in NetBeans requires installation of the Sun Java System Application Server (the Java EE 5 Reference Implementation)

  • If you have neither NetBeans 5.5 nor the Java EE 5 SDK, download and install the Java EE 5 Tools Bundle.
  • If you already have NetBeans 5.5 but not the J2EE 1.4 SDK, download and install the Sun Java System Application Server Platform Edition 9.0. Then, start NetBeans and switch to the Runtime tab (Ctrl+5). Right-click the Servers node and add the Sun Java System Application Server.
  • Configure the HTTP Monitor and Start the Application Server in Debug Mode:
    • In the Runtime tab, right-click the Sun Java System Application Server entry and select Properties.
    • Check the Enable HTTP Monitor checkbox and then click the OK button.
    • Start the Application Server in Debug Mode.

Visual Web Pack

  • Download and install the NetBeans Visual Web Pack 5.5

Mobility Pack for CLDC

  • Download and install the NetBeans Mobility Pack for CLDC

Profiler

  • Download and install the NetBeans Profiler 5.5

UML

  • Install the UML module from the NetBeans Update Center

Demo

 

Use the Database Explorer

  1. On the Runtime tab, expand the Databases entry by clicking the plus sign next to it.
  2. Connect to the Derby sample database by right-clicking its entry and choosing Connect.



  3. Expand the sample database entry by clicking the plus sign next to it.
  4. Expand the Tables entry.
  5. Right-click the CUSTOMER entry and choose View Data. The window that opens displays the data in that table.
  6. Expand the CUSTOMER entry.
  7. Notice the red icon next to the CUSTOMER_ID column. This indicates the primary key for the table.


  8. Expand the Foreign keys entry and then expand the two entries under it. You'll see the CUSTOMER is dependent on the DISCOUNT_CODE table.

Create an EJB Module and Entity Beans

  1. Create a new Enterprise > EJB Module Project called CustomerEJBModule
  2. Right-click the CustomerEJBModule project and select New > Entity Classes from Database...
  • For Data Source select jdbc/sample.
  • Select the CUSTOMER table and Add it to the Selected Tables column. You'll notice the related DISCOUNT_CODE table comes along as well.
  • Click Next.
  • Set the Package to entity.
  • Click Create Persistence Unit... and click Create on the Create Persistence Unit dialog.
  • Click Finish.
  • Notice the following have been created by the wizard:
    • Under Source Packages, to Entity classes, Customer and DiscountCode.
    • Under Configuration Files, the persistence.xml. Show the nice graphical editor for persistence.xml.
  • Build the CustomerEJBModule project.
  • Create a Session Bean that accesses the Entity Beans

    1. Right-click the CustomerEJBModule project and select New > Java Class...
    2. For Class Name type in CustomerFacadeBean and for package specify ejb. Then click Finish.
    3. That's right, it's just a POJO. To make it a Stateless Session bean, just add the @Stateless annotation to the class:

    4. Right-click the implementation class in the source editor and select Persistence > Use Entity Manager.
    5. Delete the generated persist() method.
    6. Create the following getDiscount() method. If you type instead of copying and pasting, you will be able to show code completion:
          public String getDiscount(String name) {
      String discount = "-1"; //No match
      Query query = em.createNamedQuery("Customer.findByName");
      query.setParameter("name", name);
      List <Customer> customers = query.getResultList();
      if (!customers.isEmpty()) {
      Customer c = customers.get(0); // #Get the first matching customer
      discount = c.getDiscountCode().getRate().toString();
      }
      return discount;
      }
    7. Press Alt+Shift+F to add the import for Customer, Query and List. Alternatively, place your cursor on the line with the error, wait for the light bulb to appear and press Alt+Enter to add the import.


    Expose the Session Bean as a Web Service

    1. Simply add the @WebService annotation to the session bean.



    2. Right-click the CustomerEJBModule project and select Deploy Project.
    3. Expand the Web Services node of the project. Right-click the CustomerFacadeBean and select Test Web Service. This will launch the application server's Web Service Tester.
    4. Enter some values (Nano Apple, Ford Motor Co) and click the getDiscount button to test the web service.

    Create a Web Client that accesses the Web Service

    This section assumes you already have some basic familiarity with the Visual Web Pack. If you need an introduction, you may want to first read the Getting Started With NetBeans Visual Web Pack 5.5.

    Design the Page

    1. Create a new Visual Web Application project, CustomerWSClient.
    2. Drop the necessary components on the page. Feel free to get more artistic then what we've come up with below:



    Create the Web Service Client

    1. Right-click CustomerWSClient project , and select New > Web Service Client
    2. Select the WSDL URL radio button and specify:
    3. Specify a package name of wsclient and then click the Finish button.


    Call the Web Service Client

    1. Double-click the Get Discount button to open the button1_action() in the Java editor.
    2. Expand the Web Service References node all the way down until you see the getDiscount operation.
    3. Drag the getDiscount operation into the body of the button1_action() method.
    4. Set arg0 to getTextField1().getText().toString().
    5. Replace the System.out.println("Result = "+result) with the following:

      getStaticText1().setText(arg0 + " gets a " + result + "% discount.");

      So your completed method should look as follows:



    6. Right-click CustomerWSClient project and select Run Project.
    7. A browser will be started with the URL. The JSP Page will be displayed. Enter a customer name (e.g. Nano Apple) and click the Get Discount button.

    Debugging Across Projects

    1. Press Alt+Shift+8 to open the Debugging Sources window.
    2. Select the CustomerEJBModule to use for debugging:


    3. Switch to the JSP view of Page1 and set break point. Set a second break point in the getDiscount() method of CustomerFacadeBean.java. Then right-click CustomerWSClient and select Debug Project. When the debugger encounters your breakpoints, the NetBeans window will be displayed. Use the debugger to step through both the JSP and EJB code.

    View The Generated Artifacts

    1. In NetBeans, click the Files tab. Under each project there is a dist entry. Expand those dist entries to see the different files that were created.
    2. Click the Runtime tab and then expand the Servers node.
    3. The appropriate applications have been deployed to the Sun Java System Application Server. Expand the Applications entry and then expand the EJB Applications entry to display the CustomerEJBModule entry and the Web Applications to display the CustomerWS and CustomerWSClient applications . All of these entries can also be expanded, so you can view their components.
    4. There is a separate entry under Resources for JDBC. Expanding it will display the JDBC resources connection pools.


    View the HTTP Monitor

    1. In NetBeans the HTTP Monitor is displaying records on the left (if you don't see the HTTP monitor, check demo prep above). Click on the record for the customer that was just requested. The selected record will then be displayed on the right.
    2. Right-click the record and choose Edit and Replay...


    3. Change the value of the name parameter (e.g. Ford Motor Co) and then click Send HTTP Request. The browser will then display the result of the request.

    Refactoring Support

    1. In the Projects window, expand the Web Pages folder of CustomerWSClient, right-click Page1.jsp and select Rename. Change the Name to DiscountPage. In the preview window that is displayed you can see that NetBeans handles all the difficult Java EE refactoring issues, including references to Page1.jsp in our Java and XML files.


    2. Do the refactoring and run CustomerWSClient again to test the application using the refactored JSP name.

    Create a MIDlet to Access the J2EE App

    This section assumes you already have some basic familiarity with the Mobility Pack. If you need an introduction, you may want to first read the NetBeans Mobility Pack For MIDP/CLDC 5.5 Quick Start Guide.

    1. Create a new Mobile Application Project called MobileCustomerApplication. Deselect Create Hello MIDlet and click Finish.
    2. Right-click the project and choose new New > J2ME Web Service Client...
    3. Enter the WSDL URL: and click Retrieve WSDL. Then click Finish.
    4. Press F6 to launch the MIDlet.
    5. Walk through the MIDlet to get a discount rate. The MIDlet is functional but very generic. Let's fix this.
    6. Double-click the CustomerFacadeBean_StubMIDlet tab in the editor to maximize the view. Review it's contents. Double-click the tab again to return to normal view.
    7. Double-click the mainMenu [List] screen to open the Screen Designer. Change the text to Run Rate App.
    8. Use the Edited Screen combo box to switch to getDiscountRate_InputForm [Form]. Change the text to Enter Customer Name:
    9. Use the Edited Screen combo box to switch to getDiscountRate_OutputForm [Form]. Change the text to Discount Rate:
    10. Press F6 to launch the MIDlet and notice your changes.

    Create a Rich Client that accesses the Web Service

    This section assumes you already have some basic familiarity with the NetBeans GUI Builder. If you need an introduction, you may want to first read the GUI Building in NetBeans IDE 5.5 tutorial.

    1. Create a new General Java Application project, RichCustomerClient.
    2. Right-click RichCustomerClient project , and select New > Web Service Client...
    3. For the WSDL URL specify:
    4. Select the existing package name richcustomerclient and then click the Finish button.
    5. Add the following method signature to Main.java (which should already be open in your editor):
      static String getDiscount(String company) {

      }
    6. Expand the Web Service References node all the way down until you see the getDiscount operation.
    7. Drag the getDiscount operation into the body of the getDiscount() method.
    8. Set arg0 to company.
    9. Replace the System.out.println("Result = "+result) with return result;.
    10. Put return ex.getMessage(); into the catch block.

      So your completed method should look as follows:

    11. Create a new JFrame Form, RichCustomerJFrame, in the existing package, richcustomerclient.
    12. Use the GUI Builder to lay out the form so it looks something like the following:


    13. Name the components so they match the following. Note the jLabelResult which is placed in the space at the bottom of the form (but void of text until there's a result to display).


    14. Right-click the Get Discount button and choose Events > Action > actionPerformed and add the following code:
          String company = jTextFieldCompanyName.getText();
      String discount = company + " gets a " + Main.getDiscount(company) + "% discount.";
      jLabelResult.setText(discount);
    15. Switch to Main.java and add the following to the main method:
          RichCustomerJFrame app = new RichCustomerJFrame();
      app.setVisible(true);
    16. Press F6 to run the rich client.


    Analyze Performance

    This section assumes you already have some basic familiarity with the NetBeans Profiler. If you need an introduction, you may want to first read the NetBeans Profiler 5.5 Tutorial.

    1. Select Profile > Profile Main Project...
    2. Select Monitor Application and Enable Threads Monitoring
    3. Keep an eye on the AWT-EventQueue-0 thread. Enter a company name such as Nano Apple and select Get Discount. Notice the thread switches from Wait mode to Running:



    4. Open RichCustomerJFrame.java in the editor (Source view) and place your cursor somewhere in the jButtonGetDiscountActionPerformed() method. Then select Tools > Add As Profiling Root Method....
    5. Press Alt+Shift+F2 to modify profiling. Select Analyze Performance. Part of Application with our selected method should already be set for us. Click OK.
    6. Return to the Company Discount Tool and enter another company name such as Ford Motor Co and select Get Discount. Watch the Basic Telemetry in NetBeans as the threads are dynamically instrumented. The first time will take a couple of seconds while the methods are instrumented. Subsequent executions will be much faster.


    7. Look at the Live Results. To no surprise, the java.net.SocketInputStream.read() is taking over 90% of the execution time as it waits for the web service to respond.



    Diagram our Entities

    This section assumes you already have some basic familiarity with the NetBeans UML Module . If you need an introduction, you may want to first read the UML Modeling Module Documentation.

    1. In the CustomerEJBModule select and right-click both the Customer and DiscountCode source files and select Reverse Engineer...



    2. Click OK on the Reverse Engineer dialog to create the CustomerEJBModule-Model.
    3. In the UML project, select and right-click both the Customer and DiscountCode classes in the Model and select Create Diagram From Selected Elements...
    4. Select Class Diagram as the Diagram Type and name the diagram CustomerClass. Click Finish.
    5. Explore the various features: Panning, Zooming with Marquee, Interactive Zoom, Overview Window, etc.
    6. Right-click the DiscountCode class and select Navigate to Source.
    7. Add a new method, such as:
      public String getRateAsString(){
          return rate.toString();
      }
      
    8. Save the file.
    9. Right-click the class and select Reverse Engineer...
    10. Click OK to use the existing UML project and select Yes to overwrite the model element.
    11. Switch back to the CustomerClass diagram to see that it has recognized your new method.

    Unit Testing

    Since everything in Java EE 5 is now a POJO, it's very amenable to unit testing.

    1. Open DiscountCode.java from the CustomerEJBModule project in the editor.
    2. Press Ctrl+Shift+U to Create Unit Tests.
    3. Click OK on the Create Tests dialog to generate the DiscountCodeTest class.
    4. There's a test case created for each method. For our purposes, let's delete all but testSetDiscountCode() and testEquals().
    5. In both cases remove call to fail.
    6. Modify testSetDiscountCode() as follows:
      public void testSetDiscountCode() {
          System.out.println("setDiscountCode");
      String discountCode = "H"; DiscountCode instance = new DiscountCode(); instance.setDiscountCode(discountCode); assertEquals(discountCode, instance.getDiscountCode()); }
    7. Modify testEquals() as follows:
      public void testEquals() {
          System.out.println("equals");              
      Object object = null; DiscountCode instanceA = new DiscountCode(); instanceA.setDiscountCode("L"); DiscountCode instanceB = new DiscountCode(); instanceB.setDiscountCode("L");
      boolean expResult = true; boolean result = instanceA.equals(instanceB); assertEquals(expResult, result);
      instanceB.setDiscountCode("H"); result = !instanceA.equals(instanceB); assertEquals(expResult, result); }
    8. Press Shift+F6 to run the tests:





    The Complete Application

    End2End.zip contains all four projects. After performing the Demo Prep, just deploy the CustomerEJBModule and then run the 3 clients.

    Cleanup

    1. Click Runtime tab.
    2. Under SJS AS's Applications undeploy CustomerWSClient and CustomerEJBModule.
    3. Delete the four projects.

    Other Features to Explore

    • Ant Debugger
    • App Server Integration:
      • View Server Log
      • Launch Admin Console