站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Netbeans 专题Consuming Web Services in NetBeans IDE 4.1(1)

Consuming Web Services in NetBeans IDE 4.1(1)

摘要: This article will show you how to use NetBeans IDE 4.1 to consume a simple web service quickly and easily. It will demonstrate the web service facilities available in NetBeans IDE 4.1, but will not delve into too much detail.

The web service that will be consumed will be a simple quotation service, which returns a 'Thought for the Day' whenever its operation is invoked. It is intentionally a simple example, although you can, of course, extend it. Part 2 will provide a more useful example, which will also be more complex and robust.

This tutorial covers the following topics:

Note: An application using web services is as strong as its weakest web service. Should a web service be taken out of circulation, or should it not be functioning for any reason whatsoever, the entire application on which it is based falls apart. The same is true for this tutorial and the web service on which it is based. There is no guarantee that the Quotation service consumed in this tutorial will always be available. However, even if it isn't, the steps below should provide enough general information for the consumption of a service other than the demonstrated Quotation service.

After you have installed and set everything up, the example can be built in 10 minutes.

Getting Started

Installing the Software

Before you begin, you need to have the following software installed on your computer:

  • NetBeans IDE 4.1 (download).
  • Sun Java System (SJS) Application Server Platform Edition 8 2005Q1 (download)
  • Java Standard Development Kit (JDKTM) version 1.4.2 (download) or 5.0 (download)

Registering the Sun Java System Application Server

Before you can create, compile, and deploy web services, you have to register a local instance of the SJS Application Server. If you installed the NetBeans IDE 4.1/SJS Application Server bundle, the local application server is registered automatically.

  1. Choose Tools > Server Manager from the main window.
  2. Click Add Server. Select Sun Java Systems Application Server 8.1 and give a name to the instance. Then click Next.
  3. Specify the server information, the location of the local instance of the application server, and the domain to which you want to deploy.

If you are behind a firewall, you need to set a proxy host and port number on the SJS Application Server. See the first part of Troubleshooting for help.


Consuming Your First Web Service

To consume a web service, you need to create a web service client. For the creation of web service clients, NetBeans IDE 4.1 provides a client creation facility -- the Web Service Client wizard that generates code for looking up a web service. It also provides facilities for developing the created web service client -- a work area consisting of nodes in the Projects window and a built-in client for testing and analyzing web services. These facilities are part of the standard NetBeans IDE installation, they're available straight out of the box and no plug-ins are needed.

Creating the Client

  1. Choose File > New Project (Ctrl-Shift-N). Under Categories, choose Web. Under Projects, choose Web Application. Click Next. Name the project MyApplication and make sure that you specify the Sun Application Server as your target server. Click Finish.

  2. In the Projects window, right-click the MyApplication project node and choose New > File/Folder. In the New File wizard choose Web Service > Web Service Client. The Web Service Client wizard appears. This wizard is used for retrieving the WSDL file on which your client will be based and for generating the files and code snippets that enable the client to connect to the web service. (In the illustration below, the wizard is filled with the values you specify in the next step.)

    Client Wizard

  3. In the Web Service Client wizard you specify the WSDL file that you want to consume.

    In this case, we'll consume a service that retrieves quotations. In the WSDL URL field above, enter the following URL:

    http://www.seshakiran.com/QuoteService/QuotesService.asmx

    Click Retrieve WSDL to download it. If you are behind a firewall, you might need to set a proxy -- otherwise the WSDL file cannot be downloaded. If so, click Proxy Settings and set your proxy host and port number. Next, enter a package name. The package name specifies the package to which the client files will be generated.

  4. Click Finish. In the Projects window, within the Web Service References node, you should see the following:

    Projects window

    The Projects window shows that a web service called 'Quotes' has made the operation 'getQuote' available to your application. In the Files window, you can see the files generated by the Web Service Client wizard in the project's build folder. More time will be spent on this view in Part 2.

Testing the Web Service

  1. In the Projects window, within the Web Service References node, double-click the 'getQuote' operation. The Test Web Service Operation dialog box appears. If the operation required arguments, the upper part of this dialog box would let you specify them. (See Part 2 for details.) However, the getQuote operation requires no arguments.

  2. Click on the Submit button. When you do this, the web service is contacted and the operation is invoked.

  3. Wait for the quotation to be retrieved (it should not take long), click on the Value at the bottom of the dialog box, and you should then see something similar to the following:

    Test Web Service Client wizard

If you see something similar to the results displayed in the above illustration, your test has succeeded. The web service is up and running and has returned a quotation. If a quotation is not returned and you are behind a firewall, see the second part of Troubleshooting for help.

Developing the Client

  1. Right-click the project node in the Projects window, choose New > File/Folder and then choose Web > Servlet. Click Next. Name the servlet MyApplicationServlet and type mypackage.mysubpackage in the Package drop-down. Click Next. Note that the URL mapping for this servlet is /MyApplicationServlet and click Finish. The servlet opens in the Source Editor.

  2. Put your cursor inside the Source Editor, inside the processRequest method, right-click, and choose Web Service Client Resources > Call Web Service Operation. Click the getQuote operation in the "Select Operation to Invoke" dialog box and then click OK.

    Bits of code have now been added to the servlet. Scroll to the bottom, and you see code that connects the client to the web service (much of the code is underlined in red -- ignore that, it's a bug that has no effect on the building and running of the application).

    At the top of the processRequest method you see a snippet of code that invokes the web service. Cut and paste it into the body tags of the processRequest method, and delete the lines that comment out the code (put your cursor in the line and press Ctrl-E to delete the whole line). The method now looks as follows:

     protected void processRequest(HttpServletRequest request, HttpServletResponse response)
     throws ServletException, IOException {
       response.setContentType("text/html;charset=UTF-8");
       PrintWriter out = response.getWriter();
    
       out.println("<html>");
       out.println("<head>");
       out.println("<title>Servlet MyApplicationServlet</title>");
       out.println("</head>");
       out.println("<body>");
    
       try {
         getQuotesSoap().getQuote(/* TODO enter operation arguments */);
       } catch(java.rmi.RemoteException ex) {
          // TODO handle remote exception
       }
    
       out.println("</body>");
       out.println("</html>");
       out.close();
     }
  3. Add String myReturnedQuotation = in front of the first line in the try/catch block and then type out.println(myReturnedQuotation); in the line below, so that the try/catch block is now like this:
     try {
       String myReturnedQuotation = getQuotesSoap().getQuote(/* TODO enter operation arguments */);
       out.println(myReturnedQuotation);
     } catch(java.rmi.RemoteException ex) {
        // TODO handle remote exception
     }

  4. Save the servlet. Remember that you can ignore the red underlinings in this case -- it's a known bug that has no effect on the building and running of the application.

Deploying the Client

  1. Right-click the project node and choose Run Project. After a while, the application should deploy and display a blank JSP page in the IDE's default browser.

  2. In the URL at the top of the page, type the servlet's URL mapping (MyApplicationServlet). Alternatively, before deploying the client, you can also specify the URL mapping in the Run panel of the project's Project Properties dialog box, which you access by right-clicking the project node and choosing Properties.

    Now you should see something like this:

    Result

If you are behind a firewall and have not set your proxy host and port number on the Sun Java System Application Server, an error will be displayed in the browser instead of a quotation from the web service. See Preparing to Deploy for help.

That's it. You're done. Without very much coding, you created a client and consumed a web service. In Part 2, you will interact more closely with the web service facilities provided by NetBeans IDE 4.1 and you will acquire a fuller understanding of the interaction between a client and the web services it consumes.


Troubleshooting

Since the IDE provides most of the code you need for communicating with the web service, the only problems you should encounter are errors in your own code. In addition, though, you might encounter a few problems if you are behind a firewall. The troubleshooting tips that follow attempt to resolve these problems

Applying What You Have Learned

Next Steps

For more information about using NetBeans IDE 4.1, see the following resources:

To send comments and suggestions, get support, and keep informed on the latest developments on the NetBeans IDE J2EE development features, join the mailing list .


↑返回目录
前一篇: Connecting a GUI to a Derby Database with NetBeans IDE
后一篇: Consuming Web Services in NetBeans IDE 4.1(2)