NetBeans 5.5 End-2-End Demo
Contributed and maintained by
and
, 05 Apr
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
- On the Runtime tab, expand the Databases entry by clicking the plus sign next to it.
- Connect to the Derby sample database by right-clicking its entry and choosing Connect.
- Expand the sample database entry by clicking the plus sign next to it.
- Expand the Tables entry.
- Right-click the CUSTOMER entry and choose View Data. The window that opens displays the data in that table.
- Expand the CUSTOMER entry.
- Notice the red icon next to the CUSTOMER_ID column. This indicates the primary key for the table.
- 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
- Create a new Enterprise > EJB Module Project called CustomerEJBModule
- 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
- Right-click the CustomerEJBModule project and select New > Java Class...
- For Class Name type in CustomerFacadeBean and for package specify ejb. Then click Finish.
- That's right, it's just a POJO. To make it a Stateless Session bean, just add the @Stateless annotation to the class:
- Right-click the implementation class in the source editor and select Persistence > Use Entity Manager.
- Delete the generated persist() method.
- 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;
}
- 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
- Simply add the @WebService annotation to the session bean.
- Right-click the CustomerEJBModule project and select Deploy Project.
- 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.
- 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
- Create a new Visual Web Application project, CustomerWSClient.
- 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
- Right-click CustomerWSClient project , and select New > Web Service Client
- Select the WSDL URL radio button and specify:
- Specify a package name of wsclient and then click the Finish button.
Call the Web Service Client
- Double-click the Get Discount button to open the button1_action() in the Java editor.
- Expand the Web Service References node all the way down until you see the getDiscount operation.
- Drag the getDiscount operation into the body of the button1_action() method.
- Set arg0 to getTextField1().getText().toString().
- 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:
- Right-click CustomerWSClient project and select Run Project.
- 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
- Press Alt+Shift+8 to open the Debugging Sources window.
- Select the CustomerEJBModule to use for debugging:
- 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
- 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.
- Click the Runtime tab and then expand the Servers node.
- 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.
- There is a separate entry under Resources for JDBC. Expanding it will display the JDBC resources connection pools.
View the HTTP Monitor
- 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.
- Right-click the record and choose Edit and Replay...
- 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
- 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.
- 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.
- Create a new Mobile Application Project called MobileCustomerApplication. Deselect Create Hello MIDlet and click Finish.
- Right-click the project and choose new New > J2ME Web Service Client...
- Enter the WSDL URL: and click Retrieve WSDL. Then click Finish.
- Press F6 to launch the MIDlet.
- Walk through the MIDlet to get a discount rate. The MIDlet is functional but very generic. Let's fix this.
- 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.
- Double-click the mainMenu [List] screen to open the Screen Designer. Change the text to Run Rate App.
- Use the Edited Screen combo box to switch to getDiscountRate_InputForm [Form]. Change the text to Enter Customer Name:
- Use the Edited Screen combo box to switch to getDiscountRate_OutputForm [Form]. Change the text to Discount Rate:
- 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.
- Create a new General Java Application project, RichCustomerClient.
- Right-click RichCustomerClient project , and select New > Web Service Client...
- For the WSDL URL specify:
- Select the existing package name richcustomerclient and then click the Finish button.
- Add the following method signature to Main.java (which should already be open in your editor):
static String getDiscount(String company) {
}
- Expand the Web Service References node all the way down until you see the getDiscount operation.
- Drag the getDiscount operation into the body of the getDiscount() method.
- Set arg0 to company.
- Replace the System.out.println("Result = "+result) with return result;.
- Put return ex.getMessage(); into the catch block.
So your completed method should look as follows:
- Create a new JFrame Form, RichCustomerJFrame, in the existing package, richcustomerclient.
- Use the GUI Builder to lay out the form so it looks something like the following:
- 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).
- 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);
- Switch to Main.java and add the following to the main method:
RichCustomerJFrame app = new RichCustomerJFrame();
app.setVisible(true);
- 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.
- Select Profile > Profile Main Project...
- Select Monitor Application and Enable Threads Monitoring
- 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:
- 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....
- 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.
- 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.
- 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.
- In the CustomerEJBModule select and right-click both the Customer and DiscountCode source files and select Reverse Engineer...
- Click OK on the Reverse Engineer dialog to create the CustomerEJBModule-Model.
- In the UML project, select and right-click both the Customer and DiscountCode classes in the Model and select Create Diagram From Selected Elements...
- Select Class Diagram as the Diagram Type and name the diagram CustomerClass. Click Finish.
- Explore the various features: Panning, Zooming with Marquee, Interactive Zoom, Overview Window, etc.
- Right-click the DiscountCode class and select Navigate to Source.
- Add a new method, such as:
public String getRateAsString(){
return rate.toString();
}
- Save the file.
- Right-click the class and select Reverse Engineer...
- Click OK to use the existing UML project and select Yes to overwrite the model element.
- 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.
- Open DiscountCode.java from the CustomerEJBModule project in the editor.
- Press Ctrl+Shift+U to Create Unit Tests.
- Click OK on the Create Tests dialog to generate the DiscountCodeTest class.
- There's a test case created for each method. For our purposes, let's delete all but testSetDiscountCode() and testEquals().
- In both cases remove call to fail.
- 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());
}
- 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);
}
- 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
- Click Runtime tab.
- Under SJS AS's Applications undeploy CustomerWSClient and CustomerEJBModule.
- Delete the four projects.
Other Features to Explore
- Ant Debugger
- App Server Integration:
- View Server Log
- Launch Admin Console