您的位置:首页 > 其它

基于Servlet发送、接收SOAP消息

2010-03-08 12:45 337 查看
名词:SAAJ : SOAP with Attachments API for Java

1. WebServices在服务端和客户端使用SOAP消息格式来传递数据,SOAP消息的格式如下:

<?xml version="1.0"?>

<soap:Envelope

  xmlns:soap="http://www.w3.org/2001/12/soap-envelope"

  soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">

<soap:Header>

...

</soap:Header>

<soap:Body>

...

<soap:Fault>

...

</soap:Fault>

</soap:Body>

</soap:Envelope>

[附件信息]

2. 下面我们就要使用Servlet来模拟SOAP消息的客户端和服务端。

要求: 客户端将姓名(username),发送给服务端,然后服务端返回(Hello,[username]).

代码说明: SendingServlet.java: SOAP消息的客户端

ReceivingServlet.java: SOAP消息的服务端,继承自

com.sun.xml.messaging.soap.server.SAAJServlet(也是一个Servlet)

SAAJServlet是个抽象的HttpServlet,有一个抽象方法,接收请求的SOAPMessage,并返回响应的SoapMessage

public abstract SOAPMessage onMessage(SOAPMessage reqMsg);

需要jar包: saaj-api.jar、saaj-impl.jar、saaj-coms.jar (SAAJ : SOAP with Attachments API for Java)

Web服务器: Tomcat

3. 开发过程

1> 编写SOAP客户端的Servlet,并在web.xml文件中进行相应配置,开发流程大致如下

// 1.创建消息工厂

// 2.创建soap消息reqMsg

// 3.创建soap消息的部分reqMsgpart

// 3.1.创建sope信封envelope,要开始写信了,哈哈

// 3.2.写header

// 3.3.写body

// 3.4.向body中添加元素,即要传递的数据

// 4.创建reqMsg的附件attachmentpart,

// 5.创建SOAP消息的目标对象(服务端点endPoint),即消息发给谁(发给服务端Servlet的URL),

////// 这里也可以将SOAP消息发送给一个已经存在的WebService,如:

////// URL endPoint = new URL("http://localhost:8080/axis/services/HelloWorld");

// 6.发送SOAP消息,并接收返回信息

2> 编写SOAP服务端的Servlet,并在web.xml文件中进行相应配置,开发流程大致如下。

// 1.接收客户端发送的SOAP请求消息,并提取出信息中的有效数据

// 2.根据请求的数据,返回SOAP消息响应

3> 编写一个jsp页面,调用客户端Servlet请求SOAP消息到服务端

4> SOAP消息请求、响应成功,观察输出的SOAP消息内容。

[SOAP请求消息(SendingServlet.java发送的)]

<SOAP-ENV:Envelope xmlns:SOAP-ENV = "http://schemas.xmlsoap.org/soap/envelope/">

<SOAP-ENV:Header/>

<SOAP-ENV:Body>

<koma:sayHello xmlns:koma = "http://koma.java.cn">

<koma:username>test</koma:username>

</koma:sayHello>

</SOAP-ENV:Body>

</SOAP-ENV:Envelope>

[附件信息(index.jsp文件的内容)]

[SOAP响应消息(ReceivingServlet.java返回的)]

<SOAP-ENV:Envelope xmlns:SOAP-ENV = "http://schemas.xmlsoap.org/soap/envelope/">

<SOAP-ENV:Header/>

<SOAP-ENV:Body>

<ResponseMsg>Hello test!</ResponseMsg>

</SOAP-ENV:Body>

</SOAP-ENV:Envelope>

4. 代码见附件 saaj_sample.rar
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐