Ejb Webservice

In this tutorial I will explain you how you can develop an EJB and then expose the EJB as webservice.

Ejb Webservice

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
Select File--> New Project and then select JavaEE project

EjbWebservice

Give the name of the project as EjbWebService
EjbWebservice

Select the server as Glassfish
In Ejb web Service Only Stateless Bean can be used .So take a Session Bean

EjbWebservice
Now take a Session Bean

EjbWebservice

Here select Stateless Bean and Remote interface

EjbWebservice
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

EjbWebservice
Now deploy the project
After deploying  now test the Web Service
Right Click on the web service Session1Bean .
Select the Test Web Service

EjbWebservice

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

EjbWebservice
It gives the sum with the SOAP Request and SOAP response

EjbWebservice

Creating the client for the EJB Web Service

Client can be created as the the servlet or jsp or simple java file
For this take a web project

EjbWebservice
After making the project Right click on it
Select New Web Service Client
It creates the client environment in this project

EjbWebservice

In the dialog box select the WSDL file for the web service by clicking on the browser button

EjbWebservice
EjbWebservice

Now make a servlet program

EjbWebservice
Give name as Client1

EjbWebservice

Rt. Click  on the servlet code and select Web Service Client operation

EjbWebservice

Here Select the operation name  as in fig

EjbWebservice

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
EjbWebservice

  1 Download Code