JAX-RPC
It stands
for Java API for XML-based RPC. JAX-RPC is a technology for building web
services and clients that use remote procedure
calls (RPC) and XML. Often used in a distributed client-server model, an
RPC mechanism enables clients to execute procedures on other systems.
With
JAX-RPC, clients and web services have a big advantage: the platform
independence of the Java programming language. In addition, JAX-RPC is not
restrictive: a JAX-RPC client can access a web service that is not running on
the Java platform, and vice versa. This flexibility is possible because JAX-RPC
uses technologies defined by the World Wide Web Consortium (W3C) such as:
SOAP
defines message format
WSDL
describes the various services offered by web service application as xml
file
XML
Schema – defines data types used in WSDL doc.
Writing
Web Service using JAX-RPC (Currency Converter):
These are
the basic steps for creating the web service and client:
1. Create an
interface
2. Code the
implementation class.
3. Use
wscompile to generate wsdl doc.
4. Create
deployment descriptor file
5. Package
the files into a WAR file.
6. Deploy
the WAR file.
7. Create a
client file to access the web service
8. Use
wscompile to generate and compile the web service artifacts needed to connect
to the service.
9. Compile
the client class.
10.
Run the client.
Example
for Creating Web service software:
To write
web service using JAX-RPC, we need JWSDP(Java Web Server Development Package).
This package has wscompile and wsdeploy tools.
wscompile
is used to convert java file into wsdl file whereas wsdeploy is used to package
our web service.
To Create
a web server file:
Create a directory in the name of CurrencyConverter
and create a sub director myCurCon under WEB-INF/Src folder
Step 1:
Create a service end point interface
Rules for
creating service end point interface are as follows:
1. The
interface must extend java.rmi.Remote interface
2. Every
method in this interface must throw java.rmi.Remote exception
3. Every
method return type and its parameter data type in this interface must be java
primitive data types
4. The
interface must not contain any public static final declarations.
package
myCurCon;
public
class ExchangeValues
{
public
docuble dollars; public double euros; publci doubel yens;
}
package
myCurCon;
public
interface CurCon extends java.rmi.Remote
{
public ExchangeValues fromDollars(double dollars)
throws java.rmi.RemoteException;
public ExchangeValues fromEuros(double dollars)
throws java.rmi.RemoteException;
public ExchangeValues fromYens(double dollars)
throws java.rmi.RemoteException;
}
Step 2: Create a class to implement this interface
public class CurConImpl implements CurCon
{
public ExchangeValues fromDollars(Double Dollars) thorws
java.rmi.RemoteException
{
ExchangeValues
ev=new ExchangeValues(); ev.dollars=dollars;
ev.euros=dollars*100;
ev.yens=dollars*200;
return ev;
}
}
Step 3: Compile using javac to create respective classes
for ExchangeValues.java, CurCon.java and CurConImpl.java
Step 4: Use wscompile to create WSDL doc. Inputs to
wscompile are supplied via the following configuration file called config.xml
file.
<? xml
version="1.0" ?> <configuration xmlns="url" >
<service
name="HistoricCurrencyConverter" targetNameSpace="url"
typeNameSpace="url" packageName="myCurCon">
<interface
name="muCurCon.CurCon" /> </service>
</configuration>
- Save
this file as config.xml and use the following command to create wsdl doc.
wscompile
–define – d WEB-INF –classpath WEB-INF/classes – model WEB-INF/model.xml.gz
config.xml
-d specifies the directory to receive the generated
wsdl doc. which will be named as Historic Currencyconverter.wsdl
Step 5: Create another configuration file called
jaxrpc-ri.xml which is used as deployment descriptor file as shown below
<? xml
version="1.0" ?>
<webServices
xmlns="url" targetNameSpace="url"
typeNameSpace="url" urlPatternBase="/converter" >
<endpoint
name="CurrConverter" displayName="Currency Converter"
description="Converts b/w dollars, yens and euros"
interface="myCurCon.curCon" model="/WEB-INF/model.xm.gz"
implementation="myCurCon.CurConImpl"/> <enpointMapping
endPointName="CurrConverter"
urlPattern="/Currency"
/> </webServices>
The tag <endpoint> gives the information to
web server including name of the service end point interface, implementation
file model generated by wscompile etc.
Step 6:
Package and deploying the web service created Create WAR(Web Archive) file. Add
the deployment descriptor and service files to the WAR file. The following
command is used:
jar cf
converter-temp.war WEN-INF
Deploy
the war file using the following command wsdeploy –o converter.war
converter-temp.war
Step 7:
Start tomcat server
Step 8:
Browse to http://localhost:8080/Converter/Currency to see the status of the
currency converter web service. The output will be:
To write a client program
To access the currency converter web service it is
necessary to have a java client program
Step 1: Create xml file named config.xml <? xml
version="1.0" ?>
<configuration
xmlns="url" wsdl location
=http://localhost:8080/Converter/Currency?wsdl
packageName="myCurConClient" />
</configuration
Step 2:
Use wscompile to load wsdl from the url specified in the location attribute of
config.xml. Use the following command for that wscomplie –gen –keep –d classes
–s src config.xml
This
instructs to generate a class file from the wsdl and keep the java source file
in src
folder.
Step 3:
Create java client program for accessing the web services as shown below
package mycurConClient;
public
class CurConBean
{
private
double value=1.0;
private
String Currency="dollars"; public void setValue(double value)
{
this.value=value;
return;
}
public
void setCurrency(String currency)
{
this.currency=currency;
return;
}
public
ExchangeValues getExValues()
{
ExchangeValues
ev=null;
CurCon=(new
HistoricCurrencyConverter_impl().getCurConPort()); try
{
if(currecny.equals("euros")
{
ev=curcon.fromEuros(value);
}
else
if(currency.equals("yens")
{
ev=curcon.fromYens(value);
}
}catch(Exception
e){} return ev;
}
}
Step 4:
Compile using javac
Step 5:
Create a jsp for using this bean class to perform a currency conversion class
and display the result in a HTML file
<html>
<jsp:useBean
id="client" class="myCurConClient.CurConBean" />
<body>
<%
String
value=request.getPArameter(val); %>
<jsp:setProperty
name="client" property="Currency" value="curr"
/> <jsp:setProperty name="client" property="Currency"
value="cur" /> Converted amount:
</jsp:getProperty
name="Client" property=ExValue" /> </body>
</html>
Step 6:
In the browser location bar type
http://localhost:8080/ConverterClient/Conver.jspx?cur=”euros” val=”500.30”
This
invokes the web service from the server and converts the euros value 500.30 to
equivalent dollars and yens value and displays the result.
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.