Search This Blog

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

Tuesday, January 26, 2010

Web Service with Tomcat

//with tomcat


software

*****************************************************************

1. axis2.war from [ axis2-1.5.1-war.zip ] deploy into tomcat

2. axis2-1.5.1-bin.zip extract into d:\axis2-1.5.1-bin

3. set environment


AXIS2_HOME=d:\axis2-1.5.1-bin

PATH=d:\axis2-1.5.1-bin\bin;

CLASS_PATH=d:\axis2-1.5.1-bin\lib\*

TOMCAT_HOME=d:\tomcat6.0


*****************************************************************





1. service program

~~~~~~~~~~~~~~~~~~~~~~~~~~~


d:\temp\aaxis\com\abi> edit HelloWorld.java


package com.abi;

public class HelloWorld {

public String sayHello(String name) {

return "Hello " + name;

}

}



2. services.xml

~~~~~~~~~~~~~~~~~~~~~~~~~~~


d:\temp\aaxis\META-INF> edit services.xml


<service>
   <description>
      This is my first service, which says hello
   </description>
   <parameter name="ServiceClass">com.abi.HelloWorld</parameter>
   <operation name="sayHello">
      <messageReceiver
      class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
   </operation>
</service>







3. D:\temp\aaxis> jar cvf HelloWorld.aar *
~~~~~~~~~~~~~~~~~~~~~~~~~~~

4. copy that HelloWorld.aar into tomcat/webapps/axis2/WEB-INF/services

5. restart tomcat

6. http://localhost:8080/axis2/

7. http://localhost:8080/axis2/services/listServices

8. http://localhost:8080/axis2/services/HelloWorld?wsdl //copy this url

9. D:\temp\aaxis>wsdl2java -uri http://localhost:8080/axis2/services/HelloWorld?wsdl -o client

10. write client program
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

d:\temp\aaxis\client\src\com\abi> edit Test.java

package com.abi;
import com.abi.*;
import com.abi.HelloWorldStub.SayHello;
public class Test {
public static void main(String[] args) throws Exception {
HelloWorldStub stub = new HelloWorldStub();
//Create the request

com.abi.HelloWorldStub.SayHello request = new com.abi.HelloWorldStub.SayHello();

request.setArgs0("vijay Kumar");
//Invoke the service
com.abi.HelloWorldStub.SayHelloResponse response = stub.sayHello(request);
System.out.println("Response : " + response.get_return());
}
}


11. compile with auto generate ant via wsdl2java tool
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


D:\temp\aaxis\client> ant
OR
(manual) D:\temp\aaxis>edit build.xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="antwsdl2java" default="gen" basedir=".">
<path id="axis2.classpath">
<fileset dir="D:/temp/axis2">
  <include name="**/*.jar" />
  </fileset>
  </path>
<target name="gen">
  <taskdef name="axis2-wsdl2java" classname="org.apache.axis2.tool.ant.AntCodegenTask" classpathref="axis2.classpath" />
  <axis2-wsdl2java wsdlfilename="http://localhost:8080/axis2/services/HelloWorld?wsdl" output="d:/temp/aaxis" />
  </target>
  </project>



// RUN Manual Script and write the client program and then do steps for execute
D:\temp\aaxis> ant

12. //Execute the client
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

D:\temp\aaxis\build\classes>java -Djava.ext.dirs=D:/axis2-1.5.1/lib/ com.abi.Test

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