一个使用WSAD开发Web services的例子(以JAVA为例)

我们需要完成销售员查询库存的工作,但是不幸的是,销售系统和库存系统是两个相对独立的系统,如何在他们之间建立连接呢?这就要用到我们前面提到的Web services,下面介绍如何用WSAD实现Web services 。

 

建一个web项目,命名为“StoreSystem”,选择MyFactoryEAR作为企业应用程序的项目名,点击下一步,在StoreEJB.jar前面打勾,点击下一步,选择“项目”选项卡,在“StoreEJB”前面打勾。点击完成按钮。
选中导航器中StoreSystem->source,在右键菜单中选择“新建”->其他。
在弹出的对话框中选择“Java”里面的“Java 包”。点击下一步,填写包为“com.taiji.bean”,单击“完成”。

 

    还是选中导航器中StoreSystem->source,在右键菜单中选择“新建”->其他。
在弹出的对话框中选择“Java”里面的 “Java类”。点击下一步,点包后面的“浏览按钮”,选择“com.taiji.bean”,填写名称为“StoreEJBClient”,点击“完成按钮”。

    打开“导航器”下“StoreSystem”->“source”->“com”->“taiji”->“bean”下的StoreEJBClient。修改代码为:

  
  
package com.taiji.bean; import javax.ejb.* ; import javax.rmi.* ; import com.taiji.ejb.* ; import javax.naming.* ; import org.w3c.dom.* ; import javax.xml.parsers.* ; public class StoreEJBClient { private DocumentBuilder builder = null ; private StorageHome storageHome = null ; private DocumentBuilder getDocumentBuilder() { if (builder == null ) { DocumentBuilderFactory factory = new org.apache.xerces.jaxp.DocumentBuilderFactoryImpl(); try { builder = factory.newDocumentBuilder(); } catch (ParserConfigurationException pce) { pce.printStackTrace(); } } return builder; } private StorageHome getStroageHome(){ if(storageHome==null ){ try { InitialContext ic =new InitialContext(); Object objref =ic.lookup("ejb/com/taiji/ejb/StorageHome" ); storageHome = (StorageHome)PortableRemoteObject.narrow (objref,StorageHome.class ); }catch (NamingException nx){ nx.printStackTrace(); } } return storageHome; } private Storage getStorage(String productid){ Storage storage = null ; try { storage = getStroageHome().findByPrimaryKey(new StorageKey(productid)); }catch (Exception e){ e.printStackTrace(); } return storage; } private Element newElement (Document doc,String name,String value){ Element element = doc.createElement(name); element.appendChild(doc.createTextNode(value)); return element; } private Element populate(Document doc,String productid) throws org.w3c.dom.DOMException { Element myStorage =doc.createElement("StorageResult" ); try { myStorage.appendChild(newElement(doc,"productid" ,productid)); myStorage.appendChild(newElement(doc,"quantity" ,String.valueOf (getStorage(productid).getQuantity()))); myStorage.appendChild(newElement(doc,"productTime" ,getStorage (productid).getProductTime().toString())); }catch (java.rmi.RemoteException e){ myStorage.appendChild(newElement(doc,"error" ,e.getMessage())); } return myStorage; } public Element getResult(String productid){ Element result =null ; Document doc = getDocumentBuilder().newDocument(); result = populate(doc,productid); return result; } }

    然后保存。在导航器中选中“StoreSystem”,在右键菜单中选择“特性”,在弹出的对话框中选择“Java构建路径”,在右边选择“库”选项卡,点击“添加变量”。在弹出的对话框中点击“浏览”按钮。选中“XERCES”,然后点击确定。

    在WSAD中,有允许用户定义类路径的变量,这样,只要添加一个变量,就可以实现添加一组jar的效果。同时自己看着也比较舒服,呵呵。

查看一下Web透视图下的任务栏,看看是不是还有错误。如果没有,我们继续。

对Web services 开发而言, WSAD 提供了下列智能化向导:
Web service wizard
Web service client wizard
Web service skeleton JavaBean wizard
Web service DADX group configuration wizard

我们的Web services 的开发就是基于上述的一些向导完成的。具体的步骤如下:

    右键选中“导航器”下“StoreSystem”->“source”->“com”->“taiji”->“bean”下的“StoreEJBClient”,在弹出的菜单中选择“新建”->“Web 服务”。确认在Web服务类型上选择的是“Java bean Web 服务”,Web项目选择的是“StoreSystem”,接受其他默认选项(启动Web项目中的Web Services是,在建立Web服务向导后启动服务器,发布新的Web服务,其实要简单一点的话,我想只要选中“生成代理”和生成代理中的“生成样本”还有“必要时创建文件夹”就可以直接完成了,不过我还是建议大家按向导一步一步来做,会理解的多一些)。

点击下一步。
确认Bean下填写的是“com.taiji.bean.StoreEJBClient”。



点击下一步。
接受默认选项。

点击下一步。
确认在上面的“org.w3c.dom.Element getResult(java.lang.String productid)”,接受其他默认选项。



点下一步。
接受默认选项。



点下一步。
接受默认选项。



点下一步。
在“生成样本”的前面打勾。


点下一步。
点击完成。

   现在,WSAD要发布Web服务,可能时间会少长一点,当服务器启动完毕以后,我们要进行测试。
你可以看到,在导航器的StoreSystem项目中多了一些目录和文件。我简单介绍一下:

   完全扩展source下的proxy目录下,我们会看到一个叫StoreEJBClientProxy.java的文件,它是WSAD自动生成的来测试Web服务的。在工作区中打开它,你可以了解SOAP的RPC调用是在java程序中如何实现。
在StoreSystem下的webApplication目录下,多了两个xml文件,dds.xml记录在服务器上部署的Web服务,soap.xml记录SOAP Server 的 configure。
展开wsdl目录,有两个wsdl文件和一个xsd文件。
StoreEJBClient-binding.wsdl是服务接口文件,用于具体描述某种特定类型的服务。
StoreEJBClient-service.wsdl是服务实现文件,至少包含一个服务接口的引用。

   服务接口文档由服务接口提供者开发和发布。服务实现文档由服务提供者创建和发布。服务接口提供者与服务提供者这两个角色在逻辑上是分离的,但他们可以是同一个商业实体。(看看上面的这段话,这种传授方式,一般人很难理解,我也衷心的希望,在提出一个新的技术的时候,能够用最通俗的话把它表达主来,以动手为主,而不是一味的炒概念。)简单的说服务接口文档是直接描述了一个Web services调用的规则,服务实现文档至少包含一个服务接口文档,它主要是用于在UDDI上发布。

StoreEJBClient-schema.xsd是定义一个复杂数据类型的schema。

  现在完全扩展webApplication下的sample目录。你可以看到4个jsp文件,这是WSAD为你生成的WEB测试的客户端。右键点击TestClient.jsp,选择“在服务器上运行”。在左面的页面中点击getResult,在右上方页面上填写C1005,点“Invoke”按钮。查看右下页面中的结果。




    复制StoreEJBClientProxy.java文件到SellSystem项目下的source目录:右键点击StoreSystem项目中的StoreEJBClientProxy.java,在弹出的菜单种选择“复制”,在文件夹选择对话框中选中SellSystem项目中的source目录。单击“确定”。
修改SellSystem->source下的StoreEJBClientProxy.java: 去掉第一行“package proxy.soap.com.taiji.bean;”并保存。
右键单击SellSystem项目,在弹出的菜单中选择“特性”,在“Java构建路径”的“库”选项卡中添加变量“XERCES”和“SOAPJAR”。查看任务栏中是否有还错误出现。

  <%
  String productid = null;
  int quantity = 0;
  %>
  在“<jsp:useBean id="detailsViewDBBean" class="CheckOrderDetailsViewBean"
  scope="request" />”后面加入下列代码:
  <jsp:useBean id="StoreEJBClientid" scope="session" class="StoreEJBClientProxy"
  />
        
在“<%=detailsViewDBBean.getPRODUCTID()%> ”下面插入代码:

<%productid = detailsViewDBBean.getPRODUCTID();%> 
        
在“<%=detailsViewDBBean.getQUANTITY()%> ”下面插入代码:

<%quantity = detailsViewDBBean.getQUANTITY();%>  
        
在“</FORM>”和“</BODY>”之间插入下列代码:

  
  
<%! public java.lang.String domWriter (org.w3c.dom.Node node,java.lang.StringBuffer buffer,int quantity) { if ( node == null ) { return "" ; } int type = node.getNodeType(); switch ( type ) { case org.w3c.dom.Node.ELEMENT_NODE: { org.w3c.dom.NodeList children = node.getChildNodes(); int len = children.getLength(); if(node.getNodeName().equals("StorageResult" )){ buffer.append("<table border=1>"); buffer.append("<tr>" ); buffer.append("<td colspan=2>库存状况</td>" ); buffer.append("</tr>" ); if ( children != null ) { for ( int i = 0; i < len; i++ ) { domWriter(children.item(i),buffer,quantity); } } buffer.append("</table>" ); break ; } if(node.getNodeName().equals("productid" )){ buffer.append("<tr>" ); buffer.append("<td>" ); buffer.append("产品号" ); buffer.append("</td>" ); for ( int i = 0; i < len; i++ ) { buffer.append("<td>" ); buffer.append(children.item(i).getNodeValue()); buffer.append("</td>" ); break ; } buffer.append("</tr>" ); } if(node.getNodeName().equals("quantity" )){ buffer.append("<tr>" ); buffer.append("<td>" ); buffer.append("库存数量" ); buffer.append("</td>" ); for ( int i = 0; i < len; i++ ) { buffer.append("<td>" ); String strStorequantity = children.item(i).getNodeValue(); int storequantity = Integer.parseInt(strStorequantity); if(storequantity < quantity){ buffer.append("<font color = red>" ); buffer.append(strStorequantity); buffer.append("</font>" ); }else { buffer.append(strStorequantity); } buffer.append("</td>" ); break ; } buffer.append("</tr>" ); } if(node.getNodeName().equals("productTime" )){ buffer.append("<tr>" ); buffer.append("<td>" ); buffer.append("生产日期" ); buffer.append("</td>" ); for ( int i = 0; i < len; i++ ) { buffer.append("<td>" ); buffer.append(formatTime(children.item(i).getNodeValue())); buffer.append("</td>" ); break ; } buffer.append("</tr>" ); } break ; } } return buffer.toString(); } %> <%! public String formatTime(String strTime){ try { java.text.SimpleDateFormat dateformat = new java.text.SimpleDateFormat(); java.util.Date d = dateformat.parse(strTime); dateformat.applyPattern("yyyy.MM.dd '-' hh:mm:ss" ); vTime = dateformat.format(d); }catch (java.text.ParseException e){ vTime = "不是合法时间" ; } return vTime; } %> <% org.w3c.dom.Element mtemp = StoreEJBClientid.getResult(productid); String tempResultresult = domWriter(mtemp, new java.lang.StringBuffer(),quantity); %> <%=tempResultresult%>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值