November 3, 2005, Pavel Buzek
This tutorial describes how to use the netbeans and glassfish application servers to create and test a simple "Hello World" Java ee 5 application, it uses the dependency Injection Technology of EJB 3.0 and JSF.
Get the preview version of netbeans Java ee 5
Access the download page and obtain a daily version of netbeans from the javaee5 Branch:
Decompress and start IDE.
Register an Application Server
The preview version uses the latest version of the open-source Java ee 5 Application Server glassfish and JBoss servers for testing.
Note: glassfish already supports servlet 2.5 and JSF 1.2 Dependency injection declarations on Java ee 5 at the web layer. If you want to use JBoss, you can test this independent EJB module and use j2se client or JNDI to query and call EJB.
Download the server. After installation, use menu tools> Server Manager in netbeans to register the server. Use Sun Java Systems Application Server as the server type to register glassfish:
Create a Java ee 5 Application
Create a new enterprise application project, select Java ee 5 Server as the server type, and Java ee 5 as the J2EE version.
IDE creates an enterprise application project, web project, and EJB project. The Enterprise Application project and web project contain the Java ee 5 deployment descriptor. The EJB module does not have any deployment descriptor-the descriptor is not required in EJB 3.0, and all metadata is written in the Declaration.
A Web application project must contain an EJB project in its class path to call ejbs. Right-click libraries in the web module project, select Add project, and browse the EJB module project.
Create an EJB 3.0 Bean
In the EJB Module Project pop-up menu, choose new> Session Bean. This will create a declared EJB and a remote or local business interface. Note that there are many differences between bean classes and common Java classes.@Stateless
Statement.
Add a Business Method to the interface:
package hi; /** * This is the business interface for HelloWorld enterprise bean. */ public interface HelloWorldRemote { public String sayHi (); }
And implement this interface in the bean class:
package hi; import javax.ejb.*; /** * This is the bean class for the HelloWorldBean enterprise bean. * Created 3.11.2005 13:14:42 * @author pbuzek */ @Stateless() public class HelloWorldBean implements hi.HelloWorldRemote { public String sayHi (){ return "Hello world!"; } }
Call EJB in a web application
Now we can call EJB from JSF to see how dependency injection is implemented in JSF managed bean.
First, go to the Project Properties of the web module and add JSF support:
This will create a filefaces-config.xml
, Add all libraries to the project's class path, and register related entriesweb.xml
And create a sample JSF pagewelcomeJSF.jsp
.
Now, useJSF managed BeanThe template creates a managed bean in the web module:
Then write the code that calls the EJB. In Java ee 5, you do not have to write any JNDI search code. Use only@EJB
Declare to identify a field, and the server will inject an EJB instance after creating the managed Bean:
package beans; import hi.HelloWorldRemote; import javax.ejb.EJB; public class Managed { @EJB() HelloWorldRemote hello; public String getGreeting () { return hello.sayHi(); } public Managed() { } }
The last step is to call the managed bean from JSF:
<body> <f:view>
Run the Hello world application.