当前页面: 开发资料首页 → Netbeans 专题 → 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.
Before you begin, you need to have the following software installed on your computer:
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.
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.
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.
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.
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.
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.
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(); }
try { String myReturnedQuotation = getQuotesSoap().getQuote(/* TODO enter operation arguments */); out.println(myReturnedQuotation); } catch(java.rmi.RemoteException ex) { // TODO handle remote exception }
Now you should see something like this:
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.
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
To fix this problem, do the following:
Make sure that the values you set for these properties are appropriate to your environment.
The dialog box should now look similar to the following:
To fix this problem, do the following:
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 .