⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stockclient.java

📁 java web services how to program
💻 JAVA
字号:
// Fig. 7.26: StockClient.java.
// StockClient uses the WASP API for Java and an online WSDL 
// document to access a Web service that generates stock quotes.
package com.deitel.jws1.wsdl.client;

// Java core packages
import java.awt.*;
import java.awt.event.*;

// Java extension packages
import javax.swing.*;

// WASP packages
import org.idoox.webservice.client.WebServiceLookup;
import org.idoox.webservice.client.WebServiceLookupException;
import org.idoox.wasp.Context;

// Deitel packages
import com.deitel.jws1.services.Stock;

public class StockClient extends JFrame {

   private final static int FRAME_WIDTH = 400;
   private final static int FRAME_HEIGHT = 100;

   // interface for accessing Delayed Stock Quote Web service
   private Stock service;

   // URL of Web service
   private final static String SERVICE_URL = 
      "http://66.28.98.121:9090/soap";

   // WSDL-document URL
   private final static String WSDL_URL = 
      "http://services.xmethods.net/soap/" +
      "urn:xmethods-delayed-quotes.wsdl";

   public StockClient( String title )
   {
      super( title );
      getContentPane().setLayout( new GridLayout( 2, 2 ) );

      // JLabel to display stock quote after invoking Web service
      final JLabel resultsLabel = new JLabel();

      // JComboBox for selecting company from which to receive 
      // associated stock quote
      final JComboBox symbolComboBox = new JComboBox();
      symbolComboBox.addItem( "SUNW" );
      symbolComboBox.addItem( "MSFT" );
      symbolComboBox.addItem( "INTL" );
      symbolComboBox.addItem( "IBM" );
      symbolComboBox.addItem( "CSCO" );

      // JButton invokes remote Web Service
      JButton serviceButton = new JButton( "Get Stock Quote" );
      serviceButton.addActionListener(
         new ActionListener() {

            // invoked when user presses JButton
            public void actionPerformed( ActionEvent event )
            {
               // use WASP APIs to access Web service
               try {

                  // get object that performs Web-service lookup
                  WebServiceLookup serviceLookup = 
                     ( WebServiceLookup ) Context.getInstance( 
                        Context.WEBSERVICE_LOOKUP );

                  // lookup Web service from registry
                  service = ( Stock ) serviceLookup.lookup( 
                     WSDL_URL, Stock.class, SERVICE_URL );

                  // invoke Web service to receive stock quote
                  float quote = service.getQuote( 
                     ( String ) symbolComboBox.getSelectedItem() );

                  resultsLabel.setText( "Stock Quote: " +
                     Float.toString( quote ) );
               }

               // handle exception if unable to find Web service
               catch ( WebServiceLookupException exception ) {
                  exception.printStackTrace();
               }

            } // end method actionPerformed
         }
      );

      // store JLabel, JComboBox and JButton JFrame
      getContentPane().add( new JLabel( "Select company:" ) );
      getContentPane().add( symbolComboBox );
      getContentPane().add( resultsLabel );
      getContentPane().add( serviceButton );

   } // end constructor

   // return StockClient frame size
   public Dimension getPreferredSize()
   {
      return new Dimension( FRAME_WIDTH, FRAME_HEIGHT );
   }

   // instantiate StockClient GUI
   public static void main( String args[] )
   {
      StockClient client = 
         new StockClient( "Stock Quote Service" );
      client.setDefaultCloseOperation( EXIT_ON_CLOSE );
      client.pack();
      client.setVisible( true );
   }

} // end class StockClient

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -