Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  1. Right Click on the WebContent subfolder and Select New->Other.





  2. Select Web Services->wsdl and click Next.





  3. Give the File name as CalculatorService.wsdl and click Next.





  4. On the next window give the Target namespace as "http://jws.samples.geronimo.apache.org". Select Finish.





  5. Modify the CalculatorService.wsdl as follows
    Code Block
    titleCalculatorService.wsdl
    borderStylesolid
    <wsdl:definitions name="Calculator"
    xmlns="http://schemas.xmlsoap.org/wsdl/"
    xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
    xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://jws.samples.geronimo.apache.org"
    xmlns:tns="http://jws.samples.geronimo.apache.org">
    <wsdl:types>
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns="http://jws.samples.geronimo.apache.org"
    targetNamespace="http://jws.samples.geronimo.apache.org"
    attributeFormDefault="unqualified"
    elementFormDefault="qualified">
    <xsd:element name="add">
    <xsd:complexType>
    <xsd:sequence>
    <xsd:element name="value1" type="xsd:int"/>
    <xsd:element name="value2" type="xsd:int"/>
    </xsd:sequence>
    </xsd:complexType>
    </xsd:element>
    <xsd:element name="addResponse">
    <xsd:complexType>
    <xsd:sequence>
    <xsd:element name="return" type="xsd:int"/>
    </xsd:sequence>
    </xsd:complexType>
    </xsd:element>
    </xsd:schema>
    </wsdl:types>
    <wsdl:message name="add">
    <wsdl:part name="add" element="tns:add"/>
    </wsdl:message>
    <wsdl:message name="addResponse">
    <wsdl:part name="addResponse" element="tns:addResponse"/>
    </wsdl:message>
    <wsdl:portType name="CalculatorPortType">
    <wsdl:operation name="add">
    <wsdl:input name="add" message="tns:add"/>
    <wsdl:output name="addResponse" message="tns:addResponse"/>
    </wsdl:operation>
    </wsdl:portType>
    <wsdl:binding name="CalculatorSoapBinding"
    type="tns:CalculatorPortType">
    <soap:binding style="document"
    transport="http://schemas.xmlsoap.org/soap/http"/>
    <wsdl:operation name="add">
    <soap:operation soapAction="add" style="document"/>
    <wsdl:input name="add">
    <soap:body use="literal"/>
    </wsdl:input>
    <wsdl:output name="addResponse">
    <soap:body use="literal"/>
    </wsdl:output>
    </wsdl:operation>
    </wsdl:binding>
    <wsdl:service name="Calculator">
    <wsdl:port name="CalculatorPort"
    binding="tns:CalculatorSoapBinding">
    <soap:address location="http://localhost:8080/jaxwscalculator-
    1.0/calculator"/>
    <wswa:UsingAddressing
    xmlns:wswa="http://www.w3.org/2005/08/addressing/wsdl"/>
    </wsdl:port>
    </wsdl:service>
    </wsdl:definitions>
    
    Warning
    titleWarning

    add explanation for the code

    This completes the creation of wsdl for web service.

Creating the Web Services Implementation code

To implement the Calculator we are going to create a package org.apache.geronimo.samples.jws. This package will contain 2 classes. A Calculator Interface, and CalculatorService which implements the Calculator interface. Lets go step by step with creating the package, interface class and implementation class.

...

  1. Right click on the package org.apache.geronimo.samples.jws and select
    New->Class.
  2. Name the class CalculatorService
  3. Accept all the defaults and Select Finish.





  4. Add the following code to CalculatorService.class
    Code Block
    titleCalculatorService.class
    borderStylesolid
    package org.apache.geronimo.samples.jws;
    import javax.annotation.Resource;
    import javax.jws.WebService;
    import javax.xml.ws.WebServiceContext;
    @WebService(serviceName = "Calculator",
    portName="CalculatorPort",
    endpointInterface = "org.apache.geronimo.samples.jws.Calculator",
    targetNamespace = "http://jws.samples.geronimo.apache.org")
    public class CalculatorService implements Calculator {
    @Resource
    private WebServiceContext context;
    /**
    * @return
    * returns javax.xml.ws.WebServiceContext
    */
    public WebServiceContext getContext() {
    return context;
    }
    /**
    * @param value1
    * @param value2
    * @return
    * returns int
    */
    public int add(int value1, int value2) {
    System.out.println("User Principal: " + context.getUserPrincipal());
    System.out.println("value1: " + value1 + " value2: " + value2);
    return value1 + value2;
    }
    }
    

This completes the development of Web Services implementation code.

Developing a web client for Calculator

This section will take you through the creation of two jsp's index.jsp and result.jsp.index.jsp will prompt the
user to enter two whole number values to add together. After submitting the form, the action will be forwarded to result.jsp. Result.jsp will call the Calculator add Web Service.

  1. Right Click on the Web Content folder and Select New->JSP.
  2. Name the jsp as index.jsp and Select Finish.
    Image Added
  3. Add the following code to index.jsp
    Code Block
    titleindex.jsp
    borderStylesolid
    
    <%@ page
    import="java.net.URL,javax.xml.namespace.QName,javax.xml.ws.Service,org
    .apache.geronimo.samples.jws.Calculator"%>
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
    <head>
    <title>Calculator</title>
    </head>
    <body>
    <form action="result.jsp">
    Please enter 2 whole numbers to add: <input type="text"
    name="value1"> + <input type="text" name="value2"> <input type="submit"
    value="=">
    </form>
    </body>
    </html>
    
  4. Right Click on the Web Content folder and Select New->JSP.
  5. Name the jsp as result.jsp and Select Finish.
  6. Add the following code to jsp.
    Code Block
    titleresult.jsp
    borderStylesolid
    
    <%@ page
    import="java.net.URL,javax.xml.namespace.QName,javax.xml.ws.Service,org
    .apache.geronimo.samples.jws.Calculator"%>
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
    <head>
    <title>Calculator Result</title>
    </head>
    <%
    int value1 = 0;
    int value2 = 0;
    int sum = 0;
    try {
    System.out.println( request.getParameter( "value1" ) + " " +
    request.getParameter( "value2" ) );
    value1 = Integer.parseInt( request.getParameter( "value1" ) );
    value2 = Integer.parseInt( request.getParameter( "value2" ) );
    URL url = new URL("http://localhost:8080/jaxws-calculator-
    1.0/calculator?wsdl");
    QName qname = new
    QName("http://jws.samples.geronimo.apache.org", "Calculator");
    Service service = Service.create(url, qname);
    Calculator calc =
    (Calculator)service.getPort(Calculator.class);
    sum = calc.add(value1, value2);
    } catch ( Exception e ) {
    e.printStackTrace();
    }
    %>
    <body>
    The result is: <%=value1%>+<%=value2%>=<%=sum%>
    <br>
    <a href="index.jsp">Back</a>
    </body>
    </html>
    

This finishes the development of Web client.