一个webSerice 学习的DEMO,在web上发布一个服务,就是要用到webservice,java实现webService用多种形式,其中axis也是比较流行的一种。下面做个简单的例子来测试。
1下载axis,下载地址是 [b]http://ws.apache.org/axis/[/b]
1.编写服务端程序HelloClient
public class HelloClient
{
public String getName(String name)
{
return "hello "+name;
}
}
2、将源码拷贝到Axis_HOME下,重命名为 HelloClient.jws
3、访问连接http://localhost:8080/Axis/HelloClient.jws?wsdl,页面显示Axis自动生成的wsdl
4、编写访问服务的客户端 TestHelloClient.java
import org.apache.Axis.client.Call;
import org.apache.Axis.client.Service;
import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceException;
import java.net.MalformedURLException;
import java.rmi.RemoteException;
public class SayHelloClient2
{
public static void main(String[] args)
{
try
{
String endpoint =
"http://localhost:8080/Axis/HelloClient.jws";
Service service = new Service();
[b]Call call = null;
call = (Call) service.createCall();
call.setOperationName(new QName(
"http://localhost:8080/Axis/HelloClient.jws",
"getName"));
call.setTargetEndpointAddress
(new java.net.URL(endpoint));
String ret = (String) call.invoke(new Object[]
{"zhangsan"}); [/b]
System.out.println("return value is " + ret);
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
1下载axis,下载地址是 [b]http://ws.apache.org/axis/[/b]
1.编写服务端程序HelloClient
public class HelloClient
{
public String getName(String name)
{
return "hello "+name;
}
}
2、将源码拷贝到Axis_HOME下,重命名为 HelloClient.jws
3、访问连接http://localhost:8080/Axis/HelloClient.jws?wsdl,页面显示Axis自动生成的wsdl
4、编写访问服务的客户端 TestHelloClient.java
import org.apache.Axis.client.Call;
import org.apache.Axis.client.Service;
import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceException;
import java.net.MalformedURLException;
import java.rmi.RemoteException;
public class SayHelloClient2
{
public static void main(String[] args)
{
try
{
String endpoint =
"http://localhost:8080/Axis/HelloClient.jws";
Service service = new Service();
[b]Call call = null;
call = (Call) service.createCall();
call.setOperationName(new QName(
"http://localhost:8080/Axis/HelloClient.jws",
"getName"));
call.setTargetEndpointAddress
(new java.net.URL(endpoint));
String ret = (String) call.invoke(new Object[]
{"zhangsan"}); [/b]
System.out.println("return value is " + ret);
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}