java WebService例子.docx
- 文档编号:18281611
- 上传时间:2023-08-15
- 格式:DOCX
- 页数:9
- 大小:17.29KB
java WebService例子.docx
《java WebService例子.docx》由会员分享,可在线阅读,更多相关《java WebService例子.docx(9页珍藏版)》请在冰点文库上搜索。
javaWebService例子
javaWebService例子
2009-01-1417:
2526931人阅读评论(37)收藏举报
一、Axis安装1、环境J2SESDK1.3or1.4:
我使用1.4.2ServletContainer:
我使用的Tomcat5.0
2、到http:
//ws.apache.org/Axis/网站下载Axis安装包
3、解压缩安装包,将Axis_UNZIP_PATH/Axis-version/webapps下的Axis包拷贝到TOMCAT_HOME/webapps/下,以下约定Axis_HOME为该TOMCAT_HOME/webapps/Axis目录
4、启动tomcat,访问http:
//localhost:
8080/Axis检查安装是否成功
5、以上步骤执行成功,可以开发webservice例子了
Axis支持三种webservice的部署和开发,分别为:
1、DynamicInvocationInterface(DII)
2、Stubs方式
3、DynamicProxy方式
二、编写DII(DynamicInvocationInterface)方式web服务
1.编写服务端程序HelloClient
publicclassHelloClient
{
publicStringgetName(Stringname)
{
return"hello"+name;
}
}
2、将源码拷贝到Axis_HOME下,重命名为HelloClient.jws
3、访问连接http:
//localhost:
8080/Axis/HelloClient.jws?
wsdl,页面显示Axis自动生成的wsdl
4、编写访问服务的客户端TestHelloClient.java
importorg.apache.Axis.client.Call;
importorg.apache.Axis.client.Service;
importjavax.xml.namespace.QName;
importjavax.xml.rpc.ServiceException;
import.MalformedURLException;
importjava.rmi.RemoteException;
publicclassSayHelloClient2
{
publicstaticvoidmain(String[]args)
{
try
{
Stringendpoint=
"http:
//localhost:
8080/Axis/HelloClient.jws";
Serviceservice=newService();
Callcall=null;
call=(Call)service.createCall();
call.setOperationName(newQName(
"http:
//localhost:
8080/Axis/HelloClient.jws",
"getName"));
call.setTargetEndpointAddress
(new.URL(endpoint));
Stringret=(String)call.invoke(newObject[]
{"zhangsan"});
System.out.println("returnvalueis"+ret);
}
catch(Exceptionex)
{
ex.printStackTrace();
}
}
}
三、编写DynamicProxy方式访问服务
1、编写部署服务端程序,同上边DII方式,本次仍使用上边部署的HelloClient
2、编写代理接口
publicinterfaceHelloClientInterface
extendsjava.rmi.Remote
{
publicStringgetName(Stringname)
throwsjava.rmi.RemoteException;
}
3、编写并执行客户端程序TestHelloClient.java
importjavax.xml.rpc.Service;
importjavax.xml.rpc.ServiceFactory;
import.URL;
importjavax.xml.namespace.QName;
publicclassTestHelloClient
{
publicstaticvoidmain(String[]args)
{
try
{
StringwsdlUrl=
"http:
//localhost:
8080/Axis/HelloClient.jws?
wsdl";
StringnameSpaceUri=
"http:
//localhost:
8080/Axis/HelloClient.jws";
StringserviceName="HelloClientService";
StringportName="HelloClient";
ServiceFactoryserviceFactory=
ServiceFactory.newInstance();
ServiceafService=
serviceFactory.createService(newURL(wsdlUrl),
newQName(nameSpaceUri,serviceName));
HelloClientInterfaceproxy=(HelloClientInterface)
afService.getPort(newQName(
nameSpaceUri,portName),
HelloClientInterface.class);
System.out.println
("returnvalueis"+proxy.getName("john"));
}catch(Exceptionex)
{
ex.printStackTrace();
}
}
}
四、编写wsdd发布web服务,编写stubclient访问web服务
1、编写服务端程序server,SayHello.java,编译server.SayHello.java
packageserver;
publicclassSayHello
{
publicStringgetName(Stringname)
{
return"hello"+name;
}
}
2.编写LogHandler.java
importorg.apache.Axis.AxisFault;
importorg.apache.Axis.Handler;
importorg.apache.Axis.MessageContext;
importorg.apache.Axis.handlers.BasicHandler;
importjava.util.Date;
publicclassLogHandler
extendsBasicHandler
{
publicvoidinvoke
(MessageContextmsgContext)
throwsAxisFault
{
/**Loganaccesseachtime
wegetinvoked.
*/
try{
HandlerserviceHandler
=msgContext.getService();
IntegernumAccesses=
(Integer)serviceHandler.getOption("accesses");
if(numAccesses==null)
numAccesses=newInteger(0);
numAccesses=newInteger
(numAccesses.intValue()+1);
Datedate=newDate();
Stringresult=
date+":
service"+
msgContext.getTargetService()+
"accessed"+numAccesses+"time(s).";
serviceHandler.setOption
("accesses",numAccesses);
System.out.println(result);
}catch(Exceptione)
{
throwAxisFault.makeFault(e);
}
}
}
3、编写wsdd文件
deploy.wsdd
"http: //xml.apache.org/Axis/wsdd/" xmlns: java= "http: //xml.apache.org/Axis/wsdd/providers/java"> LogHandler"/> provider="java: RPC"> value="server.SayHello"/> value="*"/> 3、将编译后的文件拷贝到Axis_HOME/WEB-INF/classes下,如: D: /tomcat/webapps/Axis/WEB-INF/classes 4、发布服务: javaorg.apache.Axis.client.AdminClientdeploy.wsdd 5、生成clientstub文件 a: 方式1 将SayHello.java拷贝到Axis_HOME/下,重命名为SayHello.jws, 执行下面的命令生存clientstub javaorg.apache.Axis.wsdl.WSDL2Java -pclient http: //localhost: 8080 /Axis/services/SayHello.jws? wsdl b: 方式2 执行如下命令生成SayHello.wsdl javaorg.apache.Axis.wsdl.Java2WSDL -oSayHello.wsdl-lhttp: //localhost: 8080 /Axis/services/SayHello-nsayhelloserver.SayHello 执行如下命令生成clientstub javaorg.apache.Axis.wsdl.WSDL2Java SayHello.wsdl -pclient 生成的stubclient文件列表为: 1.SayHello.java 2.SayHelloService.java。 3.SayHelloServiceLocator.java 4.SayHelloSoapBindingStub.java 6、编写客户端程序,编译并执行 publicclassSayHelloClient { publicstaticvoidmain(String[]args) { try { SayHelloServiceservice=newclient. SayHelloServiceLocator(); client.SayHello_PortType client=service.getSayHello(); StringretValue=client.getName("zhangsan"); System.out.println(retValue); } catch(Exceptione) { System.err.println ("Executionfailed.Exception: "+e); } } } 您也可以写server-config.wsdd xmlversion="1.0"encoding="UTF-8"? > //xml.apache.org/axis/wsdd/" xmlns: java="http: //xml.apache.org/axis/wsdd/providers/java"> value="org.apache.axis.attachments.AttachmentsImpl"/> org.apache.axis.handlers.JWSHandler"> org.apache.axis.handlers.JWSHandler"> org.apache.axis.transport.local.LocalResponder"/> org.apache.axis.handlers.http.URLMapper"/> org.apache.axis.handlers.SimpleAuthenticationHandler"/> stub.LogHandler"/> RPC"> org.apache.axis.handlers.http.HTTPAuthHandler"/> 这个更好用些
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- java WebService例子 WebService 例子