创立JAVA WebService步骤(4)

四、编写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(s).";
serviceHandler.setOption
("accesses", numAccesses);
System.out.println(result);
          } catch (Exception e)
                  {
              throw AxisFault.makeFault(e);
          }
      }
}

3、编写wsdd文件

deploy.wsdd
<deployment xmlns=
"http://xml.apache.org/Axis/wsdd/"
    xmlns:java=
                          "http://xml.apache.org/Axis/wsdd/providers/java">          
     <handler type="java:LogHandler"/>
<service
provider="java:RPC">
     <requestFlow>
       <handler type="print"/>
     </requestFlow>
    <parameter
    value="server.SayHello"/>
    <parameter
    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    :8080
/Axis/services/SayHello.jws?wsdl

b:方式2

执行如下命令生成SayHello.wsdl

java org.apache.Axis.wsdl.Java2WSDL
-oSayHello.wsdl -l: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);
          }
      }
}

您也可以写server-config.wsdd
<?xml version="1.0" encoding="UTF-8"?>
<deployment xmlns="http://xml.apache.org/axis/wsdd/"
          xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
<globalConfiguration> 

<parameter value="admin"/> 
    <parameter
            value="org.apache.axis.attachments.AttachmentsImpl"/> 
    <parameter value="true"/> 
    <parameter value="true"/> 
    <parameter value="true"/> 
    <parameter value="true"/>
  
    <requestFlow>  
     <handler type="java:org.apache.axis.handlers.JWSHandler">   
      <parameter value="session"/>  
     </handler>  
  
     <handler type="java:org.apache.axis.handlers.JWSHandler">   
      <parameter value="request"/>   
      <parameter value=".jwr"/>  
     </handler> 
  
    </requestFlow>
 
</globalConfiguration>
<handler type="java:org.apache.axis.transport.local.LocalResponder"/>
<handler type="java:org.apache.axis.handlers.http.URLMapper"/>
<handler type="java:org.apache.axis.handlers.SimpleAuthenticationHandler"/>  
    <handler type="java:stub.LogHandler"/>
 
    <service provider="java:RPC">
     <requestFlow>
       <handler type="print"/>
     </requestFlow>
    <parameter     value="stub.server.SayHello"/> 
    <parameter    value="*"/> 
</service>

<transport> 
    <requestFlow>  
     <handler type="URLMapper"/>  
     <handler type="java:org.apache.axis.handlers.http.HTTPAuthHandler"/> 
    </requestFlow>
</transport>
<transport> 
    <responseFlow>  
     <handler type="LocalResponder"/> 
    </responseFlow>
</transport>
</deployment>

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwpffj.html