Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migration of unmigrated content due to installation of a new plugin

...

  1. Right click on Java Resources: src and select New->Package.





  2. Name the package as org.apache.geronimo.samples.jws. Select Finish.





  3. Right click on the new package and Select New->Interface.





  4. Name the interface as Calculator. Select Finish.





  5. Add the following code to the Calculator interface class
    Code Block
    borderStylesolid
    titleCalculator.classborderStylesolid
    package org.apache.geronimo.samples.jws;
    
    import javax.jws.WebService;
    import javax.jws.WebMethod;
    import javax.jws.WebParam;
    
    @WebService(name="CalculatorPortType",
                targetNamespace = "http://jws.samples.geronimo.apache.org")
    public interface Calculator {
    
        @WebMethod
        public int add(@WebParam(name = "value1") int value1,
                       @WebParam(name = "value2") int value2);
    
    }
    
  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
    borderStylesolid
    titleCalculatorService.classborderStylesolid
    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;
    
        public int add(int value1, int value2) {
            System.out.println("User Principal: " + context.getUserPrincipal());
            return value1 + value2;
        }
    }
    

...

  • Export the source files to a jar as shown in the figure.









  • Open a command prompt and point it to the bin directory of server installation.
    1. Run gsh. This will start the Gshell.
    2. Run the command
      Code Block
      borderStylesolid
      titleWSDL Generation commandborderStylesolid
      Administrator@T60J9:/> jaxws/wsgen -classpath C:/WSDL/source.jar -d C:/WSDL/ -wsdl:soap1.1 org.apache.geronimo.samples.jws.CalculatorService
      
      In this command -classpath is used to set source.jar(exported from eclipse) in the classpath, -d defines the location where all the generated artifacts will be placed, -wsdl:soap1.1 suggests a wsdl generation following soap1.1 protocol, org.apache.geronimo.samples.jws.CalculatorService is the SEI used to generate the WSDL.
    3. Once the above command is run Calculator_schema1.xsd and Calculator.wsdl will be generated at C:/wsdl.
    4. Rename Calculator.wsdl as CalculatorService.wsdl and add both the generated files above to WEB-INF directory of the project.
      Info
      titleWhy are we using gsh if we already have a jaxws-tools.bat

      This is because there are some issues involved with jaxws-tools.bat in AG 2.1 so we are using a gshell to call the web services generator.

      To learn more about GShell refer Gshell section in user guide. To know about more options associated with jaxws. Run jaxws/wsgen -help or jaxws/wsimport -help from gshell.

...

  1. Right Click on the Web Content folder and Select New->JSP.
  2. Name the jsp as index.jsp and Select Finish.





  3. Add the following code to index.jsp
    Code Block
    borderStylesolid
    titleindex.jspborderStylesolid
    <%@ 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
    borderStyle
    borderStylesolid
    titleresult.jspsolid
    <%@ 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.

...

  1. Expand WEB-INF/web.xml and add the following code
    Code Block
    borderStyle
    borderStylesolid
    titleweb.xmlsolid
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:calc="urn:geronimo-samples-jws"
    xmlns="http://java.sun.com/xml/ns/javaee"
    version="2.5">
    <servlet>
    <display-name>CalculatorService</display-name>
    <servlet-name>CalculatorService</servlet-name>
    <servlet-class>
    org.apache.geronimo.samples.jws.CalculatorService
    </servlet-class>
    </servlet>
    <servlet-mapping>
    <servlet-name>CalculatorService</servlet-name>
    <url-pattern>/calculator</url-pattern>
    </servlet-mapping>
    <service-ref>
                <service-ref-name>services/Calculator</service-ref-name>
                <service-interface>javax.xml.ws.Service</service-interface>
                <wsdl-file>WEB-INF/CalculatorService.wsdl</wsdl-file>
            </service-ref>
    </web-app>
    
  2. Similarly double click geronimo-web.xml and add the following code.
    Code Block
    borderStylesolid
    titlegeronimo-web.xmlborderStylesolid
    <?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.1">
    <dep:moduleId>
    <dep:groupId>org.apache.geronimo.samples.jws</dep:groupId>
    <dep:artifactId>Calculator</dep:artifactId>
    <dep:version>1.0</dep:version>
    <dep:type>car</dep:type>
    </dep:moduleId>
    <dep:dependencies>
    </dep:dependencies>
    </dep:environment>
    <context-root>/jaxws-calculator-1.0</context-root>
    <service-ref>
       <service-ref-name>services/Calculator</service-ref-name>
       <port>
          <port-name>CalculatorPort</port-name>
          <protocol>http</protocol>
          <host>localhost</host>
          <port>8080</port>
          <uri>/jaxws-calculator/calculator</uri>
       </port>
     </service-ref>
    </web-app>
    

...