Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: slight corrections

...

The sample application referred in this article is a simple calculator which performs addition of two integers. The client application referred here is not a J2EE application. It is a regular Java client which will call a web service to carry out the application functionality. Web service is exposed as a Servlet in the Geronimo application server.
Two clients are provided: a jsp page and a non-javaee client.

Service implementation

The Calculator interface defines the Service Endpoint Interface (SEI) for the Web Service.

...

Info
titleweb.xml

The web.xml descriptor is not necessary for simple JAX-WS web service deployments. If the web.xml descriptor is not provided, it will be automatically generated during deployment.

...

In this sample, while not required to

...

implement the web service, it is required to provide the service-ref used by the jsp client.

The geronimo plan has no information for the web service but does specify more information for the service-ref for the jsp client. The plan can be located after building the project in jaxws-calculator/jaxws-calculator-jetty/target/resources/META-INF/plan.xml.

Code Block
xml
xml
borderStylesolid
titlegeronimo-
Code Block
xmlxml
borderStylesolid
titlegeronimo-web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://geronimo.apache.org/xml/ns/j2ee/web-1.1">
  <dep:environment xmlns:dep="http://geronimo.apache.org/xml/ns/deployment-1.12">
    <dep:moduleId>
      <dep:groupId>${pom.groupId}<groupId>org.apache.geronimo.samples</dep:groupId>
      <dep:artifactId>${pom.artifactId}<artifactId>jaxws-calculator-jetty</dep:artifactId>
      <dep:version>${version}<version>2.2-SNAPSHOT</dep:version>
      <dep:type>war<type>car</dep:type>
    </dep:moduleId>
  </dep:environment>

 <context-root>/jaxws-calculator</context-root>

 <service-ref> <dep:dependencies>
   <service-ref-name>services/Calculator</service-ref-name>
   <port><dep:dependency>
      <port-name>CalculatorPort</port-name>
    <dep:groupId>org.apache.geronimo.configs</dep:groupId>
  <protocol>http</protocol>
      <host>localhost</host>
<dep:artifactId>jasper</dep:artifactId>
        <port>8080</port>
<dep:version>2.2-SNAPSHOT</dep:version>
        <uri>/jaxws-calculator/calculator</uri>
<dep:type>car</dep:type>
      </port>dep:dependency>
 </service-ref>
</web-app>

The following WSDL file describes the Web Service:

Code Block
xmlxml
borderStylesolid
titleCalculatorService.wsdl

<?xml version="1.0" encoding="UTF-8"?>
<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>      <dep:dependency>
        <dep:groupId>org.apache.geronimo.configs</dep:groupId>
        <dep:artifactId>jetty6</dep:artifactId>
        <dep:version>2.2-SNAPSHOT</dep:version>
        <dep:type>car</dep:type>
      </dep:dependency>
      <dep:dependency>
        <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" <dep:groupId>org.apache.geronimo.configs</dep:groupId>
        <dep:artifactId>cxf</dep:artifactId>
            xmlns="http://jws.samples.geronimo.apache.org" <dep:version>2.2-SNAPSHOT</dep:version>
        <dep:type>car</dep:type>
      </dep:dependency>
      targetNamespace="http://jws.samples.geronimo.apache.org"</dep:dependencies>
    <dep:hidden-classes/>
    <dep:non-overridable-classes/>
  </dep:environment>
  <context-root>/jaxws-calculator</context-root>
  <service-ref>
    <service-ref-name>services/Calculator</service-ref-name>
   attributeFormDefault="unqualified" elementFormDefault="qualified">

 <port>
      <port-name>CalculatorPort</port-name>
       <xsd:element name="add"><protocol>http</protocol>
      <host>localhost</host>
          <xsd:complexType><port>8080</port>
      <uri>/jaxws-calculator/calculator</uri>
    </port>
  </service-ref>
</web-app>

The following WSDL file describes the Web Service:

Code Block
xml
xml
borderStylesolid
titleCalculatorService.wsdl

<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions name="Calculator"         <xsd:sequence>
    xmlns="http://schemas.xmlsoap.org/wsdl/"
                  <xsd:element name="value1" type="xsd:int"/>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"
        <xsd:element name="value2" type="xsd:int"/>xmlns:tns="http://jws.samples.geronimo.apache.org">

    <wsdl:types>
               </xsd:sequence>
  <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
              </xsd:complexType>
      xmlns="http://jws.samples.geronimo.apache.org" 
      </xsd:element>

             <xsd:element nametargetNamespace="addResponse">http://jws.samples.geronimo.apache.org"
                <xsd:complexType>
      attributeFormDefault="unqualified" elementFormDefault="qualified">

             <xsd:sequence>
 element name="add">
                <xsd:complexType>
      <xsd:element name="return" type="xsd:int"/>
           <xsd:sequence>
        </xsd:sequence>
              <xsd:element  </xsd:complexType>
name="value1" type="xsd:int"/>
              </xsd:element>
        </xsd:schema>
    </wsdl:types><xsd:element name="value2" type="xsd:int"/>
  
    <wsdl:message name="add">
       <wsdl:part name="add" element="tns:add"/>
    </wsdlxsd:message>sequence>

     <wsdl:message name="addResponse">
         <wsdl:part name="addResponse" element="tns:addResponse"/>
 </xsd:complexType>
             </wsdlxsd:message>element>

     <wsdl:portType name="CalculatorPortType">
       <wsdl<xsd:operationelement name="addaddResponse">
         <wsdl:input name="add" message="tns:add"/>
      <xsd:complexType>
          <wsdl:output name="addResponse" message="tns:addResponse"/>
       </wsdl<xsd:operation>
sequence>
          </wsdl:portType>
        
    <wsdl<xsd:bindingelement name="CalculatorSoapBindingreturn" type="tnsxsd:CalculatorPortTypeint"/>
        <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
           </xsd:sequence>
        <wsdl:operation name="add">
       </xsd:complexType>
     <soap:operation soapAction="add" style="document"/>
      </xsd:element>
        <wsdl:input name="add"></xsd:schema>
    </wsdl:types>
  
          <soap:body use="literal"/>
<wsdl:message name="add">
       <wsdl:part name="add" element="tns:add"/>
    </wsdl:input>message>

    <wsdl:message name="addResponse">
       <wsdl:outputpart name="addResponse" element="tns:addResponse"/>
    </wsdl:message>

    <wsdl:portType name="CalculatorPortType">
       <soap<wsdl:bodyoperation usename="literaladd"/>
         <wsdl:input   </wsdl:output>name="add" message="tns:add"/>
        </wsdl:operation>
   <wsdl:output name="addResponse" message="tns:addResponse"/>
              </wsdl:operation>
    </wsdl:binding>portType>
   
    <wsdl:service name="Calculator">
        <wsdl:portbinding name="CalculatorPortCalculatorSoapBinding" bindingtype="tns:CalculatorSoapBindingCalculatorPortType">
            <soap:address location<soap:binding style="document" transport="http://localhost:8080/jaxws-calculator-1.0/calculatorschemas.xmlsoap.org/soap/http"/>
        </wsdl:port>

        </wsdl:service>
<wsdl:operation name="add">
            
</wsdl:definitions>
Info
titlewebservices.xml

In J2EE version 1.4, the webservices.xml file was also necessary to describe the Web Service. In Java EE 5 that file is optional and is not required in this example.

JSP-based JAX-WS client

The add.jsp is a basic client for the CalculatorService Web Service.

<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/jaxws-calculator-1.0/calculator"/>
        </wsdl:port>
    </wsdl:service>
      
</wsdl:definitions>
Info
titlewebservices.xml

In J2EE version 1.4, the webservices.xml file was also necessary to describe the Web Service. In Java EE 5 that file is optional and is not required in this example.

JSP-based JAX-WS client

The add.jsp is a basic client for the CalculatorService Web Service.

Code Block
java
java
borderStylesolid
titleadd.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ page import="javax.naming.InitialContext" %>
<%@ page import="javax.xml.ws.Service" %>
<%@ page import="org.apache.geronimo.samples.jws.Calculator" %>
<html  xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
  <title>Apache Geronimo Sample Application - JAX-WS Calculator</title>
  <meta content="text/html; CHARSET=iso-8859-1" http-equiv="Content-Type">
</head>

<BODY>
  <font face="Verdana, Helvetica, Arial">

    <h3>This is a JAX-WS web service sample application.   Please type the value 1 and value 2 below to see the add result.</h3>
    <form action="add.jsp">
        Value 1: <input type="text" name="value1">   Value 2: <input type="text" name="value2"> <input type="submit" value="Add">
    </form>
    <br>
<%
    String value1 = request.getParameter( "value1" );
    String value2 = request.getParameter( "value2" );

    if (value1 != null && value1.trim().length() > 0 &&
        value2 != null && value2.trim().length() > 0) {

Code Block
javajava
borderStylesolid
titleadd.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ page import="javax.naming.InitialContext,javax.xml.ws.Service,org.apache.geronimo.samples.jws.Calculator"%>
<html  xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
  <title>Apache Geronimo Sample Application - JAX-WS Calculator</title>
  <meta content="text/html; CHARSET=iso-8859-1" http-equiv="Content-Type">
</head>

<BODY>
  <font face="Verdana, Helvetica, Arial">

    <h3>This is a JAX-WS web service sample application.   Please type the value 1 and value 2 below to see the add result.</h3>
    <form action="add.jsp">
        Value 1: <input type="text" name="value1">   Value 2: <input type="text" name="value2"> <input type="submit" value="Add">
    </form>
    <br>
<%
    String value1 = request.getParameter( "value1" );
    String value2 = request.getParameter( "value2" );

    if (value1 != null && value1.trim().length() > 0 &&
        value2 != null && value2.trim().length() > 0) {

        out.println("<h4>");

        try {
            int v1 = Integer.parseInt(value1);
            int v2 = Integer.parseInt(value2);
    
            InitialContext ctx = new InitialContext();
            Service service = (Service)ctx.lookup("java:comp/env/services/Calculator");
            Calculator calc = service.getPort(Calculator.class);         
            int sum = calc.add(v1, v2);
            out.println("Result: " + v1 + " + " + v2 + " = " + sum);
<h4>");

        try {
            int }v1 catch= ( Exception e ) {Integer.parseInt(value1);
            out.println("Error: " + e.getMessage()int v2 = Integer.parseInt(value2);
    
   }

         InitialContext ctx = new out.println("</h4>"InitialContext();
          }
%>

  </FONT>
</body>
</html>

The add.jsp looks up a Web Service reference in the JNDI tree. The Web Service reference must first be added the web.xml file.

Since the resource injection is not supported in JSPs the service-ref must be added explicitly to
Code Block
xmlxml
borderStylesolid
titleweb.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.5">
Service service = (Service)ctx.lookup("java:comp/env/services/Calculator");
            Calculator calc = service.getPort(Calculator.class);         
            int sum = calc.add(v1, v2);
            .....

        <service-ref>
out.println("Result: " + v1 + " + " + v2 + " = " + sum);
        } catch ( Exception e  <service-ref-name>services/Calculator</service-ref-name>) {
            <service-interface>javax.xml.ws.Service</service-interface>
      out.println("Error: " + e.getMessage());
      <wsdl-file>WEB-INF/wsdl/CalculatorService.wsdl</wsdl-file> }

        out.println("</service-ref>
h4>");
        .....}
%>

  </FONT>
</body>
</web-app>
Note
titleResource injection in JSP
html>

The add.jsp looks up a Web Service reference in the JNDI tree. The Web Service reference must first be added the web.xml file.

...

Checkout the sample source code from SVN:

...

Code Block
xml
xml
borderStylesolid
titleweb.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://

...

java.

...

sun.

...

com/

...

xml/

...

The source code will be checked out into the calculator/ directory. This sample code code lives in calculator/jaxws-calculator-war/ directory.

Required Tools

The tools used for developing and building the Calculator sample application are:

Apache Maven 2.0.x

Apache Maven is used for building the Calculator application.

Building

Compile Source Code

From command prompt execute the following command in the calculator/jaxws-calculator-war/ folder:

mvn install

After the code is successfully compiled a jaxws-calculator-war-2.0-SNAPSHOT.war file will be created in the target/ subfolder.

Deploying

Deploy the jaxws-calculator-war-2.0-SNAPSHOT.war using the Geronimo Console (http://localhost:8080/console):

...

ns/javaee" version="2.5">
        .....

        <service-ref>
            <service-ref-name>services/Calculator</service-ref-name>
            <service-interface>javax.xml.ws.Service</service-interface>
            <wsdl-file>WEB-INF/wsdl/CalculatorService.wsdl</wsdl-file>
        </service-ref>

        .....
</web-app>
Note
titleResource injection in JSP

Since the resource injection is not supported in JSPs the service-ref must be added explicitly to the web.xml file

...

.

Testing of the Sample
Anchor
testing
testing

To test this sample service use the add.jsp (http://localhost:8080/jaxws-calculator/add.jsp) to invoke the Web Service. Once the JSP page loads type in two values to add and press the Add button. The result of the addition should show up below. For example:

Note

TODO document command line client