Java实现SOAP协议 之 HelloWorld
Java实现SOAP协议 之 HelloWorld
SOAP是把基于HTTP的WEB技术与XML的灵活性和可扩展性组合在了一起。
准备:
1. 准备一个web server。Tomcat 下载地址 http://tomcat.apache.org/
本例使用版本 apache-tomcat-7.0.54-windows-x64.zip
2. 写一个servlet send用来发送一个SOAP的message并添加一个附件。
3. 写一个servlet receive用来做SOAP message的解析并返回一个response。
4. 写一个简单的html页面触发 servlet A。
开发工具:
eclipse 版本 eclipse-jee-juno-SR2-win32-x86_64
下载地址:
http://www.eclipse.org/downloads/
下载J2EE版本的eclipse里面包含了SOAP常用的一些jar,但这个版本的没有jaxm-api.jar,这个需要额外下载
然后倒入。
代码结构:
web.xml代码如下:
- <?xml version="1.0" encoding="ISO-8859-1"?>
- <!DOCTYPE web-app
- PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
- "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
- <web-app>
- <servlet>
- <servlet-name>DemoSend</servlet-name>
- <servlet-class>cody.soap.demo.DemoSend</servlet-class>
- </servlet>
- <servlet>
- <servlet-name>DemoReceiver</servlet-name>
- <servlet-class>cody.soap.demo.DemoReceiver</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>DemoSend</servlet-name>
- <url-pattern>/DemoSend</url-pattern>
- </servlet-mapping>
- <servlet-mapping>
- <servlet-name>DemoReceiver</servlet-name>
- <url-pattern>/DemoReceiver</url-pattern>
- </servlet-mapping>
- </web-app>
DemoSend.java
- package cody.soap.demo;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.OutputStream;
- import java.net.URL;
- import javax.activation.DataHandler;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import javax.xml.soap.AttachmentPart;
- import javax.xml.soap.MessageFactory;
- import javax.xml.soap.SOAPBody;
- import javax.xml.soap.SOAPConnection;
- import javax.xml.soap.SOAPConnectionFactory;
- import javax.xml.soap.SOAPEnvelope;
- import javax.xml.soap.SOAPException;
- import javax.xml.soap.SOAPHeader;
- import javax.xml.soap.SOAPMessage;
- import javax.xml.soap.SOAPPart;
- public class DemoSend extends HttpServlet {
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- private SOAPConnection connection;
- @Override
- public void init() throws ServletException {
- super.init();
- try {
- SOAPConnectionFactory connectionFactory = SOAPConnectionFactory.newInstance();
- connection = connectionFactory.createConnection();
- } catch (UnsupportedOperationException e) {
- e.printStackTrace();
- } catch (SOAPException e) {
- e.printStackTrace();
- }
- }
- @Override
- protected void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- String outString ="<HTML><H1>Sending and reading the SOAP Message</H1><P>";
- try {
- MessageFactory messageFactory = MessageFactory.newInstance();
- SOAPMessage outgoingMessage = messageFactory.createMessage();
- SOAPPart soappart = outgoingMessage.getSOAPPart();
- SOAPEnvelope envelope = soappart.getEnvelope();
- SOAPHeader header = envelope.getHeader();
- SOAPBody body = envelope.getBody();
- body.addBodyElement(envelope.createName("numberAvailable", "laptops", "http://ecodl.taobao.com/")).addTextNode("216");
- StringBuffer serverUrl = new StringBuffer();
- serverUrl.append(request.getScheme()).append("://").append(request.getServerName());
- serverUrl.append(":").append(request.getServerPort()).append(request.getContextPath());
- String baseUrl = serverUrl.toString();
- URL url = new URL(baseUrl + "/test.html");
- AttachmentPart attachmentpart = outgoingMessage.createAttachmentPart(new DataHandler(url));
- attachmentpart.setContentType("text/html");
- outgoingMessage.addAttachmentPart(attachmentpart);
- URL client = new URL(baseUrl + "/DemoReceiver");
- FileOutputStream outgoingFile = new FileOutputStream("out.msg");
- outgoingMessage.writeTo(outgoingFile);
- outgoingFile.close();
- outString += "SOAP outgoingMessage sent (see out.msg). <BR>"+baseUrl+"<BR>";
- SOAPMessage incomingMessage = connection.call(outgoingMessage, client);
- if (incomingMessage != null) {
- FileOutputStream incomingFile = new FileOutputStream("in.msg");
- incomingMessage.writeTo(incomingFile);
- incomingFile.close();
- outString += "SOAP outgoingMessage received (see in.msg).</HTML>";
- }
- } catch (SOAPException e) {
- e.printStackTrace();
- }
- try {
- OutputStream outputStream = response.getOutputStream();
- outputStream.write(outString.getBytes());
- outputStream.flush();
- outputStream.close();
- } catch (IOException e) {}
- }
- @Override
- protected void doPost(HttpServletRequest req, HttpServletResponse resp)
- throws ServletException, IOException {
- doGet(req, resp);
- }
- }
DemoReceiver.java
- package cody.soap.demo;
- import java.util.Iterator;
- import javax.servlet.ServletConfig;
- import javax.servlet.ServletException;
- import javax.xml.messaging.JAXMServlet;
- import javax.xml.messaging.ReqRespListener;
- import javax.xml.soap.MessageFactory;
- import javax.xml.soap.SOAPBody;
- import javax.xml.soap.SOAPElement;
- import javax.xml.soap.SOAPEnvelope;
- import javax.xml.soap.SOAPException;
- import javax.xml.soap.SOAPMessage;
- import javax.xml.soap.SOAPPart;
- public class DemoReceiver extends JAXMServlet implements ReqRespListener{
- /**
- * Generated automatically
- */
- private static final long serialVersionUID = 1L;
- private static MessageFactory messageFactory = null;
- @Override
- public void init(ServletConfig servletConfig) throws ServletException {
- super.init(servletConfig);
- try {
- messageFactory = MessageFactory.newInstance();
- } catch (SOAPException e) {
- e.printStackTrace();
- }
- }
- @Override
- public SOAPMessage onMessage(SOAPMessage msg) {
- SOAPPart soappart = msg.getSOAPPart();
- try {
- SOAPEnvelope incomingEnvelope;
- incomingEnvelope = soappart.getEnvelope();
- SOAPBody body = incomingEnvelope.getBody();
- Iterator<?> iterator = body.getChildElements(incomingEnvelope.createName("numberAvailable", "laptops", "http://ecodl.taobao.com/"));
- SOAPElement element;
- element = (SOAPElement) iterator.next();
- SOAPMessage message = messageFactory.createMessage();
- SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
- SOAPBody responsebody = envelope.getBody();
- String responseText = "Got the SOAP message indicating there are " + element.getValue() + " laptops available.";
- responsebody.addChildElement(envelope.createName("Response")).addTextNode(responseText);
- return message;
- } catch (SOAPException e) {
- e.printStackTrace();
- return null;
- }
- }
- }
test.xml
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <HTML>
- <HEAD>
- <TITLE>SOAP and Java</TITLE>
- </HEAD>
- <BODY>
- <H1>SOAP and Java</H1>
- Click <A HREF="DemoSend">here</a> to send the SOAP message.
- </BODY>
- </HTML>
out.msg
- ------=_Part_0_1985246804.1403343570084
- Content-Type: text/xml; charset=utf-8
- <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
- <SOAP-ENV:Header/>
- <SOAP-ENV:Body>
- <laptops:numberAvailable xmlns:laptops="http://ecodl.taobao.com/">216</laptops:numberAvailable>
- </SOAP-ENV:Body>
- </SOAP-ENV:Envelope>
- ------=_Part_0_1985246804.1403343570084
- Content-Type: text/html
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <HTML>
- <HEAD>
- <TITLE>SOAP and Java</TITLE>
- </HEAD>
- <BODY>
- <H1>SOAP and Java</H1>
- Click <A HREF="DemoSend">here</a> to send the SOAP message.
- </BODY>
- </HTML>
- ------=_Part_0_1985246804.1403343570084--
in.msg
- <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
- <SOAP-ENV:Header/>
- <SOAP-ENV:Body>
- <Response>Got the SOAP message indicating there are 216 laptops available.
- </Response>
- </SOAP-ENV:Body>
- </SOAP-ENV:Envelope>
原文:http://www.informit.com/library/content.aspx?b=STY_XML_21days&seqNum=228
在Tomcat上部署的目录结构:
webapps [This is a directory] |____soapdemoserver [This is a directory] |____test.html [Our starting Web page] |____WEB-INF [This is a directory] |____web.xml [Configures Tomcat] |____classes [This is a directory] |____cody/soap/demo [This is a directory] |____DemoReceiver.class [The server servlet] |____DemoSend.class [The client servlet]
Java实现SOAP协议 之 HelloWorld的更多相关文章
- 使用CXF实现基于Soap协议的WebService
本文介绍使用CXF实现基于Soap协议的WebService(CXF的版本是3.0.0) 一. 前言 Java有三种WebService规范:Jax-WS,Jax-RS,Jaxm 1. Jax-WS( ...
- C#如何使用Soap协议调用WebService?
WebService是什么?它的作用? WebService是一个平台独立.低耦合的.自包含的.基于可编程的可使用xml描述.调用的web应用程序,用于开发分布式的交互式的应用程序. Soap是什么? ...
- [Java] webservice soap,wsdl 例子
java 调用webservice的各种方法总结 现在webservice加xml技术已经逐渐成熟,但要真正要用起来还需时日!! 由于毕业设计缘故,我看了很多关于webservice方面的知识,今天和 ...
- 09_httpclient测试SOAP协议
[工程截图]注意:无需使用Wsimport生成客户端代码 [HttpClient.java] import java.io.ByteArrayOutputStream; import java.io. ...
- webservice第二篇【自定义webservice服务、soa、uddi概念、soap协议】
自定义webservice服务 我们在上一章节中已经使用wsimport生成本地代理来调用webservice的服务了,其实我们自己写的web应用程序也是可以发布webservice的 我们发布了we ...
- SOAP协议初级指南 (一)
SOAP(Simple Object Access Protocal) 技术有助于实现大量异构程序和平台之间的互操作性,从而使存在的应用能够被广泛的用户所访问.SOAP是把成熟的基于HTTP的WEB技 ...
- Android与服务器端数据交互(基于SOAP协议整合android+webservice)
http://www.cnblogs.com/zhangdongzi/archive/2011/04/19/2020688.html 上一节中我们通过http协议,采用HttpClient向服务器端a ...
- soap协议有get方式
今天去面试,被问到了这个问题.一时没想起来.考官的说法是没有 get,使用post 发请求.restful 有get put delete等谓词. 特地查了一下.记在下边. <system.we ...
- 08_使用TCP/IP Monitor监视SOAP协议
[SOAP定义] SOAP 简单对象访问协议,基于http传输xml数据,soap协议体是xml格式.SOAP 是一种网络通信协议SOAP 即Simple Object Access Pr ...
随机推荐
- Oracle sqlplus 语法
目录: 0. FREFACE 1. 执行一个SQL脚本文件 2. 对当前的输入进行编辑 3. 重新运行上一次运行的sql语句 4. 将显示的内容输出到指定文件 5. 关闭spool输出 6.显示一个表 ...
- iOS开发之iPhone通过get和post方式请求asp.net webservice
.创建一个webservice .在webconfig中启用http get 和http post. 复制代码 <</span> webServices > <</ ...
- ICMP报文分析
一.概述: 1. ICMP同意主机或路由报告差错情况和提供有关异常情况.ICMP是因特网的标准协议,但ICMP不是高层协议,而是IP层的协议.通常ICMP报文被IP层或更高层协议(TCP或UDP) ...
- TinyXml高速入门(一)
作者:朱金灿 来源:http://blog.csdn.net/clever101 对于xml文件,眼下我的工作仅仅是集中在配置文件和作为简单的信息文件来用,因此我不太喜欢使用msxml这样的重量级的x ...
- soundPool和audiofocus
audiofocus试验: 使用soundPool来写一个播放音频的porject. 资源初始化: setContentView(R.layout.activity_main); Button bt1 ...
- 子查询in和表连接效率
在数据查询时,尽量减少in子查询而使用表连接的方式进行,效率更高.
- angular指令浅谈
今天在闲暇时间再次对angularjs的指令进行了初探,不探不知道一探吓一跳啊, 就一个简单的指令整整难住我了两个小时,先不说代码的逻辑是否复杂,就一些内部的一些实现让我看起来都是头疼的不行啊,不过最 ...
- Dev GridControl 按条件合并相同单元格
Dev 默认的合并方式,只要(垂直方向)相邻两个单元格的值相同都会进行合并,这种方式并不是最优的,所以需要在进行合并的过程中进行判断. 方式如下: 1:先设置需要合并的列为允许合并 OptionsVi ...
- 项目中用到的input 遇到的问题的归类
input 前几天 为了这个词 用在搜索框被我们总监喷,为了加强印象,我把它记录下来 最原始的造型 <input type="text" value="搜索&quo ...
- php代码结尾不要添加结尾标记
如果文件内容是纯 PHP 代码,最好在文件末尾删除 PHP 结束标记.这可以避免在 PHP 结束标记之后万一意外加入了空格或者换行符,会导致 PHP 开始输出这些空白,而脚本中此时并无输出的意图.