實驗三:web服務(wù)嵌套調(diào)用——CheckPassword.jws異常檢測。 本實驗由Hello.jws來調(diào)用CheckPassword.jws的密碼檢測服務(wù),然后由CheckPassword.jws產(chǎn)生一個異常,在客戶端檢測信息。 1: Web Service服務(wù)端開發(fā) 服務(wù)一:Hello.jws源代碼: 本程序是檢驗用戶名是否正確的一個服務(wù),但是它不存在檢驗密碼的功能,所以需要調(diào)用服務(wù)二來檢驗密碼。如果密碼對則返回對的消息,否則將返回一個遠程異常帶到服務(wù)一。并由服務(wù)一返回給客戶端程序處理。 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";//標(biāo)示webservice路徑 Service service = new Service();//創(chuàng)建service實例 Call call = (Call) service.createCall();//通過service創(chuàng)建call實例 call.setTargetEndpointAddress(endpoint);//將webservice的服務(wù)路徑放在call實例中,為call設(shè)置服務(wù)位置 call.setOperationName("CheckPassword");//調(diào)用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服務(wù)的世界!"; 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(); } } } 服務(wù)一Hello.jws中調(diào)用的服務(wù)二: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客戶端開發(fā) //該程序是在本地輸入用戶名,如果不為空,則調(diào)用web服務(wù)一予以響應(yīng),如果為空,則在本地報錯。 package localhost.axis.Hello_jws; import java.io.*; public class 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);//輸出結(jié)果:java.io.IOExcepion:username is empty e.printStackTrace(System.err);//輸出結(jié)果java.io.IOExcepion:username is empty at yhInput.main(yhInput.java:14) } } } 3:執(zhí)行結(jié)果示例
圖三:服務(wù)嵌套異常機制 |