Sun Java Solaris Communities My SDN Account Join SDN
 
Article

Learning JavaFX Script, Part 3: Client-Server Communication With JAX-WS

 
By Robert Eckstein, September 2007  

Articles Index

Part 1 of this series introduced the syntax and semantics of the JavaFX Script programming language to the Java technology programmer. Part 2 continued this series by demonstrating how JavaFX Script can use a helper class to make a simple client-server connection using Java's Remote Method Invocation (RMI) technology.

RMI is a fantastic prototyping tool, but it has its limitations for real-world use. A more common solution for programmers creating client-server tools on the Internet is to use a service-oriented architecture (SOA) solution such as the Java API for XML Web Services (JAX-WS). This article will show you how to use JavaFX Script to access a web service using JAX-WS 2.1 in much the same way that the previous article in this series showed you how to use RMI.

Exporting a Server Class as a Web Service Using NetBeans IDE 5.5.1

Begin by creating a simple web service using JAX-WS 2.1. The first thing you need is a class with one or more methods that you wish to export as a web service. For simplicity, you can reuse the server code from Part 2 of this series, which is shown in Code Example 1.

Code Example 1
package server;

import java.io.*;

public class ServerImpl {

    private String name;

    public ServerImpl() {
        super();
    }

    public String ping(String s) {
        return "Hello " + s;
    }

}
 

To export this method, you must do two things: Add an import statement for the javax.jws.WebService package, and add a @WebService annotation at the beginning that tells the Java interpreter that you intend to publish the methods of this class as a web service. These simple additions appear in Code Example 2 in bold type.

Code Example 2
package server;

import java.io.*;
import javax.jws.WebService;

@WebService

public class ServerImpl {

    private String name;

    public ServerImpl() {
        super();
    }

    public String ping(String s) {
        return "Hello " + s;
    }

}
 

At this point, you can publish the web service to an application server. This article discusses how to publish a web service in NetBeans IDE 5.5.1. If you would like to create a web service using the embedded server in the Java SE 6 platform, outside of an IDE, then look through the article Introducing JAX-WS 2.0 With the Java SE 6 Platform, Part 1.

  1. Choose File -> New Project. You should see the dialog box shown in Figure 1.
     
    NetBeans IDE 5.5.1 New Project Dialog Box
    Figure 1. NetBeans IDE 5.5.1 New Project Dialog Box
     
  2. In the Categories column, choose Web. In the Projects subwindow, choose Web Application. Click Next, and you should see the page shown in Figure 2.
     
    New Web Application Dialog Box
    Figure 2. New Web Application Dialog Box
     
  3. In the Project Name field, type WebServiceExample. Note that the context root of the web application changes to the same name. Also, deselect the Set Source Level to 1.4 option if it is selected. The source level must be 1.5 or higher in order for you to add web services to the project. Finally, click the Finish button. If you accidentally click Next, the NetBeans IDE will give you the option of including various frameworks. Just ignore the frameworks and press Finish.
     
    The dialog box should close and you should see the WebServiceExample project added to the Projects subwindow in the upper left, as shown in Figure 3.
     
    NetBeans IDE 5.5.1 Projects Subwindow With WebServiceExample Project
    Figure 3. NetBeans IDE 5.5.1 Projects Subwindow With WebServiceExample Project
     
  4. Next, left-click the WebServiceExample project name to select it. That is, click on the root node with the small globe icon immediately to the left. Right-click, and select New -> Web Service... You should now see the dialog box shown in Figure 4.
     
    New Web Service
Dialog Box
    Figure 4. New Web Service Dialog Box
     
  5. In the Web Service Name field, type ExampleService. In the Package field, type ws. Leave the Create an Empty Web Service radio button selected, and click Finish. The NetBeans IDE will generate empty web server source code for you.
     
    If you're using JAX-WS 2.0, you may notice that the annotations that the NetBeans IDE created are slightly more complicated than the simple @WebService annotation introduced earlier in this article. Don't worry -- these parameters and additional annotations are there to help you refine how Java SE 6 or JAX-WS 2.0 publishes the web services. However, the functionality is still the same by default.
     
  6. Change the source of the class to match Code Example 3.
     
    Code Example 3
    /*
     * ExampleService.java
     *
     * Created on August 17, 2007, 2:03 PM
     *
     * To change this template, choose Tools | Template Manager
     * and open the template in the editor.
     */
    
    package ws;
    
    import java.io.*;
    import javax.jws.WebService;
    
    @WebService
    
    public class ExampleService {
    
        private String name;
    
        public ExampleService() {
            super();
        }
    
        public String ping(String s) {
            return "Hello " + s;
        }
    
    }
    
     
  7. To deploy this example, right-click on WebServiceExample in the Projects subwindow again, as shown in Figure 3, and this time choose the Deploy Project menu item. This will start the application server, compile the web service, and deploy it. When this is completed successfully, open a browser and go to the following location:
     
    http://localhost:8084/WebServiceExample/ExampleService
     
    Note that the default application server for NetBeans IDE 5.5.1 is Apache Tomcat, and the default deployment port for Tomcat is 8084, not 8080, which is what many web servers use. After setting your browser to that localhost URL, you should see a table in your web browser similar to Figure 5 that indicates that the ExampleService web service is active.
     
    Example Web Service Running on the Local Computer
    Figure 5. Example Web Service Running on the Local Computer
     
    You can also click on the link that shows the Web Services Definition Language (WSDL) file of the web service. Don't worry if you don't understand the contents of the WSDL. You won't need to in order to complete this tutorial.

What Is a WSDL?

A Web Services Definition Language (WSDL, often pronounced "whiz-dull") file is instrumental in creating a client that can communicate with the web service. WSDL describes the public interface to the web service. It is an XML-based service description for the protocol bindings and message formats required to interact with the web services listed in its directory. The supported operations and messages are described abstractly, and they are then bound to a concrete network protocol and message format.

 
Using the GlassFish Server or Sun Java System Application Server 9

If you wish to use the GlassFish server or Sun Java System Application Server 9 (SJSAS 9) instead of the default Tomcat server, be sure to download, install, and register the server with the NetBeans IDE first. You can find step-by-step instructions on how to do this for the GlassFish server in the latter portion of this excellent article. For the purposes of this article, be sure to specify the appropriate application server in the Server field of the New Web Applications dialog box, as shown in Figure 2. Note that the default port may differ based on the application server you use. Check your application server documentation for details.

Creating a JavaFX Web Services Client in NetBeans IDE 5.5.1

At this point, you can perform the following steps in NetBeans IDE 5.5.1 to create libraries for a web services client. When creating the client, it does not matter if you are using the Java SE 6 mini-web server, the Tomcat server that is bundled with the NetBeans IDE, SJSAS 9, or the GlassFish server. However, the server must be running so that NetBeans IDE 5.5.1 can access the WSDL file.

  1. Choose File -> New Project. You should see a dialog box similar to the one in Figure 6. Select Java Class Library from the Projects category and click Next.
     
    Creating a New Java Project
    Figure 6. Creating a New Java Project
     
  2. In the Project Name field, type JavaFXWebServiceClient, as shown in Figure 7. Note that a bug in NetBeans IDE 5.5 may prevent you from creating your web service client in a project folder that contains spaces in its path. So, for example, the path should not be C:\Documents and Settings\... Then click Finish.
     
    Creating a New Java Class Library
    Figure 7. Creating a New Java Class Library
     
  3. This will create a JavaFXWebServiceClient project in the Projects subwindow, previously shown in Figure 3. Right-click the project node and choose New -> Web Service Client. Select Project for the location of the WSDL, and click the Browse button to the right of the Project entry field. You should see the dialog box shown in Figure 8.
     
    Browsing Web Services in the WebServicesExample Project
    Figure 8. Browsing Web Services in the WebServicesExample Project
     
  4. Browse to the web service that you want to consume. Select the web service ExampleService, and click OK. You will then see the dialog box shown in Figure 9. Note that you can also manually specify a local file or URL for the location of the WSDL.
     
    The New Web Service Client Dialog Box
    Figure 9. The New Web Service Client Dialog Box
     
  5. Type ws in the Package field, and click Finish. The Projects subwindow displays the new web service client, as shown in Figure 10.
     
    The New Web Services Client Project in the Projects Subwindow
    Figure 10. The New Web Services Client Project in the Projects Subwindow
     
  6. Next, inside the package ws, create a Java file called ConnectionHelper, as shown in Code Example 4.
     
    Code Example 4
    package ws;
    
    public class ConnectionHelper {
    
        public static ws.ExampleService getWSConnection() {
    
            try {
    
                ws.ExampleServiceService service = new ws.ExampleServiceService();
                return service.getExampleServicePort();
    
            } catch (Exception ex) {
                // TODO handle custom exceptions here
            }
    
            return null;
         }
    
    }
    
     
  7. You can reuse the same JavaFX Script code as in Part 2 of this series, creating a file called ws.MyClient.fx the same way. That is, left-click on the ws package and choose New -> JavaFX File to bring up the dialog box shown in Figure 11.
     
    Creating a New JavaFX Script File
    Figure 11. Creating a New JavaFX Script File
     
  8. In fact, the only changes are the call to the static getWSConnection() method of the ConnectionHelper class and the display text. Code Example 5 shows the source code, with changes in bold type.
     
    Code Example 5
    import java.lang.*;
    import javafx.ui.*;
    import java.rmi.*;
    
    import ws.ExampleService;
    import ws.ConnectionHelper;
    
    class ButtonClickModel {
        attribute numClicks: Number;
    }
    
    var model = new ButtonClickModel();
    
    var win = Frame {
        width: 200
        content: GridPanel {
            border: EmptyBorder {
               top: 30
               left: 30
               bottom: 30
               right: 30
            }
            rows: 3
            columns: 1
            vgap: 10
            cells:
              [  Button {
                     text: "Click to make Web Services connection!"
    
                     mnemonic: I
                     action: operation() {
    
                         do {   //  Do statements are executed off the EDT
    
                             try {
    
                                 var remoteServer:ExampleService =
                                     ConnectionHelper.getWSConnection();
    
                                 var results = remoteServer.ping("Test");
                                 System.out.println("response: {results}");
                                 model.numClicks++;
    
                             } catch (e:Exception) {
                                 System.out.println("exception: {e}");
                             }
                         }
                     }
                 },
    
                 Label {
                     text: bind "Number of WS connections: {model.numClicks}"
    
                 }
              ]
        }
        visible: true
    };
    
     
  9. Use the same procedure as in Part 2 of this series to modify the JavaFXWebServiceClient properties to run the JavaFX Script client when executing the project. These steps are the following:
     
    • From the Projects window, right-click the project node, and select Properties.
    • In the Categories pane of the Properties dialog box, select Run.
    • Ensure that the Main Class referenced is net.java.javafx.FXShell.
    • Enter the name of the script, ws.MyClient, in the Arguments field. Click OK.
     
    For reference, Figure 12 shows a screen capture of the Run category of the Project Properties dialog box.
     
    The Run Category of the Project Properties Dialog Box
    Figure 12. The Run Category of the Project Properties Dialog Box
     
  10. Finally, right-click the project node and choose Run Project. The Output window should now show output similar to Figure 13.
     
    Running the JavaFX Script Web Services Client Project
    Figure 13. Running the JavaFX Script Web Services Client Project
     

At this point, you have a solid understanding of how JavaFX Script can be used in conjunction with NetBeans IDE 5.5.1 to communicate with a server across a network, using both JAX-WS 2.1 and RMI. If you would like more information on how to create GUIs with JavaFX Script, see the tutorial for Swing programmers in the For More Information section. Also, remember that the JavaFX specifications are still in development, so it is possible that the language parameters may change over time. See the OpenJFX and the OpenJFX Compiler web sites for the most current information.

For More Information
Rate and Review
Tell us what you think of the content of this page.
Excellent   Good   Fair   Poor  
Comments:
Your email address (no reply is possible without an address):
Sun Privacy Policy

Note: We are not able to respond to all submitted comments.