...
- Create a Dynamic Web Project
- Select File --> New, select Other...
- In the popup window select Web --> Dynamic Web Project category and click Next.
- Type in jaxws-calculator as the Project name: and select Next
- Make sure Generate Deployment Descriptor is selected and click Next.
- Modify the Group Id to: org.apache.geronimo.samples.jws and Artifact Id to: Calculator.
Info title Useful Information Group ID: A name that identifies the group of a module. The default value is default.
Artifact ID: A name of the module in a group. The default value is <blank>.
Version: The version of module. The default value is 1.0
Type: The type of module, such as system module (car), jar module (jar), web
archive (war), enterprise archive (ear), and so on. The default value is car
A module is stored inside a repository as repository/groupId/artifactId/version/artifactIdversion.
type. This repository structure gives a more standardized and easily maintainable
structure to Geronimo applications.
- Select File --> New, select Other...
- Axis2 Configuration:
- Select Window --> Preferences.
- Select Web Services --> Axis2 Preferences.
- Under Axis2 Runtime tab, browse to the installation root of Apache Geronimothe Axis2 runtime installation to be used in Eclipse. Select Ok.
Note Ignore any error messages
- Select Window --> Preferences.
- Right-click on the jaxws-calculator project and select Properties, then select Project Facets
- On the next screen check the box for Axis2 Web Services
- Select Finish.
Warning title Warning Depending upon your environment, you may see the error message "Failed while installing Axis2 Web Services Core 1.1". Do not worry. We will just have to manually point Axis2 to the installation directory of Apache Geronimo to fix this error.
This completes the configuration of Eclipse for application development.
- On the next screen check the box for Axis2 Web Services
Creating the Web Services Implementation code
This completes the configuration of Eclipse for application development.
Creating the Web Services Implementation code
To implement the Calculator web service To implement the Calculator we are going to create a package called: org.apache.geronimo.samples.jws. This package will contain 2 two classes. A : a Calculator Interfaceinterface, and a CalculatorService which implements the Calculator interface. Lets Let's go step by step with creating the package, interface class and implementation class.
- Right-click on Java Resources: src and select New --> Package.
- Name the package as org.apache.geronimo.samples.jws. Select Finish.
- Right-click on the new package and Select New --> Interface.
- Name the interface as Calculator. Select Finish.
- Add the following code to the Calculator interface class:
Code Block title Calculator.class borderStyle solid 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); }
- Right-click on the package org.apache.geronimo.samples.jws and select
New --> Class. - Name the class CalculatorService
- Accept all the defaults and Select Finish.
- Add the following code to CalculatorService. class:
Code Block title CalculatorService.class borderStyle solid 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.following three figures:
- Open a command prompt and point it to the bin directory of server installation. :
- *#
- Run gsh. This will start the Gshell.
- Run the command
In this command -classpath is used to set the source.jar( exported from eclipse) Eclipse in the classpath, -d defines the location where all the generated artifacts will be placed, -wsdl:soap1.1 suggests a wsdl WSDL generation following soap1.1 protocol, and org.apache.geronimo.samples.jws.CalculatorService is the SEI used to generate the WSDL.Code Block title WSDL Generation command borderStyle solid Administrator@T60J9:/> jaxws/wsgen-tools.bat -classpath C:/WSDL/source.jar -d C:/WSDL/ -wsdl:soap1.1 org.apache.geronimo.samples.jws.CalculatorService
- Once the above command is run Calculator_schema1.xsd and Calculator.wsdl will be generated at C:/wsdl.
- Rename Calculator.wsdl as CalculatorService.wsdl and add both the generated files above to WEB-INF directory of the project.
Info title Why are we using gsh if we already have a jaxws-tools.bat 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.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.
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.
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 the result.jsp, which will call the Calculator Web Service.
- Right-click on the Web Content folder and Select New --> JSP.
- Name the JSP as index.jsp and select Finish.
- Add the following code to index.jsp:
Code Block title index.jsp borderStyle solid <%@ page
- Right-click on the Web Content folder and Select New --> JSP.
- Name the jsp as index.jsp and Select Finish.
Add the following code to index.jspCode Block title index.jsp borderStyle solid <%@ 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>
- Right-click on the Web Content folder and Select New --> JSP.
- Name the jsp JSP as result.jsp and Select select Finish.
- Add the following code to result.jsp. :
This finishes the development of Web client.Code Block title result.jsp borderStyle solid <%@ 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>
Setting up the Deployment Descriptor and Deployment Plan
This finishes the development of Web client.); 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>
Setting up the Deployment Descriptor and Deployment Plan
- Double-click WEB-INF/web.xml and add the following code:
Code Block title web.xml borderStyle solid <?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>
- Similarly double-click geronimo-Expand WEB-INF/web.xml and add the following code:
Similarly double-click geronimo-web.xml and add the following code.Code Block title geronimo-web.xml borderStyle solid <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:calc="urn:geronimo-samples-jws" xmlns-8"?> <web:web-app xmlns:dep="http://javageronimo.sunapache.comorg/xml/ns/javaeedeployment-1.2" 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> xmlns:conn="http://geronimo.apache.org/xml/ns/j2ee/connector-1.2" xmlns:name="http://geronimo.apache.org/xml/ns/naming-1.2" <service-ref-name>services/Calculator</service-ref-name> xmlns:ejb="http://openejb.apache.org/xml/ns/openejb-jar-2.2" <service-interface>javax.xml.ws.Service</service-interface> xmlns:pkgen="http://openejb.apache.org/xml/ns/pkgen-2.1" <wsdl-file>WEB-INF/CalculatorService.wsdl</wsdl-file> </service-ref> </web-app>
Code Block title geronimo-web.xml borderStyle solid <?xml version="1.0" encoding="UTF-8"?> <web-app xmlnsxmlns:app="http://geronimo.apache.org/xml/ns/j2ee/application-2.0" xmlns:sec="http://geronimo.apache.org/xml/ns/security-2.0" xmlns:web="http://geronimo.apache.org/xml/ns/j2ee/web-2.0.1" xmlns:pers="http://geronimojava.apachesun.orgcom/xml/ns/j2ee/web-1.1"> <dep:environmentpersistence" xmlns:depclient="http://geronimo.apache.org/xml/ns/j2ee/deploymentapplication-client-12.1"> 0"> <dep:environment> <dep:moduleId> <dep:groupId>org.apache.geronimo.samples.jws</dep:groupId> <dep:artifactId>Calculator</dep:artifactId> <dep:version>1.0</dep:version> /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> 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:web-app>
Deploy and Run
- Right-click on jaxws-calculator project. Select Run As --> Run On Server.
- On the next window select Apache Geronimo V2.1 and Select Finish.
- Once the application is deployed on the server . Launch launch a browser and run the following URL http://localhost:8080/jaxws-calculator-1.0/index.jsp.
- Give the values as 5,7. Select =. and 7 and then select the = button:
- The result is 12 as shown in the figure: