Calling A Session Bean Using A Web Module7 April 2007
It is assumed that the reader knows how to create an Enterprise Application and to add J2EE modules into it. To know more click here. So you created an Enterprise Application and added an EJB module and a Web module to it. Here we will discuss how to call a “Session Bean” from the web module.
- Select the web module and select “New” -> “Servlet”.
- Now fill servlet specific information in the next window.
- Click “Next” for adding other servlet related information like “Url pattern(s)”, “Servlet Name” these details will be stored in the web.xml file, configuration file of the web module.
- Servlet will be added to web module with some basic code.
- Open the Servlet file by clicking on it. Then right click “Select Enterprise Bean”
-> “ Call Enterprise Bean ”.
- A window will be displayed showing information of EJB’s in the project. Select one you want to call with referred interface, As EJB can be called remotely or locally.
- Click “OK”. A private method for remote lookup will be generated, so in “processRequest” method we can call that private method to return remote reference and on that EJB remote instance any of the EJB business methods can be invoked. We will invoke “sayWelcome” method that is assumed to be already added.
- Now right click “Enterprise Application” icon and click “Run project”. EJB and web module will be deployed automatically. If application server is not running it will also be started.
- In “Console” window you can see EJB has been deployed and NetBeans will automatically run the web module.
- Use new servlet URL to run your servlet that has code of EJB client we added. You can see the remote method results there.
It will look like this: Calling A Session Bean Using A Web Module