java axis发布web service(三) 调用web service

三. Web Service的客户端使用

   调用Web Service的方式有三种:

        1.Dynamic Invocation Interface(DII):http://blog.csdn.net/Brookes/archive/2007/03/23/1539021.aspx

        2.Dynamic Proxy 动态代理:http://blog.csdn.net/Brookes/archive/2007/03/23/1538400.aspx

        3.Stubs 桩:http://blog.csdn.net/Brookes/archive/2007/03/23/1538532.aspx

 

下面我们采用第一种方式:

       1.首先建立一个java web程序:WebServiceSample

       2.在工程WebServiceSample的src目录下建立一个包:mypack

       3.在mypack下建立一个class,命名为:TestWS,编写代码如下

package  mypack;

import  javax.xml.namespace.QName;
import  org.apache.axis.client.Call;
import  org.apache.axis.client.Service;

public   class  TestWS {
    
public  String getWelcome(String name) 
    {
        
try
        {
            String endpoint 
=    " http://localhost:8080/axis/HelloWorld.jws " ;
            Service service 
=   new  Service();            
            Call call 
=   null ;    
            call 
=  (Call)service.createCall();            
            call.setOperationName(
new  QName(  " http://localhost:8080/axis/HelloWorld.jws " , " sayHello " ));            
            call.setTargetEndpointAddress(
new  java.net.URL(endpoint));            
            String response 
=  (String) call.invoke( new  Object[]{name});
            
return  response;
        }
        
catch  (Exception ex)
        { 
            System.out.println(ex);
            
return   null ;
        }
       
    }
}

     4.修改WebServiceSample的index.jsp代码

<% @ page language = " java "  import = " java.util.* "  pageEncoding = " GB2312 " %>
<% @ page import = " mypack.* "   %>


<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >
< html >
  
< head >     
    
< title > My JSP 'index.jsp' starting page </ title >
    
< meta  http-equiv ="pragma"  content ="no-cache" >
    
< meta  http-equiv ="cache-control"  content ="no-cache" >
    
< meta  http-equiv ="expires"  content ="0" >     
    
< meta  http-equiv ="keywords"  content ="keyword1,keyword2,keyword3" >
    
< meta  http-equiv ="description"  content ="This is my page" >
    
<!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    
-->
  
</ head >
  
  
< body >  
      
<%  
          TestWS Tws 
=   new  TestWS();
          
String  r  =  ( String )Tws.getName( " feifei " );
          System.out.println(r);
      
%>
    
< br > This is my JSP page.  <% = r %>   < br >
  
</ body >
</ html >

 

      5.在浏览器中输入http://localhost:8080/WebServiceSample/

            会显示This is my JSP page. Hello,feifei

 

    例子通过TestWS.java来连接HelloWorld的webservice,前台jsp发送请求参数给TestWS.java,并显示返回的结果。

你可能感兴趣的:(web Service)