Search This Blog

Showing posts with label Sample webservice. Show all posts
Showing posts with label Sample webservice. Show all posts

Wednesday, March 25, 2009

Web Service in Java Sample

Go to web-inf\src :

use cp.bat to set classpath.
download the same jar and paste into ur lib folder..

jars
****
set AXIS_HOME=C:\Tomcat5.0\webapps\axis
set AXIS_LIB=%AXIS_HOME%\WEB-INF\lib

set AXISCLASSPATH=

%AXIS_LIB%\axis.jar;
%AXIS_LIB%\commons-discovery-0.2.jar;
%AXIS_LIB%\wsdl4j-1.5.1.jar;
%AXIS_LIB%\commons-logging-1.0.4.jar;
%AXIS_LIB%\jaxrpc.jar;
%AXIS_LIB%\saaj.jar;
%AXIS_LIB%\log4j-1.2.8.jar;
%AXIS_LIB%\xml-apis.jar;
%AXIS_LIB%\xercesImpl.jar;
%AXIS_LIB%\servlet-api.jar;



just paste into tomcat\webapps

(((( ex 1: ))))

webservice
***********
1) create a java program and rename it as filename.jws
2) copy that filename.jws into tomcat\webapps\axis (project-root)
3) re/start tomcat

client run steps:
*****************
cp.bat
javac CalcClient.java
java CalcClient add 5 5


(((( ex 2: ))))
1) create a java program "filename.java"
2) set classpath and compile and place that .class into WEB-INF\classes folder
3) create a deploy.wsdd and undeploy.wsdd file and store into your filename.java area
4) run this wsdd cmd into cmd promt (( Note : tomcat server must be in run when u exectue this cmd )))

C:\Tomcat5.0\webapps\axis\WEB-INF\src> java org.apache.axis.client.AdminClient deploy.wsdd

5) restart the tomcat
6) check : http://localhost:8080/axis/servlet/AxisServlet
or
http://mdu-vijay:8080/axis/services/MyService?wsdl

7) then run client... (change endpoint to point ur myservice)




Calculator.java
***************

public class Calculator {
public int add(int i1, int i2)
{
return i1 + i2;
}


public int subtract(int i1, int i2)
{
return i1 - i2;
}
}


cp.bat
******
set AXIS_HOME=C:\Tomcat5.0\webapps\axis
set AXIS_LIB=%AXIS_HOME%\WEB-INF\lib
set AXISCLASSPATH=%AXIS_LIB%\axis.jar;%AXIS_LIB%\commons-discovery-0.2.jar;%AXIS_LIB%\wsdl4j-1.5.1.jar;%AXIS_LIB%\commons-logging-1.0.4.jar;%AXIS_LIB%\jaxrpc.jar;%AXIS_LIB%\saaj.jar;%AXIS_LIB%\log4j-1.2.8.jar;%AXIS_LIB%\xml-apis.jar;%AXIS_LIB%\xercesImpl.jar;%AXIS_LIB%\servlet-api.jar;
set classpath=".;%AXISCLASSPATH%";


deploy.wsdd
*************

xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">









undeploy.wsdd
*************





CalcClient.java
***************

//package samples.userguide.example2 ;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import org.apache.axis.utils.Options;

import javax.xml.rpc.ParameterMode;

public class CalcClient
{
public static void main(String [] args) throws Exception {
Options options = new Options(args);

//this is for filename.jws
String endpoint = "http://localhost:" + options.getPort() + "/axis/Calculator.jws";

//this is for service after create C:\Tomcat5.0\webapps\axis\WEB-INF\src> java org.apache.axis.client.AdminClient deploy.wsdd
//String endpoint = "http://localhost:8080/axis/services/MyService";

args = options.getRemainingArgs();

if (args == null || args.length != 3) {
System.err.println("Usage: CalcClient arg1 arg2");
return;
}

String method = args[0];
if (!(method.equals("add") || method.equals("subtract"))) {
System.err.println("Usage: CalcClient arg1 arg2");
return;
}

Integer i1 = new Integer(args[1]);
Integer i2 = new Integer(args[2]);

Service service = new Service();
Call call = (Call) service.createCall();

call.setTargetEndpointAddress( new java.net.URL(endpoint) );
call.setOperationName( method );
call.addParameter( "op1", XMLType.XSD_INT, ParameterMode.IN );
call.addParameter( "op2", XMLType.XSD_INT, ParameterMode.IN );
call.setReturnType( XMLType.XSD_INT );

Integer ret = (Integer) call.invoke( new Object [] { i1, i2 });

System.out.println("Got result : " + ret);
}
}





by
vijay.dr
9842088860
drvijayy2k2@gmail.com

Hit Counter


View My Stats