当前页面: 开发资料首页 → 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).
- Select Enterprise Application under the Enterprise Category and
click Next.
- 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
- In the Projects window, right-click the FiboApp-EJBModule's node
an choose New > Session Bean
- 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.
- This wizard created the bean, all of the necessary interface
classes, and opened the bean for you in the source editor.
- 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.
- 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;
- Press F11 to build the FiboApp-EJBModule
Create the Fibo Servlet
- In the Projects window, right-click the FiboApp-WebModule and
choose New > Servlet.
- Name the Servlet ComputeServlet, set the package to tutorial.web
and click Next and Finish.
- The Wizard created the servlet and opened it up for you in the
source editor.
- 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.
- 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();
}
- 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
- In the Projects window, right-click the FiboApp-WebModule and
choose New > HTML...
- Name the HTML file index and click Finish.
- 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>
- 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
- 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"
- Save the file (Ctrl+S).
- 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).
- 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.
- 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.
- Open the xdoclet-ejb-build.properties and make sure the xdoclet.root.dir
is set appropriately.
- 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.
- Right-click build.xml and choose Run Target. You'll see ejbdoclet
is now part of the menu. Run it.
- You'll see the following output:

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

The Compute Servlet
- 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"
- Save the file (Ctrl+S).
- 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).
- 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.
- 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.
- Open the xdoclet-web-build.properties file and make sure the
xdoclet.root.dir is set appropriately.
- 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.
- Right-click build.xml and choose Run Target. You'll see webdoclet
is now part of the menu. Run it.

- You'll see the following output:

- 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
- 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.
- 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.
- Open the servers-build.properties file and make sure the jboss.home
property is set appropriately.
- 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.
- 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.

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

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

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

- 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.
- Follow the steps above to Set Up Your
Environment.
- Download
and extract FiboApp.zip.
- Start NetBeans 4.1 and open the FiboApp project.
- 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.
- Expand the FiboApp-EJBModule node and open the
xdoclet-ejb-build.properties. Set the xdoclet.root.dir.
- Expand the FiboApp-WebModule node and open the
xdoclet-ejb-build.properties. Set the xdoclet.root.dir.
- Select the build.xml under the FiboApp node and run the following
targets:
- jboss-start
- jboss-deploy
- jboss-run-app