7/31/2019 Ex 2 Creating Web Services Using JAX-RPC
1/17
Ex. 2:
Creating Web Services using JAX-RPC
AIM:
To create a web service using JAX_RPC and access it from a java client.
Procedure:
Server:
Create an interface that extends Remote
Create a class that implements the interface
Define the Service in the implementation class.
Compile the Interface and Implementation class.
Create the configuration file.Generate the mapping.xml and MyfirstService.wsdl using wscompile.exe
Packaging and Deploying:
Create a new web component using deployment tool(NewFileWeb component)
Specify the .war (web archive) file to be created.
Add the interface, implementation class, mapping.xml and MyfirstService.wsdl
Specify the context root.
choose web service endpoint
Add Alias name
ToolsDeploy
Verify URL: http://localhost:8080/math-service/math?wsdl
Client:
Create the config-wsdl.xml that specifies the URL
Generate the Stub classes.Specify the endpoint address in the Client class.
Compile and Run the client.
7/31/2019 Ex 2 Creating Web Services Using JAX-RPC
2/17
Server:
//MathFace.java
package math;
import java.rmi.Remote;import java.rmi.RemoteException;
public interface MathFace extends Remote {
public int add(int a, int b) throws RemoteException;
}
MathImpl.java
package math;
import java.rmi.RemoteException;
public class MathImpl implements MathFace {
public int add(int a, int b) throws RemoteException {
return a + b;
}
}
config.xml
7/31/2019 Ex 2 Creating Web Services Using JAX-RPC
3/17
I) To start the default server:
Start->Program filesSun MicrosystemsSun AppServer-Start Default Server
Start->Program filesSun MicrosystemsSun AppServer-Deploytool
II) To create a new domain and start the server:
Z:\reva>set path=C:\Sun\Appserver\bin;c:\Sun\Appserver\jdk\bin
Z:\reva>asadmin
asadmin>create-domain --adminuser admin --adminport 4848 domain2Please enter the admin password>
Please enter the admin password again>
Please enter the master password>
Please enter the master password again>
Using default port 8080 for HTTP Instance.
Using default port 7676 for JMS.Using default port 3700 for IIOP.
Using default port 8181 for HTTP_SSL.Using default port 3820 for IIOP_SSL.
Using default port 3920 for IIOP_MUTUALAUTH.
Using default port 8686 for JMX_ADMIN.
Domain domain2 created.
asadmin>start-domain domain2
Starting Domain domain2, please wait.
Log redirected to C:\Sun\AppServer\domains\domain2\logs\server.log.
Please enter the master password>
Domain domain2 is ready to receive client requests. Additional services are being started in background.
..
asadmin>stop-domain domain2
asadmin>exit
Z:\Reva>
Create build directory
7/31/2019 Ex 2 Creating Web Services Using JAX-RPC
4/17
wscompile command, which reads the config.xml file created earlier, creates the MyFirstService.wsdl file
and mapping.xml in the build directory.
MyFirstService.wsdl
7/31/2019 Ex 2 Creating Web Services Using JAX-RPC
5/17
mapping.xml
math
urn:Foo
math
urn:Foo
math.MyFirstService
serviceNS:MyFirstService
MathFacePort
MathFacePort
math.MathFace
portTypeNS:MathFace
bindingNS:MathFaceBinding
add
add
0
int
wsdlMsgNS:MathFace_add
int_1
IN
1int
wsdlMsgNS:MathFace_add
int_2
IN
int
wsdlMsgNS:MathFace_addResponseresult
7/31/2019 Ex 2 Creating Web Services Using JAX-RPC
6/17
Packaging and Deploying the Service:
Steps:
7/31/2019 Ex 2 Creating Web Services Using JAX-RPC
7/17
7/31/2019 Ex 2 Creating Web Services Using JAX-RPC
8/17
ContextRoot: /math-service
Click Edit Contents, Add MathFace.class, MathImpl.class and the generated MyFirstWebService.wsdl and
mapping.xml from the build directory.
7/31/2019 Ex 2 Creating Web Services Using JAX-RPC
9/17
7/31/2019 Ex 2 Creating Web Services Using JAX-RPC
10/17
Endpoint Address: math
7/31/2019 Ex 2 Creating Web Services Using JAX-RPC
11/17
Alias Name: /math
7/31/2019 Ex 2 Creating Web Services Using JAX-RPC
12/17
7/31/2019 Ex 2 Creating Web Services Using JAX-RPC
13/17
Verify the endpoint address: math
[ Note: To add a new server instance created using asadmin, FileAdd ServerLocalhost:4849admin, pwd]
Select Tools Deploy
7/31/2019 Ex 2 Creating Web Services Using JAX-RPC
14/17
7/31/2019 Ex 2 Creating Web Services Using JAX-RPC
15/17
Tools Descriptor Viewer
URL: http://localhost:8080/math-service/math?wsdl
7/31/2019 Ex 2 Creating Web Services Using JAX-RPC
16/17
Client
config-wsdl.xml
// MathClient.java
package sstub;
import javax.xml.rpc.Stub;
public class MathClient {
private String endpointAddress;
public static void main(String argv[]) {
try {
// Invoke createProxy() to create a stub object
Stub stub = createProxy();
// Set the endpoint address the stub uses to access the service
stub._setProperty(javax.xml.rpc.Stub.ENDPOINT_ADDRESS_PROPERTY,
"http://localhost:8080/math-service/math");
// Cast the stub to the service endpoint interface (MathFace)
MathFace math = (MathFace) stub;
// Invoke the add method
System.out.println(math.add(12, 24));
} catch (Exception ex) {
ex.printStackTrace();
}
}
private static Stub createProxy() {// Create a stub object
// Note that MyFirstService_Impl, generated by wscompile, is implementation-specific
return (Stub) (new MyFirstService_Impl().getMathFacePort());}
}
7/31/2019 Ex 2 Creating Web Services Using JAX-RPC
17/17
[ Note: Create sstub directory under build dir so that all generated files will be placed in that folder ]
Z:\reva> wscompile -gen:client -d build -classpath build config-wsdl.xml
[copy C:\Sun\Appserver\ lib\*.jar to C:\Sun\Appserver\ jdk\jre\lib\ext; this way, you don't have to worry about
the classpath for the JAX-RPC classes. ]
Z:\reva>javac -classpath build -d build MathClient.java
Z:\reva>java -classpath build sstub.MathClient
36
RESULT:
Top Related