danchaofan

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  14 Posts :: 20 Stories :: 3 Comments :: 0 Trackbacks

#

本人通过实际项目开发,叙述关于测试的使用和感悟,本人坚持用代码说话的方式,来讲述测试在项目中的真正实际应用,同时也希望大家多多赐教,共同探讨,总结更适用的开发经验,共同分享!
文章来源:http://dev.csdn.net/author/fuwei2241/cb5b26ad75fa41829d967fbefef3685f.html
posted @ 2007-03-02 18:30 单炒饭 阅读(87) | 评论 (0)编辑 收藏

jdk为1。3版本,没有replaceAll()方法

public class Trans {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub

 }
 /**
  *
  * @param sourceString
  * @param toReplaceString
  * @param replaceString
  * @return
  */
 public static String transChar(String sourceString, String toReplaceString,
   String replaceString) {
  String returnString = sourceString;
  int stringLength = 0;
  if (toReplaceString != null) {
   stringLength = toReplaceString.length();
  }

  if (returnString != null && returnString.length() > stringLength) {
   int max = 0;
   String S4 = "";
   //int i = sourceString.length();

   for (int i = 0; i < sourceString.length(); i++) {

    max = i + toReplaceString.length() > sourceString.length() ? sourceString
      .length()
      : i + stringLength;
    String S3 = sourceString.substring(i, max);
    if (!S3.equals(toReplaceString)) {
     S4 += S3.substring(0, 1);
    } else {
     S4 += replaceString;
     i += stringLength - 1;
    }
   }
   returnString = S4;
  }
  return returnString;

 }

}

posted @ 2006-02-27 17:30 单炒饭 阅读(319) | 评论 (2)编辑 收藏

调用webservice,可以首先根据wsdl文件生成客户端,或者直接根据地址调用,下面讨论直接调用地址的两种不同方式:axis和Soap,soap方式主要是用在websphere下

axis方式调用:

 

import java.util.Date;

import java.text.DateFormat;

import org.apache.axis.client.Call;

import org.apache.axis.client.Service;

import javax.xml.namespace.QName;

import java.lang.Integer;

import javax.xml.rpc.ParameterMode;

 

public class caClient {

            

       public static void main(String[] args) {

 

              try {

                     String endpoint = "http://localhost:8080/ca3/services/caSynrochnized?wsdl";

                     Service service = new Service();

                     Call call = (Call) service.createCall();

                     call.setTargetEndpointAddress(endpoint);

                     call.setOperationName("addUser");

                     call.addParameter("userName", org.apache.axis.encoding.XMLType.XSD_DATE,

                                   javax.xml.rpc.ParameterMode.IN);

                     call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);

                     call.setUseSOAPAction(true);

                     call.setSOAPActionURI("http://www.my.com/Rpc");

                     //Integer k = (Integer) call.invoke(new Object[] { i, j });

                     //System.out.println("result is  " + k.toString() + ".");

                     String temp = "测试人员";

                     String result = (String)call.invoke(new Object[]{temp});

                     System.out.println("result is "+result);

              }

              catch (Exception e) {

                     System.err.println(e.toString());

              }

       }

}

soap方式调用

调用java生成的webservice

import org.apache.soap.util.xml.*;

import org.apache.soap.*;

import org.apache.soap.rpc.*;

 

import java.io.*;

import java.net.*;

import java.util.Vector;

 

public class caService{

       public static String getService(String user) {

       URL url = null;

       try {

           url=new URL("http://192.168.0.100:8080/ca3/services/caSynrochnized");

       } catch (MalformedURLException mue) {

          return mue.getMessage();

         }

             // This is the main SOAP object

       Call soapCall = new Call();

       // Use SOAP encoding

       soapCall.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);

       // This is the remote object we're asking for the price

       soapCall.setTargetObjectURI("urn:xmethods-caSynrochnized");

       // This is the name of the method on the above object

       soapCall.setMethodName("getUser");

       // We need to send the ISBN number as an input parameter to the method

       Vector soapParams = new Vector();

 

       // name, type, value, encoding style

       Parameter isbnParam = new Parameter("userName", String.class, user, null);

       soapParams.addElement(isbnParam);

       soapCall.setParams(soapParams);

       try {

          // Invoke the remote method on the object

          Response soapResponse = soapCall.invoke(url,"");

          // Check to see if there is an error, return "N/A"

          if (soapResponse.generatedFault()) {

              Fault fault = soapResponse.getFault();

             String f = fault.getFaultString();

             return f;

          } else {

             // read result

             Parameter soapResult = soapResponse.getReturnValue ();

             // get a string from the result

             return soapResult.getValue().toString();

          }

       } catch (SOAPException se) {

          return se.getMessage();

       }

    }

}

返回一维数组时

Parameter soapResult = soapResponse.getReturnValue();

String[] temp = (String[])soapResult.getValue();

 

调用ASP.Net生成的webservice

private String HelloWorld(String uri, String u) {

              try {

                     SOAPMappingRegistry smr = new SOAPMappingRegistry();

                     StringDeserializer sd = new StringDeserializer();

                     ArraySerializer arraySer = new ArraySerializer();

                     BeanSerializer beanSer = new BeanSerializer();

                     smr.mapTypes(Constants.NS_URI_SOAP_ENC, new QName(

                                   "http://tempuri.org/", "HelloWorldResult"), String.class,

                                   null, sd);

                     smr.mapTypes(Constants.NS_URI_SOAP_ENC, new QName(

                                   "http://tempuri.org/", "temp"), String.class,

                                   beanSer, beanSer);

 

                     URL url = new URL(uri);

                     Call call = new Call();

                     call.setSOAPMappingRegistry(smr);

                     call.setTargetObjectURI("urn:xmethods-Service1");

                     call.setMethodName("HelloWorld");

                     call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);

 

                     Vector soapParams = new Vector();

                     soapParams.addElement(new Parameter("temp", String.class, u, null));

                     call.setParams(soapParams);

 

                     Response soapResponse = call.invoke(url,"http://tempuri.org/HelloWorld");

 

                     if (soapResponse.generatedFault()) {

                            Fault fault = soapResponse.getFault();

                            System.out.println(fault);

                     } else {

                            Parameter soapResult = soapResponse.getReturnValue();

                            Object obj = soapResult.getValue();

                            System.out.println("===" + obj);

                     }

              } catch (Exception e) {

                     e.printStackTrace();

              }

              return null;

       }
posted @ 2006-02-27 13:46 单炒饭 阅读(1078) | 评论 (0)编辑 收藏

Webservice开发

1.  发布环境:win2000 Professional + JDK1.4.2_03 + Tomcat5

2.  下载Axis,解压缩,将其webapps目录下的axis拷贝到tomcatwebapps目录下,进行访问测试,http://localhost:8080/axis/  出现正常页面即可。

3.  下载包含wtpEclipse,解压缩

4.  新建动态Web Project,比如ca3,将axis下的jar包导入该项目的编译环境里,在JavaSource中写java程序比如caSynrochnized,写好后,在上面点右键,选择Create Web Service按照默认设置,即可生成Web Service

tomcat下部署

5. 生成后,将eclipseca3\.deployables下的ca3目录拷贝到tomcatwebapps目录下

6. 设置axis的环境变量,如下                                                                                    

a)        AXIS_HOME  E:\Tomcat5.0\webapps\axis

b)        AXIS_LIB    %AXIS_HOME%\WEB-INF\lib

c)         Classpath   .;%AXIS_LIB%\axis.jar;%AXIS_LIB%\commons-discovery-0.2.jar;%AXIS_LIB%\commons-logging-1.0.4.jar;%AXIS_LIB%\jaxrpc.jar;%AXIS_LIB%\saaj.jar;%AXIS_LIB%\log4j-1.2.8.jar; %AXIS_LIB%\wsdl4j-1.5.1.jar;

7. E:\Tomcat 5.0\webapps\ca3\WEB-INF\caSynrochnizedService\com\hshz\ca找到deploy.wsdd文件,在dos命令行状态下进入上面目录,运行以下命令进行发布        java org.apache.axis.client.AdminClient deploy.wsdd

8.  IE中输入http://localhost:8080/ca3/services可以看到已发布的webservice

E:\Tomcat 5.0\webapps\ca3\wsdl目录下找到wsdl文件,最后几行比如<wsdlsoap:address location="http://localhost:8080/ca3/services/caSynrochnized"/>其中的location才是web Service相互调用的地址,另外localhost改为自己的IP地址。

Webservice的重新部署

对于已发布的服务,修改接口后,直接将发布目录下的wsdl,以及wsddclassesservice文件夹拷贝到tomcat相应目录下,不用重新发布即可。可先在浏览器中输入地址/services进行查看。

部署时可能遇到的问题

1)             dos窗口下执行java org.apache.axis.client.AdminClient deploy.wsdd命令时,出现404错误,此时可能你的tomcat服务器没有启动,请先启动tomcat服务器。

2)             不同系统安装相同的jdk版本,发布webservice服务时,可能会出现unsupportedVersionException,如果在IE下敲入http://localhost:8080/java-oa/services,发现服务已经发布成功,并且点wsdl链接能够显示wsdl文件,则此错误可以忽略

3)             如果发现在启动tomcat时,出现server-config.wsdd文件需要typehandle一类的错误,则有可能你的应用下存在gnujaxp.jar,因为这个jar包会与axis所需要的jar包相冲突,将gnujaxp.jar拷贝到common\lib下即可。

4)             如果webservice中的方法名字或者参数名或者参数数目,更改后需要重新发布webservice

提供webservice中的程序在方法名,参数不变的情况下,重新编译后只需要覆盖原来的类即可。
posted @ 2006-02-27 13:41 单炒饭 阅读(1887) | 评论 (1)编辑 收藏

仅列出标题
共2页: 上一页 1 2