一、 java-java实现WebService访问(来自网络)
1.环境
J2SE SDK 1.5
2.到 http://ws.apache.org/axis/
3.解压缩安装包,将axis-version/webapps下的axis包拷贝到TOMCAT_HOME/webapps/下,
以下约定AXIS_HOME为该TOMCAT_HOME/webapps/axis目录
4.启动tomcat,访问http://localhost:8080/axis 检查安装是否成功
5.以上步骤执行成功,可以开发webservice例子了
axis支持三种web service的部署和开发,分别为:
1。Dynamic Invocation Interface ( DII)
2。 Stubs方式
3。Dynamic Proxy方式
二、编写DII(Dynamic Invocation Interface )方式web服务
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();
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"});
System.out.println("return value is " + ret);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
三、编写Dynamic Proxy方式访问服务
1.编写部署服务端程序,同上边DII方式,本次仍使用上边部署的HelloClient
2.编写代理接口
public interface HelloClientInterface extends java.rmi.Remote {
public String getName(String name) throws java.rmi.RemoteException;
}
3.编写并执行客户端程序TestHelloClient.java
import javax.xml.rpc.Service;
import javax.xml.rpc.ServiceFactory;
import java.net.URL;
import javax.xml.namespace.QName;
public class TestHelloClient {
public static void main(String[] args) {
try
{
String wsdlUrl = "http://localhost:8080/axis/HelloClient.jws?wsdl";
String nameSpaceUri = "http://localhost:8080/axis/HelloClient.jws";
String serviceName = "HelloClientService";
String portName = "HelloClient";
ServiceFactory serviceFactory = ServiceFactory.newInstance();
Service afService = serviceFactory.createService(new URL(wsdlUrl),
new QName(nameSpaceUri, serviceName));
HelloClientInterface proxy = (HelloClientInterface)
afService.getPort(new QName(
nameSpaceUri, portName), HelloClientInterface.class);
System.out.println("return value is "+proxy.getName("john") ) ;
}catch(Exception ex)
{
ex.printStackTrace() ;
}
}
}
四、编写wsdd发布web服务,编写stub client访问web服务
1.编写服务端程序server,SayHello.java,编译server.SayHello.java
package server;
public class SayHello {
public String getName(String name)
{
return "hello "+name;
}
}
2.编写LogHandler.java
import org.apache.axis.AxisFault;
import org.apache.axis.Handler;
import org.apache.axis.MessageContext;
import org.apache.axis.handlers.BasicHandler;
import java.util.Date;
public class LogHandler extends BasicHandler {
public void invoke(MessageContext msgContext) throws AxisFault
{
/** Log an access each time we get invoked.
*/
try {
Handler serviceHandler = msgContext.getService();
Integer numAccesses =
(Integer)serviceHandler.getOption("accesses");
if (numAccesses == null)
numAccesses = new Integer(0);
numAccesses = new Integer(numAccesses.intValue() + 1);
Date date = new Date();
String result = date + ": service " +
msgContext.getTargetService() +
" accessed " + numAccesses + " time.";
serviceHandler.setOption("accesses", numAccesses);
System.out.println(result);
} catch (Exception e) {
throw AxisFault.makeFault;
}
}
}
3..编写wsdd文件
deploy.wsdd
<deployment xmlns="http://xml.apache.org/axis/wsdd/"
xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
<handler name="print" type="java:LogHandler"/>
<service name="sayhello" provider="java:RPC">
<requestFlow>
<handler type="print"/>
</requestFlow>
<parameter name="className" value="server.SayHello"/>
<parameter name="allowedMethods" value="*"/>
</service>
</deployment>
3.将编译后的文件拷贝到AXIS_HOME/WEB-INF/classes下,如:D:/tomcat/webapps/axis/WEB-INF/classes
4.发布服务:
java org.apache.axis.client.AdminClient deploy.wsdd
5.生成client stub文件
a:方式1
将SayHello.java拷贝到AXIS_HOME/下,重命名为SayHello.jws,执行下面的命令生存client stub
java org.apache.axis.wsdl.WSDL2Java -p client http://localhost:8080/axis/services/SayHello.jws?wsdl
b:方式2
执行如下命令生成SayHello.wsdl
java org.apache.axis.wsdl.Java2WSDL -oSayHello.wsdl -lhttp://localhost:8080/axis/services/SayHello -nsayhello server.SayHello
执行如下命令生成client stub
java org.apache.axis.wsdl.WSDL2Java SayHello.wsdl -p client
生成的stub client文件列表为:
1。SayHello.java
2。SayHelloService.java。
3。SayHelloServiceLocator.java
4。SayHelloSoapBindingStub.java
6.编写客户端程序,编译并执行
public class SayHelloClient {
public static void main(String[] args) {
try {
SayHelloService service = new client.
SayHelloServiceLocator();
client.SayHello_PortType client = service.getSayHello();
String retValue=client.getName("zhangsan");
System.out.println(retValue);
} catch (Exception e) {
System.err.println("Execution failed. Exception: " + e);
}
}
}
首先,要搭建一个环境, 笔者搭建的环境是: Windows2000 Server(中文版)+TOMCAT4
1 从http://jakarta.apache.org/下载TOMCAT4,安装,并且配置好。启动TOMCAT4,在浏览器中输入http://localhost:8080。可以看到首页面,表示TOMCAT4已经配置好了。
2 从http://xml.apache.org/dist/axis/1_1RC1/下载AXIS,解压缩到$TOMCAT_HOME/webapps/axis下面去。
在浏览器中运行http://localhost:8080/axis/index.html ,测试是否可以运行。
接下来,我们开始开发一个Web Service应用。编辑一个java文件
CalcService.java
public class CalcService { public int add(int p1, int p2) {
return p1 + p2; }
public int subtract(int p1, int p2) {
return p1 - p2; } } |
复制CalcService.java文件到$TOMCAT_HOME/webapps/axis目录下,改名为CalcService.jws。在浏览器中,输入http://localhost:8080/axis/CalcService.jws,得到提示信息如下:
Axis把CalcService.jws包装后,提供了一个WSDL的接口外露信息。可以输入http://localhost:8080/axis/CalcService.jws?WSDL 察看WSDL的接口信息。通过WSDL接口,我们可以遍写一个VB程序来调用。
测试步骤如下:
1 从http://www.microsoft.com上下载SOAP TOOLKIT 3.0,安装。
2 VB6中新建一个工程,添加一个FORM,FROM的界面如下。
3 分别在两个按钮cmdAdd和cmdMinus中写上如下代码
Private Sub cmdAdd_Click() '测试add方法 On Error Resume Next Dim SoapClient As Object
Set SoapClient = CreateObject("MSSOAP.SoapClient30") Call SoapClient.MSSoapInit2(Text1.Text, "", "", "CalcService", "") If Err.Number <> 0 Then MsgBox SoapClient.FaultString, vbExclamation Exit Sub End If ' Now the client can call an operation listed in the portType element ' specified when calling mssoapinit(). Text4.Text = CStr(SoapClient.Add(Val(Text2.Text), Val(Text3.Text)))
If Err.Number <> 0 Then MsgBox SoapClient.FaultString, vbExclamation Exit Sub End If End Sub
Private Sub cmdMinus_Click() '测试substract方法 On Error Resume Next Dim SoapClient As Object
Set SoapClient = CreateObject("MSSOAP.SoapClient30") Call SoapClient.MSSoapInit2(Text1.Text, "", "", "CalcService", "") If Err.Number <> 0 Then MsgBox SoapClient.FaultString, vbExclamation Exit Sub End If ' Now the client can call an operation listed in the portType element ' specified when calling mssoapinit(). Text7.Text = CStr(SoapClient.subtract(Val(Text5.Text), Val(Text6.Text)))
If Err.Number <> 0 Then MsgBox SoapClient.FaultString, vbExclamation Exit Sub End If End Sub |
其中需要注意的是,MSSoapInit2方法简单地说明如下:
HRESULT MSSoapInit2(
[in] VARIANT par_WSDLFile,
[in] VARIANT par_WSMLFile,
[in] BSTR par_ServiceName,
[in] BSTR par_Port,
[in] BSTR par_Namespace);
par_WSDLFile WSDL文件的位置,本例中为 http://localhost:8080/axis/CalcService.jws?wsdl par_WSMLFile 本例中为空 par_ServiceName 本例中为空 par_Port 服务端口号,可以从wsdl文件中找到。 par_Namespace 本例中为空 |
关于Microsoft SOAP TOOLKIT 3.0的详细说明,我会以后,专门写文章介绍。
4 运行,并且测试。
总结,应用Web Service技术,我们已经看到了,用VB编写的程序一样可以享用JAVA技术,当然也可以享用.NET技术,当然java编写的应用一样可以享用VB6或.NET开发的WebService。这项技术的应用必将给信息系统的结构模式带来全新的改变。