分享

webservice异常机制实验(2)

 jimmylin88cn 2007-05-20
webservice异常机制实验(2)--建议初学者必做
2007-04-28 15:09

实验三:web服务嵌套调用——CheckPassword.jws异常检测。

本实验由Hello.jws来调用CheckPassword.jws的密码检测服务,然后由CheckPassword.jws产生一个异常,在客户端检测信息。

1: Web Service服务端开发

服务一:Hello.jws源代码:

本程序是检验用户名是否正确的一个服务,但是它不存在检验密码的功能,所以需要调用服务二来检验密码。如果密码对则返回对的消息,否则将返回一个远程异常带到服务一。并由服务一返回给客户端程序处理。

import java.rmi.RemoteException;

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 javax.xml.rpc.ParameterMode;

import javax.xml.rpc.ServiceException;

import java.net.MalformedURLException;

public class Hello{

public String hello(String name,String ps)throws RemoteException,ServiceException,MalformedURLException

{

String result=null;

try{

if(name.equals("vegauser"))

{

      try{

     String endpoint = "http://localhost:8080/axis/CheckPassword.jws?wsdl";//标示webservice路径

      Service service = new Service();//创建service实例

      Call call = (Call) service.createCall();//通过service创建call实例

      call.setTargetEndpointAddress(endpoint);//webservice的服务路径放在call实例中,为call设置服务位置

      call.setOperationName("CheckPassword");//调用webservice方法

      //call.addParameter(ps, 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://localhost:8080/axis/Hello.jws");

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

      }catch(Exception ee)

      {

        return ee.getMessage();

     }

      //CheckPasswordService service = new CheckPasswordServiceLocator();

      //CheckPassword cps=service.getCheckPassword();

      if(result.equals("password is true"))

           return "你好"+name+",your"+result+",欢迎来到Web服务的世界!";

           else

           return result;

}

else

throw new RemoteException("A RemoteException occurs:username is wrong! In the Hello.jws");

}catch(RemoteException e)

{

   //e.printStackTrace(System.err);

   return e.getMessage();

}

}

}

服务一Hello.jws中调用的服务二:CheckPassword.jws

import java.rmi.RemoteException;

public class CheckPassword{

public String CheckPassword(String ps)throws RemoteException{

try{

if(ps.equals("vegauser"))

return "password is true";

else

throw new RemoteException("A RemoteException occurs:password is wrong! In the CheckPassword.jws");

}catch(RemoteException e)

{

   //e.printStackTrace(System.err);

   return e.getMessage();

}

}

}

2: Web Service客户端开发

//该程序是在本地输入用户名,如果不为空,则调用web服务一予以响应,如果为空,则在本地报错。

package localhost.axis.Hello_jws;

import java.io.*;

public class Main{

public static void main(String args[])throws IOException,Exception

{

   HelloService service = new HelloServiceLocator();

   Hello hello = service.getHello();

   System.out.println("请输入你的用户名: ");

   BufferedReader in1=new BufferedReader(new InputStreamReader(System.in));

   //String s,ss;

   String s=in1.readLine();

   System.out.println("请输入你的密码: ");

   BufferedReader in2=new BufferedReader(new InputStreamReader(System.in));

   String ss=in2.readLine();

   try

   {

    if(s.length()!=0)

    System.out.println("Response:"+hello.hello(s,ss));

    else if(s.length()==0)

    {

           throw new IOException("username is empty in local machine!");

    }

   }catch(IOException e)

    {

           //System.err.println(e);//输出结果:java.io.IOExcepion:username is empty

           e.printStackTrace(System.err);//输出结果java.io.IOExcepion:username is empty at yhInput.main(yhInput.java:14)

          }

}

}

3:执行结果示例


 

                     图三:服务嵌套异常机制

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多