EJB Webservies
In this tutorial I will explain you how you can develop an EJB and then expose the EJB as webservice. We will also explain how the webservices can be called from a client.
Topics discussed are:
- Create EJB and deployin on the server
- Then expose EJB as webservice
- Develop the webservices client
- Deploy the client
- Test the application
Give the name of the project as EjbWebService
Select the server as Glassfish
In Ejb web Service Only Stateless Bean can be used .So take a Session Bean
Now take a Session Bean
Here select Stateless Bean and Remote interface
It creates session1Bean.java and session1Remote.java
In Remote Interface give a method named add() as follows
package pack1;
import javax.ejb.Remote;
@Remote
public interface session1Remote {
public int add(int x,int y);
}
Implement the method add in the bean class
Add @Webservice method before class
Add @Webmethod before the method as given in figure
Now deploy the project
After deploying now test the Web Service
Right Click on the web service Session1Bean .
Select the Test Web Service
It will open the file in the browser
There give the two no and click on add button
Add button is actually the operation present in the SessionBean file
It gives the sum with the SOAP Request and SOAP response
Client can be created as the the servlet or jsp or simple java file
For this take a web project
After making the project Right click on it
Select New Web Service Client
It creates the client environment in this project
In the dialog box select the WSDL file for the web service by clicking on the browser button
Now make a servlet program
Give name as Client1
Rt. Click on the servlet code and select Web Service Client operation
Here Select the operation name as in fig
This will generate the additional code in the servlet file
In arg0 and arg1 give the value
0
public class Client1 extends HttpServlet {
@WebServiceRef(wsdlLocation = "WEB-INF/wsdl/client/session1Bean/localhost_8080/session1BeanService/session1Bean.wsdl")
private Session1BeanService service;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
pack1.Session1Bean port = service.getSession1BeanPort();
int arg0 = 10;
int arg1 = 20;
int result = port.add(arg0, arg1);
out.println("Result = "+result);
}
catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}
Now deploy this project
Rt click and select run file