...
- From Eclipse main menu, select File->New->Other
- In the New dialog, select Web->Dynamic Web Project and click Next
- Type jaxws-converterclient as the Project Name and click Next
- On the Project Facets page, the default selections are enough.
- Make sure that the check box Generate Deployment Descriptor is selected and click Next
- On the Geronimo Deployment Page modify the Group Id to org.apache.geronimo.samples.jaxws and the Artifact Id to jaxws-converterclient.
- Click Finish
...
- Now all the stubs should have been created and placed in C:\WSDL directory. We will be needing only the class ConverterPortType for accessing Web Service.
- Copy the files Converter.java and ConverterPortType.java and place them in the appropriate folder according to their package declaration
...
Code Block | ||||
---|---|---|---|---|
| ||||
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <html> <head> <title>Converter</title> <meta content="text/html; CHARSET=iso-8859-1" http-equiv="Content-Type"> </head> <body> <center> <h3>This from invokes a Web Service.</h3> <br> Please type an amount and click submit to see the result. <form action="resultindex.jsp">Amount(in Dollars): <input type="text" name="amount"> <input type="submit" value="Submit"></form> <br> <br><br> <jsp:include page="result.jsp"></jsp:include> </center> </body> </html> |
...
Code Block | ||||
---|---|---|---|---|
| ||||
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@page import="java.math.BigDecimal"%> <%@ page import="org.apache.geronimo.samples.jaxws.Converter"%> <%@ page import="org.apache.geronimo.samples.jaxws.ConverterPortType"%> <html> <head> <title>Converter</title> <meta content="text/html; CHARSET=iso-8859-1" http-equiv="Content-Type"> </head> <body> <% String amount = request.getParameter("amount"); if (amount != null && amount.trim().length() > 0) { out.println("<center>"); try { BigDecimal dollars = new BigDecimal(amount); Converter conv = new Converter(); ConverterPortType port = conv.getConverterPort(); BigDecimal rupees = conv.dollarToRupees(dollars); BigDecimal euros = conv.rupeesToEuro(rupees); out.println(dollars + " Dollars equals to " + rupees + " Rupees"); out.println("<br>"); out.println(rupees + " Rupees equals to " + euros + " Euros"); } catch (Exception e) { out.println("Error: " + e.getMessage()); } out.println("<center>"); } %> </body> </html> |
This concludes the development section of our web based client.concludes the development section of our web based client.
Using <service-ref> element to consume Web Service:
- In our previous result.jsp we accessed the Web Service by instantiating the Service class. This can slightly overhead the client application.
- We can also use JNDI Naming service to directly access our web service without instantiating any Service class.
- We need to add a service-ref element to web.xml to give necessary information about WSDL file location, Service name etc .,
Code Block | ||||
---|---|---|---|---|
| ||||
<service-ref>
<service-ref-name>services/Converter</service-ref-name>
<service-interface>
org.apache.geronimo.samples.jaxws.Converter
</service-interface>
<wsdl-file>
http://localhost:8080/jaxws-converter/converter?wsdl
</wsdl-file>
</service-ref>
|
- Let us walkthrough the elements that we added to web.xml
- service-ref-name - This is the name by which JNDI identifies our deployed service. The location that these services are located are java:comp/env/<service-ref-name>
- service-interface - This is the interface which client uses to access the web service and create necessary stubs under covers. This interface should compulsorily extend either javax.xml.ws.Service or javax.xml.rpc.Service. Observer that this is not the Service-Endpoint Interface that we used to deploy the service.
- wsdl-file - The location of WSDL file specified the location of deployed web service. Suppose the web service is deployed at *http://localhost:8080/jaxws-converter/converter*, then the WSDL file is located at *http://localhost:8080/jaxws-converter/converter?wsdl*
- The changes required in accessing the service are as follows:
Code Block | ||||
---|---|---|---|---|
| ||||
........
Context ctx= new InitialContext();
Object obj = ctx.lookup("java:comp/env/services/Converter");
Converter service= (Converter) obj;
ConverterPortType port = service.getConverterPort();
BigDecimal rupees =port.dollarToRupees(dollars);
........
|
Deploying and Testing the Web Client
...
- Right click the index.jsp present under WebContent directory of our project and select Run As->Run On Server
- In the popup, check the check box Always use this server when running the project and then click Finish
- Now Eclipse will try to open the jsp in a web browser which shows you a form to enter amount in Dollars.
- Enter any amount and press submit, the jsp should display the result that is returned by the web service.
Info | ||
---|---|---|
| ||
Note that we accessed the webservice in the jsp by creating a service, and then retrieving the Converter port from the WSDL document. |
...
- Right click the jaxws-converterpojoclient project and then select Properties
- Select Java Build Path and then go to Libraries tab
- Click on Add External JARS and then add the axis2-adb-1.3.jar from the location <GERONIMO_INSTALL_DIR>/repository/org/apache/axis2/axis2-adb/1.3/ and then click Open
Info title Version Numbers The version numbers may differ slightly. It wont matter much.
- Repeat the above step for the following external JARS
Panel | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
| ||||||||||
axis2-adb-1.3.jar - <GERONIMO_INSTALL_DIR>/repository/org/apache/axis2/axis2-adb/1.3/axis2-adb-1.3.jar Also add Server Runtime Library to class path which reduces the effort for adding some more jars. Note that these jars are used when JAX-WS Engine is configured as Axis2. If JAX-WS engine is configured as CXF the jars may differ slightly. |
This completes the adding external JARs to the project
...
- Now Right click the ConverterClient.class and select Run As->Run as Java Application
- Now enter the amount in the console window of Eclipse
- The output will be shown which is retrieved by accessing the methods of Web service.
This completes the development and deployment of clients for consuming a Web Service. Even though this tutorial demonstrated for one particular Web Service, the method can be extended for any deployed web service.