zoukankan      html  css  js  c++  java
  • 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,并显示返回的结果。

  • 相关阅读:
    vim编辑参数
    hive的元数据存储在mysql后,报错的解决方法!
    hadoop添加删除节点
    Android画图Path的使用
    android中path的arcTo方法的使用 .
    StrongReference、SoftReference、WeakReference、PhantomReference .
    为Android应用增加渠道信息 自动化不同渠道的打包过程
    C#知识点
    CSS判断浏览器
    调用外部程序
  • 原文地址:https://www.cnblogs.com/king1302217/p/2045191.html
Copyright © 2011-2022 走看看