blogger templates blogger widgets
This is part of a list of blog posts.
To browse the contents go to


Create a Dynamic Web application which will act as your WS server.

Create a wsdl document within this project (could be anywhere).

Change the default namespace (just for fun) to

Note that the endpoint address is set to the default namespace. You could change this now or could be changed automatically when we generate Server skeleton code.

I changed it. And also the operation name.

Now create the request and response types.

Generate the Java Bean Skeleton. (WS Server code)

Right click the wsdl -> Web Services -> Generate Java Bean Skeleton.

Select the appropriate server, projects and also notice the slider. I planned to test it later than including it soon after this installation. So the slider was kept at "start service".

In the next screen select "Enable wrapper style" and "Copy WSDL to project". This will regenerate the wsdl into WEB-INF/wsdl folder by default.

If not target package is selected, it will generate the code into a newly created package based on your namespace.
In our case com.server.calculator. I specified a package (which was created previously).

<wsdl:definitions name="Calculator" targetNamespace="" xmlns:soap="" xmlns:tns="" xmlns:wsdl="" xmlns:xsd="">

  <xsd:schema targetNamespace="">
   <xsd:element name="calculate">
      <xsd:element name="in" type="tns:CalcRequestType"/>
   <xsd:element name="calculateResponse">
      <xsd:element name="out" type="xsd:string"/>

   <xsd:complexType name="CalcRequestType">
     <xsd:element name="operand1" type="xsd:int"/>
     <xsd:element name="operand2" type="xsd:int"/>
     <xsd:element name="operation" type="tns:operationType"/>

   <xsd:simpleType name="operationType">
    <xsd:restriction base="xsd:string">
     <xsd:enumeration value="ADD"/>
     <xsd:enumeration value="SUB"/>
 <wsdl:message name="calculateRequest">
  <wsdl:part element="tns:calculate" name="CalcRequest"/>
 <wsdl:message name="calculateResponse">
  <wsdl:part element="tns:calculateResponse" name="CalcResponse"/>
 <wsdl:portType name="Calculator">
  <wsdl:operation name="calculate">
   <wsdl:input message="tns:calculateRequest"/>
   <wsdl:output message="tns:calculateResponse"/>
 <wsdl:binding name="CalculatorSOAP" type="tns:Calculator">

  <soap:binding style="document" transport=""/>
  <wsdl:operation name="calculate">
   <soap:operation soapAction=""/>
    <soap:body use="literal"/>
    <soap:body use="literal"/>
 <wsdl:service name="Calculator">
  <wsdl:port binding="tns:CalculatorSOAP" name="CalculatorSOAP">
   <soap:address location="http://localhost:9089/WSServer/Calculator"/>

Write the implementation code.

public class CalculatorSOAPImpl implements com.server.Calculator_PortType{
    public java.lang.String calculate(com.server.CalcRequestType in) throws java.rmi.RemoteException {
        //do business operation here or delegate it to a business component
      return Integer.toString(in.getOperand1()+in.getOperand2());
     }else {
      return Integer.toString(in.getOperand1()-in.getOperand2());


Create another dynamic web application project that will act as your client.

Generating client code

To generate client code, Rightclick wsdl -> Webservices -> Generate client.

Notice that I selected the slider to deploy client and no further because installing and running the client I wanted to do later at my convenience.

Write a servlet to invoke the webservice.

public class TestServlet extends HttpServlet {
 private static final long serialVersionUID = 1L;

    public TestServlet() {
        // TODO Auto-generated constructor stub

 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  System.out.println("Called WS");
  Calculator_Service service = new Calculator_ServiceLocator();
  try {
   Calculator_PortType proxy = service.getCalculatorSOAP(new URL("http://localhost:9089/WSServer/services/CalculatorSOAP"));
   CalcRequestType in = new CalcRequestType();
   String result = proxy.calculate(in);
  } catch (ServiceException e) {
   // TODO Auto-generated catch block


Notice that the service gets installed on the client server. And a JNDI lookup name is created. Check out the web.xml and code.

  WSDL Service Calculator</description>
  <service-qname xmlns:pfx="">pfx:Calculator</service-qname>

public class Calculator_PortTypeProxy implements com.server.Calculator_PortType {
  private boolean _useJNDI = true;
  private boolean _useJNDIOnly = false;
  private String _endpoint = null;
  private com.server.Calculator_PortType __calculator_PortType = null;
  private void _initCalculator_PortTypeProxy() {
    if (_useJNDI || _useJNDIOnly) {
      try {
        javax.naming.InitialContext ctx = new javax.naming.InitialContext();
        __calculator_PortType = ((com.server.Calculator_Service)ctx.lookup("java:comp/env/service/Calculator")).getCalculatorSOAP();

Now hitting this url,

Hi there, this is a Web service!

You could obtain the wsdl by querying for it to the webservice runtime

No comments:

Post a Comment