Preparation: Install netbean and Sun Company Application Server: sjsas_pe-9_0_01-nb-5_5-win.exe start development environment: [start]-> [Program]-> [netbeans 5.5]-> [netbeans Ide ], the following interface is displayed after startup:
Part 1: create a separate session beanObjective: To create a session bean that has a business method that returns a string "Hello !".
1. Create a projectSelect File> new. The following interface is displayed: Select enterprise in the middle of the interface, and then select EJB module on the right ], select [next] To Go To The following interface: Enter the project name in this interface, where "hellobean" is used ". Select Sun Java System Application Server 9 as the server. Select "Java ee 5" for Java EE version, and click Finish. The following interface is displayed. To create a session bean, the related files are stored in the source package.
Create a package for storing class (including interface) filesRight-click source packages and choose new> JAVA package ...], As shown in. After confirmation, the following interface is displayed. Enter the package name "session. Hello" and click "finish" to create the package. The package name should be in lower case.
Create Session BeanRight-click the project and choose [new] à [Session Bean ...] Then enter the following interface. Enter the following information in the interface: EJB name: hellosessionpackage: the location where the file is stored session type: defines whether it is stateful or stateless ). Create interface: If remote access is provided, select remote interface. If remote access is provided for local access, select local. Click Finish to complete the configuration. The following interface is displayed:
Compile Business Methods
Modify Bean classDouble-click hellosessionbean. Java and add the following method: Public String Hello () {return new string ("Hello! ");} The modified hellosessionbean code is as follows:/** hellosessionbean. java ** created on April 16, 2007, ** to change this template, choose tools | template manager * and open the template in the editor. */package session. hello; import javax. EJB. stateless;/***** @ author administrator */@ statelesspublic class hellosessionbean implements session. hello. hellosessionremote, session. hello. hellosessionlocal {/** Creates a new instance of hellosessionbean */Public hellosessionbean () {} Public String Hello () {return new string ("Hello! ") ;}} The red part is newly added.
Modify InterfaceModify the interface file in the same way. The modified interface file is as follows: remote interface hellosessionremotepackage session. hello; import javax. EJB. remote;/*** this is the business interface for hellosession enterprise Bean. * // @ remotepublic interface hellosessionremote {Public String Hello ();} The Local interface hellosessionlocal package session. hello; import javax. EJB. local;/*** this is the business interface for hellosession enterprise Bean. * // @ localpublic Interface Hellosessionlocal {Public String Hello ();} the red part is the added code.
Deploy Session BeanRight-click the hellobean project and choose deploy project to complete the deployment. If the server is not started, the server is started and then deployed. You can view the deployed applications on the server in two ways: by integrating the development environment and by using the server console.
View through integrated development environmentSelect the runtime view to view the deployed EJB.
View on the consoleGo to the console through a browser. Address: http: // localhost: 4848/default username: Admin default password: adminadmin. The following interface is displayed:
Reference Book: Java ee 5 practical tutorial-EJB Based on WebLogic and eclipse