Advanced Web Services Tutorial: Step 3: Create a Service Control to Access the CreditScore Web Service

In this step, you will enhance the logic of the LoanApproval control to access the CreditScore web service. The existing logic of the LoanApproval control is:

If a loan exists for this SSN then turn down the application (return false to caller) otherwise accept the application (return true to the caller).

We are going to expand that logic to:

If the person with this ssn already has a loan then turn down their application (return false) otherwise check the credit score. If the credit score is < 700, turn down the application (return false). If the credit score is 700 or higher, accept the application (return true).

To access the external web service, we will create a new web service control ("service control") to access the CreditScore web service. We will then modify the LoanApproval control to use the new web service control.

To Create a Control to Access a Web Service

To create a new control we will first generate a WSDL file from a web service and then generate a service control from the WSDL.

  1. On the Project Explorer view, open the nodes CreditScoreWS > Java Resources > src > services. Locate the WSDL file CreditScoreService.wsdl

    We will use this WSDL to automatically generate our new control.
  2. To copy the WSDL file, right-click on the file CreditScoreWS/src/services/CreditScoreService.wsdl and select Copy.
  3. Right-click on the LoanApp/Java Resources/src/controls package and select Paste.
  4. Generate a web service control by right-clicking on LoanApp/Java Resources/src/controls/CreditScoreService.wsdl and selecting Web Services > Generate Service Control.
  5. Select Use JAX-RPC Types and click Next.
  6. Confirm that the name for the service control is CreditScoreServiceControl.java. Click Finish.

  7. In the Project Explorer view, double click the file LoanApprovalControlImpl.java to open it in the editor.

    Right click on the editor window and choose Insert > Control. Choose the new CreditScoreServiceControl and click OK.

    Code will appear that declares and instantiates the control

    @Control
    private CreditScoreServiceControl creditScoreServiceControl;
    
  8. In the LoanApprovalControlImpl.java file, add the code shown in red to the getLoanApproval method.
    public boolean getLoanApproval(int ssn, float amount) throws SQLException
    {
            init();
      
            // if they are already borrowing, don't allow another loan
            if (loansDB.getLoanValue(ssn) > 0)
                return false;
            
            if(creditScoreServiceControl.getCreditScore(ssn) < 700)
                return false;
            
            // otherwise, allow the loan.
            loansDB.insertLoan(ssn, amount);
            return true;
    }
    	

    Save your changes with File > Save.

  9. Test the updated web service by right-clicking on LoanApplicationService.java in the services package of the LoanApp project and choosing Run As > Run on Server.

    Test the service by entering SSN values of 599999999 and 600000000 in turn. The larger value will produced a credit score of 700, which results in loan approval.

Click one of the following arrows to navigate through the tutorial:


Still need help? Post a question on the Workshop newsgroup.