geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Geronimo v2.2 > Developing Clients for a JAX-WS Web Service
Date Thu, 13 May 2010 05:01:00 GMT
<html>
<head>
    <base href="http://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/1810/9/1/_/styles/combined.css?spaceKey=GMOxDOC22&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background: white;" bgcolor="white" class="email-body">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
    <h2><a href="http://cwiki.apache.org/confluence/display/GMOxDOC22/Developing+Clients+for+a+JAX-WS+Web+Service">Developing
Clients for a JAX-WS Web Service</a></h2>
    <h4>Page <b>edited</b> by             <a href="http://cwiki.apache.org/confluence/display/~chirunhua@gmail.com">Runhua
Chi</a>
    </h4>
        <br/>
                         <h4>Changes (6)</h4>
                                 
    
<div id="page-diffs">
            <table class="diff" cellpadding="0" cellspacing="0">
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" > <br>{code:title=Client Stubs
Generation|borderStyle=solid} <br></td></tr>
            <tr><td class="diff-changed-lines" >&lt;%SERVEER_INSTALLATION_BIN_DIR%&gt;:\
jaxws-tools.bat wsimport -s C:\WSDL <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">http://localhost:8080/jaxws-conveter/converter?wsdl</span>
<span class="diff-added-words"style="background-color: #dfd;">converter/converter?wsdl&quot;&gt;http://localhost:8080jaxws-converter/converter?wsdl</span>
<br></td></tr>
            <tr><td class="diff-unchanged" >{code} <br> <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >\\ \\ !console.gif! \\ \\ <br>
<br></td></tr>
            <tr><td class="diff-changed-lines" >* Copy the files *Converter.java*
and <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">*ConverterPortType.java*</span>
<span class="diff-added-words"style="background-color: #dfd;">*ObjectFactory.java*</span>
and place them in the appropriate folder according to their package declaration <br></td></tr>
            <tr><td class="diff-unchanged" > <br>Let us walk through the
steps that we just completed <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >                        Converter
conv = new Converter(); <br>			ConverterPortType port = conv.getConverterPort(); <br></td></tr>
            <tr><td class="diff-changed-lines" >BigDecimal rupees = <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">conv.dollarToRupees(dollars);</span>
<span class="diff-added-words"style="background-color: #dfd;">port.dollarToRupees(dollars);</span>
<br></td></tr>
            <tr><td class="diff-changed-lines" >BigDecimal euros = <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">conv.rupeesToEuro(rupees);</span>
<span class="diff-added-words"style="background-color: #dfd;">port.rupeesToEuro(rupees);</span>
<br></td></tr>
            <tr><td class="diff-unchanged" > <br>			out.println(dollars
+ &quot; Dollars equals to &quot; + rupees <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >........ <br>&lt;%@ page
import=&quot;javax.naming.InitialContext&quot; %&gt; <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">&lt;%@
page import=&quot;javax.naming.Context&quot; %&gt; <br></td></tr>
            <tr><td class="diff-unchanged" >........ <br>Context ctx= new
InitialContext(); <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >ConverterPortType port = service.getConverterPort();
<br>BigDecimal rupees =port.dollarToRupees(dollars); <br></td></tr>
            <tr><td class="diff-changed-lines" >BigDecimal euros = <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">conv.rupeesToEuro(rupees);</span>
<span class="diff-added-words"style="background-color: #dfd;">port.rupeesToEuro(rupees);</span>
<br></td></tr>
            <tr><td class="diff-unchanged" >........ <br>{code} <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
        </table>
</div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <style type='text/css'>/*<![CDATA[*/
table.ScrollbarTable  {border: none;padding: 3px;width: 100%;padding: 3px;margin: 0px;background-color:
#f0f0f0}
table.ScrollbarTable td.ScrollbarPrevIcon {text-align: center;width: 16px;border: none;}
table.ScrollbarTable td.ScrollbarPrevName {text-align: left;border: none;}
table.ScrollbarTable td.ScrollbarParent {text-align: center;border: none;}
table.ScrollbarTable td.ScrollbarNextName {text-align: right;border: none;}
table.ScrollbarTable td.ScrollbarNextIcon {text-align: center;width: 16px;border: none;}

/*]]>*/</style><div class="Scrollbar"><table class='ScrollbarTable'><tr><td
class='ScrollbarPrevIcon'><a href="/confluence/display/GMOxDOC22/Developing+a+simple+Calculator+Web+service"><img
border='0' align='middle' src='/confluence/images/icons/back_16.gif' width='16' height='16'></a></td><td
width='33%' class='ScrollbarPrevName'><a href="/confluence/display/GMOxDOC22/Developing+a+simple+Calculator+Web+service">Developing
a simple Calculator Web service</a>&nbsp;</td><td width='33%' class='ScrollbarParent'><sup><a
href="/confluence/display/GMOxDOC22/Developing+Web+services"><img border='0' align='middle'
src='/confluence/images/icons/up_16.gif' width='8' height='8'></a></sup><a
href="/confluence/display/GMOxDOC22/Developing+Web+services">Developing Web services</a></td><td
width='33%' class='ScrollbarNextName'>&nbsp;<a href="/confluence/display/GMOxDOC22/Developing+JAX-RPC+Web+services">Developing
JAX-RPC Web services</a></td><td class='ScrollbarNextIcon'><a href="/confluence/display/GMOxDOC22/Developing+JAX-RPC+Web+services"><img
border='0' align='middle' src='/confluence/images/icons/forwd_16.gif' width='16' height='16'></a></td></tr></table></div>

<p>This tutorial will take you through the steps required in developing, deploying and
testing a Web Service Client in Apache Geronimo for a web services which are already deployed
on the server</p>

<p>This tutorial will take you through the steps involved in creating a Web based Client
and a POJO Client for a JAX-WS web service. It is required that you complete the <a href="/confluence/display/GMOxDOC22/Developing+a+JAX-WS+POJO+Web+Service"
title="Developing a JAX-WS POJO Web Service">Developing a JAX&#45;WS POJO Web Service</a>
tutorial before completing this one. Since we will be using the WSDL file of the Web Service
deployed for creating the client, ensure that the Web Service from the <a href="/confluence/display/GMOxDOC22/Developing+a+JAX-WS+POJO+Web+Service"
title="Developing a JAX-WS POJO Web Service">Developing a JAX&#45;WS POJO Web Service</a>
tutorial is deployed and the Geronimo server is running before starting this tutorial.</p>

<div class='panelMacro'><table class='infoMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/information.gif" width="16"
height="16" align="absmiddle" alt="" border="0"></td><td><b>Types of
Web Services</b><br />For new users, Web Services can be created in two ways:
<ul>
	<li>Bottom Up Web Service - creating web services from Java classes.</li>
	<li>Top Down Web Service - creating web services from WSDL document.</li>
</ul>


<p>This tutorial will help you in creating a Top Down Web Service from WSDL document
which is already deployed on server.</p></td></tr></table></div>

<p>To run this tutorial, as a minimum you will be required to have installed the following
prerequisite software.</p>

<ol>
	<li>Sun JDK 6.0+ (J2SE 1.6)</li>
	<li>Eclipse IDE for Java EE Developers, which is platform specific</li>
	<li>Apache Geronimo Eclipse Plugin 2.1.x</li>
	<li>Apache Geronimo Server 2.1.x
<div class='panelMacro'><table class='noteMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/warning.gif" width="16" height="16"
align="absmiddle" alt="" border="0"></td><td>Geronimo version 2.1.x, Java 1.5
runtime, and Eclipse Ganymede are used is used in this tutorial but other versions can be
used instead (e.g., Geronimo version 2.2, Java 1.6, Eclipse Europa)</td></tr></table></div></li>
</ol>


<p>Details on installing eclipse are provided in the <a href="/confluence/display/GMOxDOC22/Development+environment"
title="Development environment">Development environment</a> section. </p>

<div class='panelMacro'><table class='infoMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/information.gif" width="16"
height="16" align="absmiddle" alt="" border="0"></td><td>This tutorial assumes
that you have completed the <a href="/confluence/display/GMOxDOC22/Developing+a+JAX-WS+POJO+Web+Service"
title="Developing a JAX-WS POJO Web Service">Developing a JAX&#45;WS POJO Web Service</a>
tutorial. Here we will try to develop the client for the web service deployed in the above
mentioned tutorial.</td></tr></table></div>

<p>This tutorial will take you through the following steps:</p>
<div>
<ul>
    <li><a href='#DevelopingClientsforaJAX-WSWebService-WebbasedClient'>Web based
Client</a></li>
<ul>
    <li><a href='#DevelopingClientsforaJAX-WSWebService-CreateaDynamicWebProjecttoconsumetheWebService'>Create
a Dynamic Web Project to consume the Web Service</a></li>
    <li><a href='#DevelopingClientsforaJAX-WSWebService-CreatingnecessarystubstoconsumeWebService'>Creating
necessary stubs to consume Web Service</a></li>
    <li><a href='#DevelopingClientsforaJAX-WSWebService-DevelopingtheWebbasedClient'>Developing
the Web based Client</a></li>
    <li><a href='#DevelopingClientsforaJAX-WSWebService-Using%3Cserviceref%3EelementtoconsumeWebService%3A'>Using
&lt;service-ref&gt; element to consume Web Service:</a></li>
    <li><a href='#DevelopingClientsforaJAX-WSWebService-DeployingandTestingtheWebClient'>Deploying
and Testing the Web Client</a></li>
</ul>
    <li><a href='#DevelopingClientsforaJAX-WSWebService-POJOClient'>POJO Client</a></li>
<ul>
    <li><a href='#DevelopingClientsforaJAX-WSWebService-DevelopingtheClient'>Developing
the Client</a></li>
    <li><a href='#DevelopingClientsforaJAX-WSWebService-Addingnecessaryjarfilestotheclasspath'>Adding
necessary jar files to the class path</a></li>
    <li><a href='#DevelopingClientsforaJAX-WSWebService-Testing'>Testing</a></li>
</ul>
</ul></div>

<h1><a name="DevelopingClientsforaJAX-WSWebService-WebbasedClient"></a>Web
based Client</h1>
<p>The following steps will help you in creating a Web based Client for a JAX-WS Web
Service which is already deployed on the server.</p>

<h2><a name="DevelopingClientsforaJAX-WSWebService-CreateaDynamicWebProjecttoconsumetheWebService"></a>Create
a Dynamic Web Project to consume the Web Service</h2>
<ul>
	<li>From Eclipse main menu, select <b>File --&gt; New --&gt; Other</b>
<br class="atl-forced-newline" /> <br class="atl-forced-newline" /></li>
	<li>In the <b>New</b> dialog, select <b>Web --&gt; Dynamic Web
Project</b> and click <b>Next</b>
<br class="atl-forced-newline" /> <br class="atl-forced-newline" /></li>
	<li>Type <b>jaxws-converterclient</b> as the <b>Project Name</b>
and click <b>Next</b>
<br class="atl-forced-newline" /> <br class="atl-forced-newline" /></li>
	<li>On the <b>Project Facets</b> page, the default selections are enough.
<br class="atl-forced-newline" /> <br class="atl-forced-newline" /> <span class="image-wrap"
style=""><img src="/confluence/download/attachments/93413/projectfacets.gif?version=1&amp;modificationDate=1215502897000"
style="border: 0px solid black" /></span> <br class="atl-forced-newline" />
<br class="atl-forced-newline" /></li>
	<li>Make sure that the check box <b>Generate Deployment Descriptor</b>
is selected and click <b>Next</b>
<br class="atl-forced-newline" /> <br class="atl-forced-newline" /></li>
	<li>On the <b>Geronimo Deployment Page</b> modify the <b>Group Id</b>
to <b>org.apache.geronimo.samples.jaxws</b> and the <b>Artifact Id</b>
to <b>jaxws-converterclient</b>.
<br class="atl-forced-newline" /> <br class="atl-forced-newline" /></li>
	<li>Click <b>Finish</b></li>
</ul>


<h2><a name="DevelopingClientsforaJAX-WSWebService-CreatingnecessarystubstoconsumeWebService"></a>Creating
necessary stubs to consume Web Service</h2>
<p>We need to create Service Endpoint Interface based on the WSDL document to use it
in out JSP's. Luckily in Geronimo this can be automated using the jaxws-tools utility provided
by Geronimo.</p>

<div class='panelMacro'><table class='infoMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/information.gif" width="16"
height="16" align="absmiddle" alt="" border="0"></td><td>Please take care that
jaxws-tools.bat utility in server's bin directory may not work in old versions of Apache Geronimo.
<br/>
Try using the latest version of Geronimo i.e <b>v2.1.1</b> .</td></tr></table></div>

<ul>
	<li>Open a command prompt and point it to the <b>bin</b> directory of server
installation.
<br class="atl-forced-newline" /> <br class="atl-forced-newline" /></li>
	<li>We will use the <b>jaxws-tools</b> utility provided in the server's
bin directory to generate client stubs.
<br class="atl-forced-newline" /> <br class="atl-forced-newline" /></li>
	<li>Use the following command to generate the necessary stubs that are needed to consume
the web service.</li>
</ul>


<div class="code panel" style="border-style: solid;border-width: 1px;"><div class="codeHeader
panelHeader" style="border-bottom-width: 1px;border-bottom-style: solid;"><b>Client
Stubs Generation</b></div><div class="codeContent panelContent">
<pre class="code-java">
&lt;%SERVEER_INSTALLATION_BIN_DIR%&gt;:\ jaxws-tools.bat wsimport -s C:\WSDL converter/converter?wsdl"&gt;http:<span
class="code-comment">//localhost:8080jaxws-converter/converter?wsdl</span>
</pre>
</div></div>

<ul>
	<li>Now all the stubs should have been created and placed in <b>C:\WSDL</b>
directory. We will be needing only the class <b>ConverterPortType</b> for accessing
Web Service.
<br class="atl-forced-newline" /> <br class="atl-forced-newline" /> <span class="image-wrap"
style=""><img src="/confluence/download/attachments/93413/console.gif?version=1&amp;modificationDate=1215502897000"
style="border: 0px solid black" /></span> <br class="atl-forced-newline" />
<br class="atl-forced-newline" /></li>
</ul>


<ul>
	<li>Copy the files <b>Converter.java</b> and <b>ObjectFactory.java</b>
and place them in the appropriate folder according to their package declaration</li>
</ul>


<p>Let us walk through the steps that we just completed</p>


<ul>
	<li><b>Converter.java</b> - This file extends the <b>javax.xml.ws.Service</b>
and provides method to create a service and retrieve port which is the Service Endpoint Interface
for the Web Service deployed.</li>
</ul>


<ul>
	<li><b>ConverterPortType.java</b> - This is the same file that is used
to deploy the Web Service. This is the Service Endpoint Interface that is required to access
the Web Methods exposed by the Service.</li>
</ul>


<ul>
	<li><b>jaxws-tools.bat wsimport</b> - This is the utility in Geronimo which
takes the help of <b>jaxws-tools</b> provided by <b>Sun</b> to create
client stubs</li>
</ul>


<h2><a name="DevelopingClientsforaJAX-WSWebService-DevelopingtheWebbasedClient"></a>Developing
the Web based Client</h2>
<ul>
	<li>Right-click the <b>jaxws-converterclient</b>, and Select <b>New
--&gt; JSP</b>
<br class="atl-forced-newline" /> <br class="atl-forced-newline" /> </li>
	<li>Name the JSP as <b>index.jsp</b> and click <b>Finish</b>
<br class="atl-forced-newline" /> <br class="atl-forced-newline" /></li>
	<li>Add the following code to the <b>index.jsp</b></li>
</ul>


<div class="code panel" style="border-style: solid;border-width: 1px;"><div class="codeHeader
panelHeader" style="border-bottom-width: 1px;border-bottom-style: solid;"><b>index.jsp</b></div><div
class="codeContent panelContent">
<pre class="code-java">

&lt;%@ page language=<span class="code-quote">"java"</span> contentType=<span
class="code-quote">"text/html; charset=ISO-8859-1"</span>
	pageEncoding=<span class="code-quote">"ISO-8859-1"</span>%&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Converter&lt;/title&gt;
&lt;meta content=<span class="code-quote">"text/html; CHARSET=iso-8859-1"</span>
http-equiv=<span class="code-quote">"Content-Type"</span>&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;center&gt;
&lt;h3&gt;This from invokes a Web Service.&lt;/h3&gt;
&lt;br&gt;
Please type an amount and click submit to see the result.
&lt;form action=<span class="code-quote">"index.jsp"</span>&gt;Amount(in
Dollars): &lt;input type=<span class="code-quote">"text"</span>
	name=<span class="code-quote">"amount"</span>&gt; &lt;input type=<span
class="code-quote">"submit"</span> value=<span class="code-quote">"Submit"</span>&gt;&lt;/form&gt;
&lt;br&gt;&lt;br&gt;
&lt;jsp:include page=<span class="code-quote">"result.jsp"</span>&gt;&lt;/jsp:include&gt;
&lt;/center&gt;
&lt;/body&gt;
&lt;/html&gt;

</pre>
</div></div>

<ul>
	<li>Right click again and add another JSP named <b>result.jsp</b>
<br class="atl-forced-newline" /> <br class="atl-forced-newline" /> </li>
	<li>Add the following code to <b>result.jsp</b></li>
</ul>


<div class="code panel" style="border-style: solid;border-width: 1px;"><div class="codeHeader
panelHeader" style="border-bottom-width: 1px;border-bottom-style: solid;"><b>result.jsp</b></div><div
class="codeContent panelContent">
<pre class="code-java">

&lt;%@ page language=<span class="code-quote">"java"</span> contentType=<span
class="code-quote">"text/html; charset=ISO-8859-1"</span>
	pageEncoding=<span class="code-quote">"ISO-8859-1"</span>%&gt;
&lt;%@page <span class="code-keyword">import</span>=<span class="code-quote">"java.math.BigDecimal"</span>%&gt;
&lt;%@ page <span class="code-keyword">import</span>=<span class="code-quote">"org.apache.geronimo.samples.jaxws.Converter"</span>%&gt;
&lt;%@ page <span class="code-keyword">import</span>=<span class="code-quote">"org.apache.geronimo.samples.jaxws.ConverterPortType"</span>%&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Converter&lt;/title&gt;
&lt;meta content=<span class="code-quote">"text/html; CHARSET=iso-8859-1"</span>
http-equiv=<span class="code-quote">"Content-Type"</span>&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;%
	<span class="code-object">String</span> amount = request.getParameter(<span
class="code-quote">"amount"</span>);

	<span class="code-keyword">if</span> (amount != <span class="code-keyword">null</span>
&amp;&amp; amount.trim().length() &gt; 0) {

		out.println(<span class="code-quote">"&lt;center&gt;"</span>);

		<span class="code-keyword">try</span> {
			BigDecimal dollars = <span class="code-keyword">new</span> BigDecimal(amount);
                        Converter conv = <span class="code-keyword">new</span>
Converter();
			ConverterPortType port = conv.getConverterPort();
			BigDecimal rupees = port.dollarToRupees(dollars);
			BigDecimal euros = port.rupeesToEuro(rupees);

			out.println(dollars + <span class="code-quote">" Dollars equals to "</span>
+ rupees
					+ <span class="code-quote">" Rupees"</span>);
			out.println(<span class="code-quote">"&lt;br&gt;"</span>);
			out.println(rupees + <span class="code-quote">" Rupees equals to "</span> +
euros
					+ <span class="code-quote">" Euros"</span>);

		} <span class="code-keyword">catch</span> (Exception e) {
			out.println(<span class="code-quote">"Error: "</span> + e.getMessage());
		}

		out.println(<span class="code-quote">"&lt;center&gt;"</span>);
	}
%&gt;
&lt;/body&gt;
&lt;/html&gt;

</pre>
</div></div>

<p>This concludes the development section of our web based client.</p>

<h2><a name="DevelopingClientsforaJAX-WSWebService-Using%3Cserviceref%3EelementtoconsumeWebService%3A"></a>Using
&lt;service-ref&gt; element to consume Web Service:</h2>
<ul>
	<li>In our previous <b>result.jsp</b> we accessed the Web Service by instantiating
the Service class. This can slightly overhead the client application.
<br class="atl-forced-newline" /> <br class="atl-forced-newline" /></li>
	<li>We can also use <b>JNDI Naming</b> service to directly access our web
service without instantiating any Service class.
<br class="atl-forced-newline" /> <br class="atl-forced-newline" /></li>
	<li>We need to add a service-ref element to <b>web.xml</b> to give necessary
information about WSDL file location, Service name etc .,</li>
</ul>


<div class="code panel" style="border-style: solid;border-width: 1px;"><div class="codeHeader
panelHeader" style="border-bottom-width: 1px;border-bottom-style: solid;"><b>Modified
web.xml</b></div><div class="codeContent panelContent">
<pre class="code-java">

&lt;service-ref&gt;
	&lt;service-ref-name&gt;services/Converter&lt;/service-ref-name&gt;
	&lt;service-<span class="code-keyword">interface</span>&gt;
		org.apache.geronimo.samples.jaxws.Converter
	&lt;/service-<span class="code-keyword">interface</span>&gt;
	&lt;wsdl-file&gt;
		http:<span class="code-comment">//localhost:8080/jaxws-converter/converter?wsdl
</span>	&lt;/wsdl-file&gt;
&lt;/service-ref&gt;

</pre>
</div></div>

<ul>
	<li>Let us walkthrough the elements that we added to <b>web.xml</b> <br
class="atl-forced-newline" /> <br class="atl-forced-newline" />
	<ul>
		<li><b>service-ref-name</b> - This is the name by which JNDI identifies
our deployed service. The location that these services are located are <b>java:comp/env/&lt;service-ref-name&gt;</b></li>
		<li><b>service-interface</b> - This is the interface which client uses
to access the web service and create necessary stubs under covers. This interface should compulsorily
extend either <b>javax.xml.ws.Service</b> or <b>javax.xml.rpc.Service</b>.
Observer that this is not the Service-Endpoint Interface that we used to deploy the service.</li>
		<li><b>wsdl-file</b> - The location of WSDL file specified the location
of deployed web service. Suppose the web service is deployed at *<a href="http://localhost:8080/jaxws-converter/converter*"
class="external-link" rel="nofollow">http://localhost:8080/jaxws-converter/converter*</a>,
then the WSDL file is located at *<a href="http://localhost:8080/jaxws-converter/converter?wsdl*"
class="external-link" rel="nofollow">http://localhost:8080/jaxws-converter/converter?wsdl*</a>
<br class="atl-forced-newline" /> <br class="atl-forced-newline" /></li>
	</ul>
	</li>
	<li>The changes required in accessing the service are as follows:</li>
</ul>


<div class="code panel" style="border-style: solid;border-width: 1px;"><div class="codeHeader
panelHeader" style="border-bottom-width: 1px;border-bottom-style: solid;"><b>Modified
result.jsp</b></div><div class="codeContent panelContent">
<pre class="code-java">
........
&lt;%@ page <span class="code-keyword">import</span>=<span class="code-quote">"javax.naming.InitialContext"</span>
%&gt;
&lt;%@ page <span class="code-keyword">import</span>=<span class="code-quote">"javax.naming.Context"</span>
%&gt;
........
Context ctx= <span class="code-keyword">new</span> InitialContext();
<span class="code-object">Object</span> obj = ctx.lookup(<span class="code-quote">"java:comp/env/services/Converter"</span>);
Converter service= (Converter) obj;
ConverterPortType port = service.getConverterPort();
BigDecimal rupees =port.dollarToRupees(dollars);
BigDecimal euros = port.rupeesToEuro(rupees);
........
</pre>
</div></div>

<h2><a name="DevelopingClientsforaJAX-WSWebService-DeployingandTestingtheWebClient"></a>Deploying
and Testing the Web Client</h2>
<h3><a name="DevelopingClientsforaJAX-WSWebService-Deploy"></a>Deploy</h3>
<ul>
	<li>Right click on the <b>Apache Geronimo</b> Server Runtime present in
the servers view and select <b>Add and Remove Projects</b>
<br class="atl-forced-newline" /> <br class="atl-forced-newline" /></li>
	<li>Add <b>jaxws-converterclient</b> to configured projects list and then
click <b>Finish</b>
<br class="atl-forced-newline" /> <br class="atl-forced-newline" /></li>
	<li>Wait for some time till the server status changes to <b>Synchronized</b>
<div class='panelMacro'><table class='infoMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/information.gif" width="16"
height="16" align="absmiddle" alt="" border="0"></td><td>Note that you may
get some error and/or warning messages in the server console tab. These usually can be ignored
as long as the projects deploy correctly, which they should.</td></tr></table></div>
</li>
</ul>


<h3><a name="DevelopingClientsforaJAX-WSWebService-Testing"></a>Testing</h3>
<ul>
	<li>Right click the <b>index.jsp</b> present under WebContent directory
of our project and select <b>Run As --&gt; Run On Server</b>
<br class="atl-forced-newline" /> <br class="atl-forced-newline" /> <span class="image-wrap"
style=""><img src="/confluence/download/attachments/93413/runonserver.gif?version=1&amp;modificationDate=1215502897000"
style="border: 0px solid black" /></span> <br class="atl-forced-newline" />
<br class="atl-forced-newline" /></li>
	<li>In the popup, check the check box <b>Always use this server when running
the project</b> and then click <b>Finish</b>
<br class="atl-forced-newline" /> <br class="atl-forced-newline" /></li>
	<li>Now Eclipse will try to open the JSP in a web browser which shows you a form to
enter amount in Dollars.
<br class="atl-forced-newline" /> <br class="atl-forced-newline" /></li>
	<li>Enter any amount and press <b>submit</b>, the jsp should display the
result that is returned by the web service.
<br class="atl-forced-newline" /> <br class="atl-forced-newline" /> <span class="image-wrap"
style=""><img src="/confluence/download/attachments/93413/weboutput.gif?version=1&amp;modificationDate=1215502995000"
style="border: 0px solid black" /></span> <br class="atl-forced-newline" />
<br class="atl-forced-newline" /></li>
</ul>


<div class='panelMacro'><table class='infoMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/information.gif" width="16"
height="16" align="absmiddle" alt="" border="0"></td><td><b>Converter</b><br
/>Note that we accessed the web service in the JSP by creating a <b>service</b>,
and then retrieving the <b>Converter</b> port from the WSDL document.</td></tr></table></div>


<h1><a name="DevelopingClientsforaJAX-WSWebService-POJOClient"></a>POJO
Client</h1>

<p>Here we will have a look on how to create a plain old java client for consuming the
web service.</p>

<h2><a name="DevelopingClientsforaJAX-WSWebService-DevelopingtheClient"></a>Developing
the Client</h2>
<ul>
	<li>Create a Java Project <br class="atl-forced-newline" /> <br class="atl-forced-newline"
/>
	<ul>
		<li>Select <b>File --&gt; New --&gt; Project</b>
<br class="atl-forced-newline" /> <br class="atl-forced-newline" /></li>
		<li>In the popup window select <b>Java --&gt; Java Project</b> and
then click <b>Next</b>
<br class="atl-forced-newline" /> <br class="atl-forced-newline" /></li>
		<li>Name the project as <b>jaxws-converterpojoclient</b> and click <b>Finish</b>
in the <b>New Java Project</b> dialog.
<br class="atl-forced-newline" /> <br class="atl-forced-newline" /></li>
	</ul>
	</li>
	<li>Copy the files <b>Converter.java</b> and <b>ConverterPortType.java</b>
into the appropriate package.
<br class="atl-forced-newline" /> <br class="atl-forced-newline" /></li>
	<li>Add another class with the name <b>ConverterClient.java</b> in the
same package.
<br class="atl-forced-newline" /> <br class="atl-forced-newline" /></li>
	<li>Add the following code to the <b>ConverterClient.java</b></li>
</ul>


<div class="code panel" style="border-style: solid;border-width: 1px;"><div class="codeHeader
panelHeader" style="border-bottom-width: 1px;border-bottom-style: solid;"><b>ConverterClient.java</b></div><div
class="codeContent panelContent">
<pre class="code-java">

<span class="code-keyword">package</span> org.apache.geronimo.samples.jaxws;

<span class="code-keyword">import</span> java.io.BufferedReader;
<span class="code-keyword">import</span> java.io.InputStreamReader;
<span class="code-keyword">import</span> java.math.BigDecimal;

<span class="code-keyword">public</span> class ConverterClient {
	<span class="code-keyword">public</span> <span class="code-keyword">static</span>
void main(<span class="code-object">String</span> args[]) {
		<span class="code-keyword">try</span> {
			BufferedReader dataIn = <span class="code-keyword">new</span> BufferedReader(<span
class="code-keyword">new</span> InputStreamReader(
					<span class="code-object">System</span>.in));
			<span class="code-object">System</span>.out.println(<span class="code-quote">"enter
a amount to convert:"</span>);
			<span class="code-object">String</span> number = <span class="code-keyword">new</span>
<span class="code-object">String</span>();
			number = dataIn.readLine();
			BigDecimal amount = <span class="code-keyword">new</span> BigDecimal(number);
			Converter conv = <span class="code-keyword">new</span> Converter();
			ConverterPortType port = conv.getConverterPort();
			BigDecimal result = port.dollarToRupees(amount);
			<span class="code-object">System</span>.out.println(amount + <span class="code-quote">"
dollars equals to "</span> + result
					+ <span class="code-quote">" rupees"</span>);
			BigDecimal result1 = port.rupeesToEuro(result);
			<span class="code-object">System</span>.out.println(result + <span class="code-quote">"
rupees equals to "</span> + result1
					+ <span class="code-quote">" euros"</span>);
		} <span class="code-keyword">catch</span> (Exception e) {
			e.printStackTrace();
		}
	}

}


</pre>
</div></div>

<p>This completes the development section of our POJO client</p>

<h2><a name="DevelopingClientsforaJAX-WSWebService-Addingnecessaryjarfilestotheclasspath"></a>Adding
necessary jar files to the class path</h2>

<div class='panelMacro'><table class='noteMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/warning.gif" width="16" height="16"
align="absmiddle" alt="" border="0"></td><td><b>Errors in Java Project</b><br
/>Now our Java project might show many errors and even if we resolve the errors the application
may not run properly. This is due to the missing jar files required to access and understand
SOAP messages sent by web service.</td></tr></table></div>

<ul>
	<li>Right click the <b>jaxws-converterpojoclient</b> project and then select
<b>Properties</b>
<br class="atl-forced-newline" /> <br class="atl-forced-newline" /></li>
	<li>Select <b>Java Build Path</b> and then go to <b>Libraries</b>
tab
<br class="atl-forced-newline" /> <br class="atl-forced-newline" /> <span class="image-wrap"
style=""><img src="/confluence/download/attachments/93413/buildpath.gif?version=1&amp;modificationDate=1215502897000"
style="border: 0px solid black" /></span> <br class="atl-forced-newline" />
<br class="atl-forced-newline" /></li>
	<li>Click on <b>Add External JARs...</b> and then add the <b>axis2-adb-1.3.jar</b>
from the location <b>&lt;GERONIMO_INSTALL_DIR&gt;/repository/org/apache/axis2/axis2-adb/1.3/</b>
and then click <b>Open</b>
<div class='panelMacro'><table class='infoMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/information.gif" width="16"
height="16" align="absmiddle" alt="" border="0"></td><td><b>Version Numbers</b><br
/>The version numbers may differ slightly. It wont matter much.</td></tr></table></div>
</li>
	<li>Repeat the above step for the following external JARS</li>
</ul>


<div class="panel" style="background-color: #FFFFCE;border-color: #ccc;border-style: solid;border-width:
1px;"><div class="panelHeader" style="border-bottom-width: 1px;border-bottom-style:
solid;border-bottom-color: #ccc;background-color: #F7D6C1;"><b>External JARS Required(Axis2)</b></div><div
class="panelContent" style="background-color: #FFFFCE;">
<p><b>axis2-adb-1.3.jar</b> - &lt;GERONIMO_INSTALL_DIR&gt;/repository/org/apache/axis2/axis2-adb/1.3/axis2-adb-1.3.jar<br/>
<b>axis2-java2wsdl-1.3.jar</b> - &lt;GERONIMO_INSTALL_DIR&gt;\repository\org\apache\axis2\axis2-java2wsdl\1.3\axis2-java2wsdl-1.3.jar<br/>
<b>axis2-jaxws-1.3.jar</b> - &lt;GERONIMO_INSTALL_DIR&gt;\repository\org\apache\axis2\axis2-jaxws\1.3\axis2-jaxws-1.3.jar<br/>
<b>axis2-kernel-1.3.jar</b> - &lt;GERONIMO_INSTALL_DIR&gt;\repository\org\apache\axis2\axis2-kernel\1.3\axis2-kernel-1.3.jar<br/>
<b>axis2-metadata-1.3.jar</b> - &lt;GERONIMO_INSTALL_DIR&gt;\repository\org\apache\axis2\axis2-metadata\1.3\axis2-metadata-1.3.jar<br/>
<b>axis2-saaj-1.3.jar</b> - &lt;GERONIMO_INSTALL_DIR&gt;\repository\org\apache\axis2\axis2-saaj\1.3\axis2-saaj-1.3.jar<br/>
<b>axiom-api-1.2.5.jar</b> - &lt;GERONIMO_INSTALL_DIR&gt;\repository\org\apache\ws\commons\axiom\axiom-api\1.2.5\axiom-api-1.2.5.jar<br/>
<b>axiom-dom-1.2.5.jar</b> - &lt;GERONIMO_INSTALL_DIR&gt;\repository\org\apache\ws\commons\axiom\axiom-dom\1.2.5\axiom-dom-1.2.5.jar<br/>
<b>axiom-impl-1.2.5.jar</b> - &lt;GERONIMO_INSTALL_DIR&gt;\repository\org\apache\ws\commons\axiom\axiom-impl\1.2.5\axiom-impl-1.2.5.jar<br/>
<b>XmlSchema-1.3.1.jar</b> - &lt;GERONIMO_INSTALL_DIR&gt;\repository\org\apache\ws\commons\schema\XmlSchema\1.3.1\XmlSchema-1.3.1.jar<br/>
<b>neethi-2.0.jar</b> - &lt;GERONIMO_INSTALL_DIR&gt;\repository\org\apache\neethi\neethi\2.0\neethi-2.0.jar<br/>
<b>wsdl4j-1.6.1.jar</b> - &lt;GERONIMO_INSTALL_DIR&gt;\repository\wsdl4j\wsdl4j\1.6.1\wsdl4j-1.6.1.jar<br/>
<b>xml-resolver-1.1.jar</b> - &lt;GERONIMO_INSTALL_DIR&gt;\repository\xml-resolver\xml-resolver\1.1\xml-resolver-1.1.jar<br/>
<b>xmlbeans-2.3.0.jar</b> - &lt;GERONIMO_INSTALL_DIR&gt;\repository\org\apache\xmlbeans\xmlbeans\2.3.0\xmlbeans-2.3.0.jar<br/>
<b>commons-codec-1.3.jar</b> - &lt;GERONIMO_INSTALL_DIR&gt;\repository\commons-codec\commons-codec\1.3\commons-codec-1.3.jar<br/>
<b>commons-httpclient-3.0.1.jar</b> - &lt;GERONIMO_INSTALL_DIR&gt;\repository\commons-httpclient\commons-httpclient\3.0.1\commons-httpclient-3.0.1.jar<br/>
<b>wstx-asl-3.2.1.jar</b> - &lt;GERONIMO_INSTALL_DIR&gt;\repository\woodstox\wstx-asl\3.2.1\wstx-asl-3.2.1.jar</p>

<p>Also add Server Runtime Library to class path (i.e., via clicking on <b>Add
Library...</b>) which reduces the effort for adding some more jars.</p>

<p>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.</p>
</div></div>

<p>This completes the adding external JARs to the project</p>
<div class='panelMacro'><table class='tipMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/check.gif" width="16" height="16"
align="absmiddle" alt="" border="0"></td><td><b>JRE 6</b><br
/>If you are running Java 6 you don't need to worry about the external JARs as they are
all inbuilt into JRE 6. But you should copy all the files generated by wsimport command into
appropriate packages as Runtime Modeller in Java 6 is slightly different.</td></tr></table></div>


<h2><a name="DevelopingClientsforaJAX-WSWebService-Testing"></a>Testing</h2>

<ol>
	<li>Now Right click the <b>ConverterClient.class</b> and select <b>Run
As --&gt; Run as Java Application</b>
<br class="atl-forced-newline" /> <br class="atl-forced-newline" /></li>
	<li>Now enter the amount in the console window of Eclipse
<br class="atl-forced-newline" /> <br class="atl-forced-newline" /></li>
	<li>The output will be shown which is retrieved by accessing the methods of Web service.
<br class="atl-forced-newline" /> <br class="atl-forced-newline" /> <span class="image-wrap"
style=""><img src="/confluence/download/attachments/93413/output.gif?version=1&amp;modificationDate=1215502897000"
style="border: 0px solid black" /></span> <br class="atl-forced-newline" />
<br class="atl-forced-newline" /></li>
</ol>


<p>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.</p>
    </div>
        <div id="commentsSection" class="wiki-content pageSection">
        <div style="float: right;">
            <a href="http://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
        </div>
        <a href="http://cwiki.apache.org/confluence/display/GMOxDOC22/Developing+Clients+for+a+JAX-WS+Web+Service">View
Online</a>
        |
        <a href="http://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=93413&revisedVersion=13&originalVersion=12">View
Changes</a>
                |
        <a href="http://cwiki.apache.org/confluence/display/GMOxDOC22/Developing+Clients+for+a+JAX-WS+Web+Service?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message