站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Netbeans 专题NetBeans 4.1 End-2-End Demo

NetBeans 4.1 End-2-End Demo

摘要: NetBeans 4.1 End-2-End Demo

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

This demo is designed to showcase NetBeans 4.1's end-2-end application development support - J2EE to J2SE to J2ME. Along the way, we'll showcase many of the useful features found in NetBeans, such as refactoring and HTTP monitoring. 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 start by showing NetBeans' support for creating EJBs by creating a CMP entity bean. We then create a simple Java object to show how easy it is to call the EJB. Then we create a JSP to show NetBeans support for JSP syntax and the Java Standard Tag Library (JSTL). Along the way we'll demonstrate the HTTP monitor, refactoring support and the graphical deployment descriptor editors. We'll then expose the J2EE application as a web service. Finally, we'll create a MIDlet that accesses the J2EE application and showcases the MIDlet visual designer.

Demo Prep

NetBeans 4.1 and the J2EE 1.4 Reference Implementation

The J2EE support in NetBeans requires installation of the J2EE 1.4 SDK.

  • If you have neither NetBeans 4.1 nor the J2EE 1.4 SDK, download and install the AS 8.1 Bundle Installer.
  • If you already have NetBeans 4.1 but not the J2EE 1.4 SDK, download and install the J2EE 1.4 SDK and Sun Java System Application Server Platform Edition 8.1. Then, start NetBeans and switch to the Runtime tab (Ctrl+5). Right-click the Servers node and add the Sun Java System Application Server 8.1.
  • 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.

Mobility Pack

  • Download and install the NetBeans 4.1 Mobility Pack

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 Pointbase sample database by right-clicking its entry and choosing Connect. The default password is pbpublic.
  3. Expand the sample database entry by clicking the plus sign next to it.
  4. Expand the Tables entry.
  5. Right-click the CUSTOMER_TBL entry and choose View Data. The window that opens displays the data in that table.
  6. Expand the CUSTOMER_TBL entry.
  7. Notice the red icon next to the CUSTOMER_NUM column. This indicates the primary key for the table.
  8. Expand the Foreign Keys entry and then expand the two entries under it.
  9. The CUSTOMER_TBL is dependent on DISCOUNT_CODE_TBL and MICRO_MARKETS_TBL

Create a Project and an EJB with Container Managed Persistence

  1. Create a new Enterprise Application Project called CustomerApp
  2. Notice that a total of three projects were created: CustomerApp, CustomerApp-EJBModule, and CustomerApp-WebModule. In terms of eventual deployment, CustomerApp represents the Enterprise ARchive (.ear file). It's really just a project that represents a deployable object. CustomerApp-EJBModule represents the Java ARchive (.jar file) and contains all of the application's EJBs. CustomerApp-WebModule represents the Web ARchive (.war file) and contains all of the web tier components (servlets, JSPs, HTML, etc.).
  3. Right-click the CustomerApp-EJBModule entry and select New > CMP Entity Beans from Database...
    • For JDBC Connection select the sample Pointbase database.
    • Set the package name to foo and then click Next.
    • Select the CUSTOMER_TBL table and click Finish.
  4. Notice the following have been created by the wizard:
    • Under Enterprise Beans, three EJBs, one each for: the Customer, Discount, and MicroMarkets tables.
    • Under Configuration Files, the EJB deployment descriptors.
    • Under Server Resources, the JDBC resource and connection pool properties.
    • Under Source Packages, the generated implementation class and interfaces for each of the EJBs.
  5. Build the CustomerApp-EJBModule project.

Create a Session Bean that accesses the CMP

  1. Right-click CustomerApp-EJBModule, select New > Session Bean...
  2. For Class Name type in CustomerFacade and for package specify foo. Create both Remote and Local interfaces. Then click Finish.
  3. Right-click the implementation class that opened in the editor and choose EJB Methods > Add Business Method...
    1. Name: getDiscount
    2. Return Type: float
    3. Add a parameter of type String that is called name.
  4. Right-click the implementation class again and select Enterprise Resources > Call Enterprise Bean. Select the CustomerTblEJB and then click OK.
  5. Add the following inside the getDiscount() method. If you type instead of copying and pasting, you will be able to use code completion:
    public float getDiscount(String name) {
        float rate = 0;
        try {
            Iterator customers = lookupCustomerTblBean().findByName(name).iterator();
            CustomerTblLocal cust = (CustomerTblLocal) customers.next();
            rate = cust.getDiscountCode().getRate().floatValue();
        } catch (Exception e) {
            System.out.println("Ex: " + e);
        }
        return rate
  6. Press Alt+Shift+F6 to add the import for Iterator.

Create a Locator Class for the Session Bean

The locator allows us easily call the session bean from our JSP below.

  1. Right-click CustomerApp-WebModule, select New > Java Class
  2. For the Class Name type in Locator and for the package specify foo.
  3. Right-click the implementation class again and select Enterprise Resources > Call Enterprise Bean. Select the CustomerFacadeSB and then click OK.
  4. Change the lookupCustomerFacadeBean() method from private to public.

Create a JSP that accesses the Session Bean

  1. Right-click the libraries node of the CustomerApp-WebModule project and add the JSTL 1.1 library.
  2. Expand the Web Pages node of the CustomerApp-WebModule project and open index.jsp
  3. Add the following page directive after the two that are at the top of the file: <%@page import="foo.*">. Type this in so the audience can see the JSP code completion.
  4. Uncomment the JSTL taglib directive
  5. Remove the HTML code that is generated by default in the body of the JSP.
  6. Add the following in the body of the JSP. If you type instead of copying and pasting, you will be able to use code completion:
    <form method="get">
        Enter customer name: <input type="text" name="name">
        <br>
        <input type="submit" value="Ok">
    </form>
    
    <c:if test="${!empty param.name}">
    <% Locator l = new Locator();
    CustomerFacadeLocal c = l.lookupCustomerFacadeBean();%>
    <c:set var="name" value="${param.name}"/>
    <jsp:useBean id="name" type="java.lang.String" /> ${param.name} gets a <%= c.getDiscount(name) %>% discount. </c:if>
  7. Right-click CustomerApp and choose Verify Project to verify J2EE compliance.
  8. Run the CustomerApp project.
  9. A browser will be started with the URL http://localhost:8081/CustomerApp-WebModule/. The JSP Page will be displayed. Enter a customer name (e.g. MicroApple) and click the Submit button.
  10. 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.
  11. Click the Runtime tab and then expand the Servers node.
  12. The appropriate applications have been deployed to the Sun Java System Application Server. Expand the Applications entry and then expand the Enterprise Applications entry to display the customerapp entry. The customerapp entry can also be expanded, where you will then see the web and EJB modules listed.
  13. There is a separate entry for JDBC. Expanding it will display the JDBC resources connection pools.
  14. Optionally, set a break point in index.jsp and in the getDiscount() method of CustomerFacade.java. Then right-click CustomerApp 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 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.

View the Deployment Descriptor Editors

  1. In the Projects window, expand the CustomerApp-EJBModule, and then expand the Enterprise Beans entry, the CustomerTblEB entry, and the Local Methods entry.
  2. Double-click the findByName method to view the ejb-jar.xml deployment descriptor.
  3. The General view of the deployment descriptor editor is displayed. Click the Edit to view the EJBQL. Then click the Cancel button.
  4. Click the CMP Relationships button at the top to display the CMP Relationships view. Select one and click Edit to view its properties. Then click the Cancel button.
  5. Click the XML button at the top to view the raw XML.

Refactoring Support

  1. In the Projects window, expand the CustomerApp-EJBModule, and then expand the Enterprise Beans entry, the CustomerTblEB entry, and the Local Methods entry.
  2. Right-click the findByName method and select Refactor > Rename.... Change the name to findByCustomerName. In the preview window that is displayed you can see that NetBeans handles all the difficult J2EE refactoring issues: in particular the lack of standard inheritance and the updating of deployment descriptors.
  3. Press F6 to run the application again.

Create a Web Service against the Session Bean

  1. Create a new Web Application project, CustomerWS
  2. Right-click CustomerWS project , and select New > Web Service.... Call the new web service CustomerWebService and specify package foo.
  3. Expand the the CustomerWS's Web Services entry and then right-click the CustomerWebSerivce entry and choose Add Operation. (you can also right-click in the Impl).
    1. Name: getDiscountRate, Type: float
    2. Parameter Type: String, Name: name
    3. Exception: Exception
  4. Right-click and choose Enterprise Resources > Call Enterprise Bean. Select the CustomerFacadeSB, set the Referenced Interface to Remote and then click OK.
  5. Add the following code to the getDiscountRate() method:
    float rate = 0;
    try {
    CustomerFacadeRemote customer = lookupCustomerFacadeBean();
    rate = customer.getDiscount(name);
    } catch (Exception e) {
    e.printStackTrace();
    }
    return rate;
  6. Right-click the CustomerWS project and select Deploy Project so that the new web service will be deployed.
  7. Expand the the CustomerWS's Web Services entry and then right-click the CustomerWebService entry and choose Add to Registry....
  8. Click the OK button of the WSDL Url dialog. This adds the newly defined web service to a local registry maintained by NetBeans.
  9. Click the Runtime tab.
  10. Expand the Web Services node until you get to the getDiscountRate entry.
  11. Right-click getDiscountRate and select Test Operation.
  12. Enter some values (MicroApple, Ford Motor Co) and click the Submit button in order to test the web service.

Create a Web Service Client that accesses an the Web Service

  1. Create a new Web Application project, CustomerWSClient.
  2. Right-click CustomerWSClient project , and select New > Web Service Client
  3. For the WSDL URL specify: http://localhost:8080/CustomerWS/CustomerWebService?WSDL
  4. If necessary in your environment, click the Proxy Settings... button and specify an HTTP host and port number.
  5. Click the Retrieve WSDL button.
  6. Specify a package name of foo and then click the Finish button.
  7. Right-click CustomerWSClient project , and select New > Servlet....
  8. For Class Name specify RateCheckServlet and for package specify foo. Then click the Next button. Then click the Finish button.
  9. In the processRequest() method of RateCheckServlet.java, right-click and then select Web Service Client Resources > Call Web Service Operation. Select the getDiscountRate entry from the dialog and then click OK.
  10. In addition to generating the lookup methods, NetBeans creates a simple invocation of the method. Replace that with this code:
  11. out.println("<H1>Discount Rate Web Service</H1>");
    out.println("<H2>Enter company name:</H2>");
    out.println("<form method=\"get\">");
    out.println("<input type=\"text\" name=\"name\" size=\"25\">");
    out.println("<br>");
    out.println("<p>");
    out.println("<input type=\"submit\" value=\"Get Discount\">");
    out.println("</form>");
    String name = request.getParameter("name");
    if ( name != null ) {
    try {
    float result = getCustomerWebServiceSEIPort().getDiscountRate(name);
    out.println("<p>" + name + " gets a : <b>" + result + " %</b> discount.</b>");
    } catch(java.rmi.RemoteException ex) {
    out.println("<p>Caught an exception <p>" + ex);
    }
    out.close();
    }

  12. Save the changes to the file.
  13. Set the Relative URL property of the CustomerWSClient project to /RateCheckServlet
  14. Right-click CustomerWSClient project and select Run Project.
  15. Enter a company name (MicroApple, Ford Motor Co) and then click the Get Discount button.

Create a MIDlet to Access the J2EE App

  1. Create a new Mobile Application Project called MobileCustomerApplication. Deselect Create Hello MIDlet.
  2. Right-click the project and choose new File/Folder > MIDP > Wireless Connection Wizard
    Web Project Information
    1. Make sure CustomerWS is the Web Application
    2. Set the Servlet Class Name to EndToEndCustomerServlet
    Exported Services
    1. Select CustomerWebServiceImpl > getDiscountRate
    Client Name and Location
    1. Set the Client Class Name to EndToEndCustomerClient
    2. Package: foo
  3. Redeploy the CustomerWS. This is necessary because the wireless connection wizard created a servlet and supporting classes in the WebModule.
  4. Press F6 to launch the MIDlet.
  5. Walk through the MIDlet to get a discount rate. The MIDlet is functional but very generic. Also, there's no way to quit the application. Let's fix this.
  6. Double-click EndToEndCustomerClientMVD.java to open the visual editor.
  7. Double-click the EndToEndCustomerClientMVD tab to maximize the view. Review it's contents.
  8. Double-click the list1 screen to open the Screen Designer
    1. Change the text to Run Rate App
    2. Drag an Exit Command to the form edit it's action to Exit the application
    3. Click Flow Design
  9. Use the Edited Screen combo box to switch to form1
    1. Change the text to Enter Customer Name
  10. Click the Source button and change "Method execution result" to "Discount Rate"
  11. Press F6 to launch the MIDlet and notice your changes.

Cleanup

  1. Click Runtime tab.
  2. Under Web Services delete the CustomerWebService entry.
  3. Under SJS AS's Enterprise Applications Undeploy customerapp
  4. Close the three projects.
  5. Delete the three project directories.

Other Features of Interest

 


↑返回目录
前一篇: MIDP Deployment Options
后一篇: NetBeans Google Toolbar Module Tutorial