Chapter: Internet & World Wide Web HOW TO PROGRAM - Rich Internet Application Server Technologies - Web Services

| Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail |

Consuming a Web Service

A web service client can be any type of application or even another web service.

Consuming a Web Service

 

Now that we’ve defined and deployed our web service, we can consume it from a client application. A web service client can be any type of application or even another web service. You enable a client application to consume a web service by adding a web service reference to the application. This process defines the proxy class that allows the client to access the web service.

 

1. Creating a Client in Netbeans to Consume the HugeInteger

Web Service

 

In this section, you’ll use Netbeans to create a client Java desktop GUI application, then you’ll add a web service reference to the project so the client can access the web service. When you add the web service reference, the IDE creates and compiles the client-side ar-tifacts—the framework of Java code that supports the client-side proxy class. The client then calls methods on an object of the proxy class, which uses the rest of the artifacts to interact with the web service.

 

Creating a Desktop Application Project in Netbeans

Before performing the steps in this section, ensure that the HugeInteger web service has been deployed and that the Sun Java System Application Server is running (see Section 28.3.3). Perform the following steps to create a client Java desktop application in

Netbeans:

 

1. Select File > New Project… to open the New Project dialog.

 

2. Select General from the Categories list and Java Application from the Projects list, then click Next >.

 

3. Specify the name UsingHugeInteger in the Project Name field and uncheck the Create Main Class checkbox. In a moment, you’ll add a subclass of JFrame that contains a main method.

 

4. Click Finish to create the project.

 

Adding a Web Service Reference to an Application

Next, you’ll add a web service reference to your application so that it can interact with the HugeInteger web service. To add a web service reference, perform the following steps.

 

1. Right click the project name (UsingHugeInteger) in the Netbeans Projects tab.

 

2. Select New > Web Service Client… from the pop-up menu to display the New Web Service Client dialog (Fig. 28.7).


3. In the     field, specify the URL WSDL URL http://localhost:8080/HugeInteger/

HugeIntegerService?WSDL (Fig. 28.7). This URL tells the IDE where to find the web service’s WSDL description. [Note: If the Sun Java System Application Serv-er is located on a different computer, replace localhost with the hostname or IP address of that computer.] The IDE uses this WSDL description to generate the client-side artifacts that compose and support the proxy. Note that the New Web Service Client dialog enables you to search for web services in several locations. Many companies simply distribute the exact WSDL URLs for their web services, which you can place in the          field.

6    In the Package field, specify com.deitel.iw3htp4.ch28.usinghugeinteger as

 

the package name.

 

7  Click Finish to dismiss the New Web Service Client dialog.

 

In the Netbeans Projects tab, the UsingHugeInteger project now contains a Web Service References folder with the HugeInteger web service’s proxy (Fig. 28.8). Note that the proxy’s name is listed as HugeIntegerService, as we specified in line 11 of Fig. 28.2.

 

When you specify the web service you want to consume, Netbeans accesses the web service’s WSDL information and copies it into a file in your project (named HugeInte-gerService.wsdl in this example). You can view this file from the Netbeans Files tab by expanding the nodes in the UsingHugeInteger project’s xml-resources folder as shown in Fig. 28.9. If the web service changes, the client-side artifacts and the client’s copy of the WSDL file can be regenerated by right clicking the HugeIntegerService node shown in Fig. 28.8 and selecting Refresh Client.

You can view the IDE-generated client-side artifacts by selecting the Netbeans Files tab and expanding the UsingHugeInteger project’s build folder as shown in Fig. 28.10.

 


2. Consuming the HugeInteger Web Service

 

For this example, we use a GUI application to interact with the web service HugeInteger web service. To build the client application’s GUI, you must first add a subclass of JFrame to the project. To do so, perform the following steps:

 

1. Right click the project name in the Netbeans Project tab.

 

2. Select New > JFrame Form… to display the New JFrame Form dialog.

 

3. Specify UsingHugeIntegerJFrame in the Class Name field.

4. Specify com.deitel.iw3htp4.ch28.hugeintegerclient in the Package field.

 

5. Click Finish to close the New JFrame Form dialog.

 

Next, use the Netbeans GUI design tools to build the GUI shown in the sample screen captures at the end of Fig. 28.11.

 

The application in Fig. 28.11 uses the HugeInteger web service to perform compu-tations with positive integers up to 100 digits long. To save space, we do not show the Net-beans autogenerated initComponents method, which contains the code that builds the GUI components, positions them and registers their event handlers. To view the complete source code, open the UsingHugeIntegerJFrame.java file in this example’s folder under

src\java\com\deitel\iw3htp4\ch28\hugeintegerclient. Netbeans places the GUI component instance-variable declarations at the end of the class (lines 326–335). Java allows instance variables to be declared anywhere in a class’s body as long as they are placed outside the class’s methods. We continue to declare our own instance variables at the top of the class.

 

Lines 6–7 import the classes HugeInteger and HugeIntegerService that enable the client application to interact with the web service. We include these import declarations only for documentation purposes here. These classes are in the same package as Using-HugeIntegerJFrame, so these import declarations are not necessary. Notice that we do not have import declarations for most of the GUI components used in this example. When you create a GUI in Netbeans, it uses fully qualified class names (such as javax.swing.JFrame in line 11), so import declarations are unnecessary.

 

Lines 13–14 declare the variables of type HugeIntegerService and HugeInteger, respectively. Line 24 in the constructor creates an object of type HugeIntegerService.

 

Line 25 uses this object’s getHugeIntegerPort method to obtain the HugeInteger proxy object that the application uses to invoke the web service’s method.

 

Lines 165–166, 189–190, 213–214, 240–241 and 267–268 in the various JButton event handlers invoke the HugeInteger web service’s web methods. Note that each call is made on the local proxy object that is referenced by hugeIntegerProxy. The proxy object then communicates with the web service on the client’s behalf.

 

The user enters two integers, each up to 100 digits long. Clicking any of the five JBut-tons causes the application to invoke a web method to perform the corresponding task and return the result. Our client application cannot process 100-digit numbers directly. Instead the client passes String representations of these numbers to the web service’s web methods, which perform tasks for the client. The client application then uses the return value of each operation to display an appropriate message.

19  // Fig. 28.11: UsingHugeIntegerJFrame.java

 

20  // Client desktop application for the HugeInteger web service.

 

21  package com.deitel.iw3htp4.ch28.hugeintegerclient;

4

21  // import classes for accessing HugeInteger web service's proxy

 

22  import com.deitel.iw3htp4.ch28.hugeintegerclient.HugeInteger;

 

23  import com.deitel.iw3htp4.ch28.hugeintegerclient.HugeIntegerServic

24           

9   import javax.swing.JOptionPane; // used to display errors to the user

 

10

1       public class UsingHugeIntegerJFrame extends javax.swing.JFrame

2       {

 

3             private HugeIntegerService hugeIntegerService; // used to obtain proxy

 

4             private HugeInteger hugeIntegerProxy; // used to access the web service

5              

14          // no-argument constructor

 

15          public UsingHugeIntegerJFrame()

16          {

 

17                 initComponents();

20

23                 try

24                 {

 

25                       // create the objects for accessing the HugeInteger web service

 

26                       hugeIntegerService = new HugeIntegerService();

 

27                       hugeIntegerProxy = hugeIntegerService.getHugeIntegerPort();

28                 }

 

29                 catch ( Exception exception )

30                 {

 

31                       exception.printStackTrace();

32                 }

 

33          } // end UsingHugeIntegerJFrame constructor

32

 

32          // The initComponents method is autogenerated by Netbeans and is called

 

33          // from the constructor to initialize the GUI. This method is not shown

 

34          // here to save space. Open UsingHugeIntegerJFrame.java in this

 

35          // example's folder to view the complete generated code (lines 37-153).

36           

41           // invokes HugeInteger web service's add method to add HugeIntegers

 

42           private void addJButtonActionPerformed(

 

43                  java.awt.event.ActionEvent evt )

44           {

 

45                  String firstNumber = firstJTextField.getText();

 

46                  String secondNumber = secondJTextField.getText();

160

50                  if ( isValid( firstNumber ) && isValid( secondNumber ) )

51                  {

52                        try

53                        {

 

54                              resultsJTextArea.setText(

166

hugeIntegerProxy.add( firstNumber, secondNumber ) );

59                        } // end try

 

60                        catch ( Exception e )

{

170     JOptionPane.showMessageDialog( this, e.toString(),

171     "Add method failed", JOptionPane.ERROR_MESSAGE );

68                              e.printStackTrace();

 

69                        } // end catch

70                  } // end if

 

71           } // end method addJButtonActionPerformed

72           

74           // invokes HugeInteger web service's subtract method to subtract the

 

75           // second HugeInteger from the first

 

76           private void subtractJButtonActionPerformed(

 

77                  java.awt.event.ActionEvent evt )

78           {

 

79                  String firstNumber = firstJTextField.getText();

 

80                  String secondNumber = secondJTextField.getText();

184

77                  if ( isValid( firstNumber ) && isValid( secondNumber ) )

78                  {

79                        try

80                        {

 

resultsJTextArea.setText(

190

hugeIntegerProxy.subtract( firstNumber, secondNumber ) );

191     } // end try

 

192     catch ( Exception e )

193     {

 

194     JOptionPane.showMessageDialog( this, e.toString(),

195     "Subtract method failed", JOptionPane.ERROR_MESSAGE );

 

196     e.printStackTrace();

 

197     } // end catch

198     } // end if

 

199     } // end method subtractJButtonActionPerformed

200    

201     // invokes HugeInteger web service's bigger method to determine whether

 

202     // the first HugeInteger is greater than the second

 

203     private void biggerJButtonActionPerformed(

 

204     java.awt.event.ActionEvent evt )

205     {

 

206     String firstNumber = firstJTextField.getText();

 

207     String secondNumber = secondJTextField.getText();

208

209     if ( isValid( firstNumber ) && isValid( secondNumber ) )

210     {

211     try

212     {

 

213     boolean result =

 

            hugeIntegerProxy.bigger( firstNumber,   secondNumber );                

214                                                    

215     resultsJTextArea.setText( String.format(  "%s %s %s %s",      

216                 firstNumber,  (           result ? "is" : "is not" ), "greater than",     

217                 secondNumber        )           );                                 

218     } // end try

 

219     catch ( Exception e )

220     {

 

221     JOptionPane.showMessageDialog( this, e.toString(),

222     "Bigger method failed", JOptionPane.ERROR_MESSAGE );

223     e.printStackTrace();

 

224     } // end catch

225     } // end if

 

226     } // end method biggerJButtonActionPerformed

227    

228     // invokes HugeInteger web service's smaller method to determine

 

229     // whether the first HugeInteger is less than the second

 

230     private void smallerJButtonActionPerformed(

 

231     java.awt.event.ActionEvent evt )

232     {

 

233     String firstNumber = firstJTextField.getText();

 

234     String secondNumber = secondJTextField.getText();

235

236     if ( isValid( firstNumber ) && isValid( secondNumber ) )

237     {

238     try

239     {

 

240     boolean result =

 

            hugeIntegerProxy.smaller( firstNumber, secondNumber );                 

241                                        

242     resultsJTextArea.setText( String.format( "%s %s %s %s",       

243                 firstNumber,  (           result ? "is" : "is not" ), "less than",          

244                 secondNumber        )           );                     

245     } // end try

 

246     catch ( Exception e )

247     {

 

248     JOptionPane.showMessageDialog( this, e.toString(),

249     "Smaller method failed", JOptionPane.ERROR_MESSAGE );

 

250     e.printStackTrace();

 

251     } // end catch

252     } // end if

 

253     } // end method smallerJButtonActionPerformed

254    

255     // invokes HugeInteger web service's equals method to determine whether

 

256     // the first HugeInteger is equal to the second

 

257     private void equalsJButtonActionPerformed(

 

258     java.awt.event.ActionEvent evt )

259     {

 

260     String firstNumber = firstJTextField.getText();

 

261     String secondNumber = secondJTextField.getText();

262

263     if ( isValid( firstNumber ) && isValid( secondNumber ) )

264     {

265     try

266     {

 

267     boolean result =

 

            hugeIntegerProxy.equals( firstNumber,  secondNumber );                

268                                                    

269     resultsJTextArea.setText( String.format(  "%s %s %s %s",      

270                 firstNumber,  (           result ? "is" : "is not" ), "equal to",

271                 secondNumber        )           );                                 

272     } // end try

 

273     catch ( Exception e )

274     {

275     JOptionPane.showMessageDialog( this, e.toString(),

276     "Equals method failed", JOptionPane.ERROR_MESSAGE );

277     e.printStackTrace();

 

278     } // end catch

279     } // end if

 

280     } // end method equalsJButtonActionPerformed

281    

282     // checks the size of a String to ensure that it is not too big

 

283     // to be used as a HugeInteger; ensure only digits in String

 

284     private boolean isValid( String number )

285     {

 

286     // check String's length

 

287     if ( number.length() > 100 )

288     {

 

289     JOptionPane.showMessageDialog( this,

 

290     "HugeIntegers must be <= 100 digits.", "HugeInteger Overflow",

 

291     JOptionPane.ERROR_MESSAGE );

 

292     return false;

293     } // end if

294

295     // look for nondigit characters in String

 

296     for ( char c : number.toCharArray() )

297     {

 

298     if ( !Character.isDigit( c ) )

299     {

 

300     JOptionPane.showMessageDialog( this,

 

 

301     "There are nondigits in the String",

302     "HugeInteger Contains Nondigit Characters",

303     JOptionPane.ERROR_MESSAGE );

304     return false;

305     } // end if

306     } // end for

307

308     return true; // number can be used as a HugeInteger

 

309     } // end method validate

310

311     // main method begins execution

 

312     public static void main( String args[] )

313     {

 

314     java.awt.EventQueue.invokeLater(

 

315     new Runnable()

316     {

 

317     public void run()

318     {

319     new UsingHugeIntegerJFrame().setVisible( true );

 

320     } // end method run

 

321     } // end anonymous inner class

 

322     ); // end call to java.awt.EventQueue.invokeLater

 

323     } // end method main

324

325     // Variables declaration - do not modify

 

326     private javax.swing.JButton addJButton;‘

122       private javax.swing.JButton biggerJButton;

 

123       private javax.swing.JLabel directionsJLabel;

 

124       private javax.swing.JButton equalsJButton;

 

125       private javax.swing.JTextField firstJTextField;

 

126       private javax.swing.JScrollPane resultsJScrollPane;

 

127       private javax.swing.JTextArea resultsJTextArea;

 

128       private javax.swing.JTextField secondJTextField;

 

129       private javax.swing.JButton smallerJButton;

 

130       private javax.swing.JButton subtractJButton;

 

131       // End of variables declaration

 

132     } // end class UsingHugeIntegerJFrame


 

 




Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail


Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.