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

当前页面: 开发资料首页Netbeans 专题Integrating NetBeans with other J2EE Server Vendors

Integrating NetBeans with other J2EE Server Vendors

摘要: This tutorial outlines how you can take advantage Ant support to integrate other J2EE server vendors into the IDE. For J2EE development, the J2EE support in NetBeans 4.1 does most of the leg work for you, really leaving you to worry about generating the vendor specific deployment descriptors. I show you how to use XDoclet to generate the these descriptors.

Setting Up Your Environment

NetBeans 4.1 and the J2EE 1.4 Reference Implementation

The J2EE support in NetBeans requires installation of the J2EE RI. If you have neither the J2EE 1.4 RI nor NetBeans 4.1, download the AS 8.1 Bundle Installer.
  • Download and installed NetBeans 4.1 or the AS 8.1 Bundle Installer.
  • If you just need the J2EE 1.4 RI, download and install the J2EE 1.4 SDK and Sun Java System Application Server Platform Edition 8.1.
  • 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.

XDoclet

  • Download and extract XDoclet. Be sure to grab the xdoclet-bin, as we rely on the additional libraries provided with the samples.

Creating the Fibo Project

First, we'll use the NetBeans J2EE features to handle most of the grunt work for us. We'll create the fully functional Fibo application that would easily deploy to J2EE 1.4 SDK. Essentially, what we'll be left with is the need to create the JBoss specific deployment descriptors, for which we'll use XDoclet.

Create the FiboApp

Choose File > New Project (Ctrl+Shift+N).
    1. Select Enterprise Application under the Enterprise Category and click Next.
    2. Name the project FiboApp and click Finish.
The Wizard created 3 projects for you:
  • FiboApp - represents your ear file.
  • FiboApp-EJBModule - represents your EJB jar file
  • FiboApp-WebModule - represents your Web war file.

Create the Fibo EJB

  1. In the Projects window, right-click the FiboApp-EJBModule's node an choose New > Session Bean
  2. Name the Bean Fibo and set the package to tutorial.ejb, change the Interface to Remote (this is to stay consistent with the JBoss tutorial) and click Finish.
  3. This wizard created the bean, all of the necessary interface classes, and opened the bean for you in the source editor.
  4. Right-click anywhere in the bean class' body and choose EJB Methods > Add Business Method. Set the following:
    - Name: compute
    - Return Type: double[]
    Add a Parameter:
    - Type: int
    - Name: number
    Click OK and OK to complete the wizard.
  5. Replace the body of the compute method with the following:
        if (number < 0) {
            throw new EJBException("Argument should be positive");
        }
        double[] suite = new double[number + 1];
        suite[0] = 0;
        if (number == 0) {
            return suite;
        }
        suite[1] = 1;
        for (int i = 2; i <= number; i++) {
            suite[i] = suite[i - 1] + suite[i - 2];
        }
        return suite;
          
  6. Press F11 to build the FiboApp-EJBModule

Create the Fibo Servlet

  1. In the Projects window, right-click the FiboApp-WebModule and choose New > Servlet.
  2. Name the Servlet ComputeServlet, set the package to tutorial.web and click Next and Finish.
  3. The Wizard created the servlet and opened it up for you in the source editor.
  4. Right-click anywhere in the servlet's body and choose Enterprise Resources > Call EJB. Select the FiboSB and click OK. The wizard has generated all of the JNDI lookup code for you.
  5. Replace the body of the processRequest method with the following:
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
    
        out.println("<head><title>");
        out.println("Fibonacci Computation");
        out.println("</title></head>");
        out.println("<body>");
        out.println("<h1>");
        out.println("Fibonacci Computation");
        out.println("</h1>");
        try {
            FiboRemote bean = lookupFiboBean();
            int limit = 0;
            String value = request.getParameter("limit");
            if (value != null) {
                try {
                    limit = Integer.parseInt(value);
                } catch (Exception e) {
                }
            }
            double[] result = bean.compute(limit);
            bean.remove();
            out.println("<p>");
            out.print("The ");
            out.print(limit);
            out.print(" first Fibonacci numbers ");
            for (int i = 0; i < result.length; i++) {
            out.println("<br>");
            out.println(i);
            out.println(" : ");
            out.println(result[i]);
            }
            out.println("</p>");
        } catch (Exception e) {
            out.println(e.getMessage());
            e.printStackTrace(out);
        } finally {
            out.println("</body></html>");
            out.close();
        }
          
  6. Right click the source and choose Fix Imports (or Alt+Shift+F). This will add the tutorial.ejb.FiboRemote import.

Create the Fibo HTML Launch Page

  1. In the Projects window, right-click the FiboApp-WebModule and choose New > HTML...
  2. Name the HTML file index and click Finish.
  3. Replace the HTML source with the following:
        <html>
            <head>
                <title>
                    Fibonacci Application
                </title>
            </head>
            <body>
                <h1>Fibonacci Form</h1>
                <form action="ComputeServlet" method="POST" >
                    <table cellspacing="2" cellpadding="2" border="0">
                        <tr>
                            <td>
                                Limit :
                            </td>
                            <td>
                                <input type="text" name="limit" value="50">
                            </td>
                            </tr>
                        <tr>
                            <td>
                                <input type="submit" name="Compute" value="Compute">
                            </td>
                            <td>
                                <input type="Reset">
                            </td>
                        </tr>
                    </table>
                </form>
            </body>
        </html>
                                    
  4. Press F11 to build the FiboApp-WebModule

Generating the JBoss Specific Deployment Descriptors

At this point, we have a project that is ready to run on the J2EE 1.4 SDK. In order to deploy to JBoss, we need the JBoss specific deployment descriptors, for which we'll use XDoclet.

The Fibo EJB

  1. Open FiboBean.java and add the following XDoclet tags anywhere inside the Javadoc for the class description:
        * @ejb.bean    name="FiboBean"
        *              display-name="Name for FiboBean"
        *              description="Description for FiboBean"
        *              jndi-name="ejb/Fibo"
        *              type="Stateless"
        *              view-type="remote"
                                    
  2. Save the file (Ctrl+S).
  3. Save xdoclet-ejb-build.xml and xdoclet-ejb-build.properties in the FiboApp-EJBModule's root directory (if you're unsure where this his, select the FiboApp-EJBModule and choose Properties).
  4. The xdoclet-ejb.build.xml is generic and can be used in any project. It should provide you enough information to understand how to extend it to work with additional JBoss features or other application servers. For a full description of what's possible, see the ejbdoclet documentation on sourceforge.
  5. Switch to the Files tab (Ctrl+2) and expend the FiboApp-EJBModule node. You'll see the xdoclet-ejb-build.xml and xdoclet-ejb-build.properties files.
  6. Open the xdoclet-ejb-build.properties and make sure the xdoclet.root.dir is set appropriately.
  7. Now we just need to link the build file into the IDE for this project. Open build.xml and add the following import statement after the existing import statement:
        <import file="xdoclet-ejb-build.xml"/>
    and save the file.
  8. Right-click build.xml and choose Run Target. You'll see ejbdoclet is now part of the menu. Run it.

    ejbdoclet

  9. You'll see the following output:



  10. And jboss.xml added to your module's conf directory:


The Compute Servlet

  1. Open the ComputeServlet (in the FiboApp-WebModule) and add the following xdoclet tags anywhere inside the Javadoc for the class description:
        * @web.servlet name="Compute"
        *              display-name="Computation Servlet"
        *              description="Servlet that computes Fibonacci suite"
        *
        * @web.servlet-mapping url-pattern="/Compute"
        *
        * @web.ejb-ref name="ejb/Fibo"
        *              type="Session"
        *              home="tutorial.ejb.FiboRemoteHome"
        *              remote="tutorial.ejb.FiboRemote
        *              description="Reference to the Fibo EJB"
        *
        * @jboss.ejb-ref-jndi  ref-name="ejb/Fibo"
        *                      jndi-name="ejb/Fibo"
                                    
  2. Save the file (Ctrl+S).
  3. Save xdoclet-web-build.xml and xdoclet-web-build.properties in the FiboApp-WebModule's root directory (if you're unsure where this his, select the FiboApp-WebModule and choose Properties).
  4. The xdoclet-web.build.xml is generic can be used in any project. It should provide you enough information to understand how to extend it to work with additional JBoss features or other application servers. For a full description of what's possible, see the webdoclet documentation on sourceforge.
  5. Switch to the Files tab (Ctrl+2) and expend the FiboApp-WebModule node. You'll see the xdoclet-web-build.xml and xdoclet-web-build.properties files.
  6. Open the xdoclet-web-build.properties file and make sure the xdoclet.root.dir is set appropriately.
  7. Now we just need to link the build file into the IDE for this project.. Open build.xml and add the following import statement after the existing import statement:
        <import file="xdoclet-web-build.xml"/>
    and save the file.
  8. Right-click build.xml and choose Run Target. You'll see webdoclet is now part of the menu. Run it.

    webdoclet

  9. You'll see the following output:

    Output - click for fullsize

  10. And jboss-web.xml added to your WEB-INF directory:



Package the FiboApp ear and Deploy to JBoss

Package the FiboApp ear

In the Projects window, expand the FiboApp project node, right-click build.xml and choose Run Target > dist. This will package the fiboapp.ear.

Deploy to JBoss

  1. Save servers-build.xml and servers-build.properties in the FiboApp projects' root directory (if you're unsure where this his, select the FiboApp project and choose Properties). The servers-build.xml is generic can be used in any project.
  2. Switch to the Files tab (Ctrl+2) and expand the FiboApp project node. You'll see the servers-build.xml and servers-build.properties files.
  3. Open the servers-build.properties file and make sure the jboss.home property is set appropriately.
  4. Now we just need to link the build file into the IDE for this project.. Open build.xml and add the following import statement after the existing import statement:
                                    <import file="servers-build.xml"/>
    and save the file.
  5. Right-click build.xml and choose Run Target. You'll see jboss-start, jboss-stop and jboss-deploy and jboss-run-app are now part of the menu. Start JBoss.

    jboss-menu

  6. You'll see the output from JBoss' server log in NetBeans' Output window:

    JBoss' server log in NetBeans' Output window - click for fullsize

  7. And you'll see the JBoss process under the Runtime tab:


  8. Deploy your FiboApp.ear to JBoss. Switch to the Files tab (Ctrl+2), right-click the build.xml under the FiboApp project, and select Run Target > jboss-deploy. You'll see from the JBoss log, that the application has been deployed.

    Jboss Postdeploy Output - click for fullsize

  9. Launch the FiboApp. Right-click the build.xml under the FiboApp project and select Run Target > jboss-run-app.

Fast Track

Follow these steps to quickly get the Fibonacci application up in running in JBoss from NetBeans 4.1.
  1. Follow the steps above to Set Up Your Environment.
  2. Download and extract FiboApp.zip.
  3. Start NetBeans 4.1 and open the FiboApp project.
  4. Switch to the Files tab (Ctrl+2) and expand the FiboApp node. Open the servers-build.properties, and set your jboss.home directory. If you're on Unix, also correct the start and stop commands to run.sh and shutdown.sh.
  5. Expand the FiboApp-EJBModule node and open the xdoclet-ejb-build.properties. Set the xdoclet.root.dir.
  6. Expand the FiboApp-WebModule node and open the xdoclet-ejb-build.properties. Set the xdoclet.root.dir.
  7. Select the build.xml under the FiboApp node and run the following targets:
    1. jboss-start
    2. jboss-deploy
    3. jboss-run-app

↑返回目录
前一篇: Integrating Dreamweaver MX with NetBeans IDE
后一篇: J2EE Application Clients in NetBeans IDE