Creating an EJB 3.0 project and calling it from servlet.
WebSphere Integration Developer is the development tool used.
Go to File, New, EJB project. Give the project Name and EAR
project name. Click on next and finish.
EAR Project name is the one which will contain your EJB project,
or any other project like web project you create later. This EAR project name
will contain all the modules and basically required for generating your EAR file
containing all the modules.
Now, we will create the EJB 3.0 stateless session bean
Select both the interfaces(Remote and Local) in the above screen .
Click Next and Finish.
Now, we will create a print method in the EJB class. Rest of the
template is created by the development tool.
We add the same method definition to both the interfaces –
Local and Remote
Now , we create the servlet
to call the stateless session EJB created above.
Go to File, New, Dynamic Web Project
Add the dynamic web project it to the Same EAR. So the modules EJB client jar, EJB jar and
web module is packaged in one ear file.
Click Finish
Right Click the Dynamic Web Project you just created and go
to properties, Java EE Module Dependencies and select the JAR module
As we will be calling the EJB remote interface from the servlet,
we need to do the above otherwise we will get Class not found exception.
Go to Dynamic Web Project, Servlets. Right click the Servlet
and create new servlet
And click Finish
In the Servlet class, add a EJB reference using the @EJB
annotation as shown below.
Press Control+Shift+O to organize imports.
Note a few items:
- The @EJB annotation is applied to a member variable that is either a local or remote interface of an EJB.
- In WebSphere, the default JNDI name of an EJB is set based on the remote or local interface name. As a result, here the @EJB annotation has enough information to deduce the JNDI name.
- The @EJB annotation works only in a Servlet or EJB bean class. You can't use it from a regular Java class, such as a JSF managed bean or Struts action. These classes must explictly do a JNDI lookup. In WebSphere, the JNDI name of an EJB is same as its remote interface name or ejblocal:LOCAL_INTERFACE_NAME.
Now , we need to deploy the ear
Add the project.
Publish the EAR to server.
In case publish fails. Do a clean, build
Access the URL
http:// hostname: http port/web
context root/servlet mapping
http://localhost:9088/asasqsWEB/Sdaas
We have not called the print method
of EJB anywhere in the servlet class , we modify the servlet code as below .
No comments:
Post a Comment