Creating a web service client
In this recipe, we will use Google Maps to show how NetBeans enables developers to quickly
create an application using web services provided by third parties.
It is require d to have NetBeans with Java EE support installed to continue with this recipe.
If this particular NetBeans version is not available in your machine, please visit:
We will use the GlassFish Server in this recipe, since it is the only server that supports Java EE
6 at the moment.
For our recipe to work, we will need a valid key for the Google Maps API. The key can be
On the site, we will generate the key. Tick the box that says I have read and agree with the
terms and conditions, after reading and agreeing of course.
Under My website URL, enter:
Or the correct port in which GlassFish is registered.
Then click on Generate API key.
Save this key, we will be using it later.
How to do it…
Creating the Java Web Project
- Click File and then New Project or Press Ctrl+Shift+N.
- For New Project: On the Categories side, choose Java Web and on the Projects side,
- Click Next.
- For Name and Location, under Project Name, enter WebServiceCl ient.
- Tick the box on Use Dedicated Folder for Storing Libraries.
- Now, either type the folder path or select one by clicking on browse.
- After choosing the folder, we can proceed by clicking Next.
- For Server and Settings: Under Server, choose GlassFish Server 3.1.
- Leave the other options with their default values and click Finish.
Right-click on the WebServiceClient project, and select New and then Servlet….
- For New Servlet: Under Class Name, enter WSClientServlet.
- And under package, enter servlet.
- Click Finish.
When the WSClientServlet opens in the editor, remove the code starting with:
/* TODO output your page here
And ending with:
And save the file.
Adding a Web Service
Navigate to the Services window and expand the Web Services node, followed by Google,
and finally Map Service.
Accepting a security certificate is required to access this service and to continue with the
recipe. Please refer the following screenshot:
A new window, Customize getGoogleMap SaaS Service, pops-up.
- Under Input Parameters, double-click the cell on the address row under the Default
Value column, to change the value to the desired address (or keep it default if the
provided one is okay).
- Click OK.
//out.println(“The SaasService returned: “+result.getDataAsString());
Remember the key generated in the Getting Ready section?
In the Projects window, expand the Source Packages node and the package org.
netbeans.saas.google, and double-click on googlemapservice.properties.
Paste the key after the = operator.
The line should look like:
Save file, open WSClientServlet and press Shift+F6.
When the Set Servlet Execution URI window pops-up, click OK.
The browser will open with our application path already in place and it will display this:
How it works…
After dragging and dropping the Google Web Service to our class, a folder structure is created
- GoogleMapsService.java: Responsible for checking the coordinates given by the
developer, and checks and reads the key from the properties file.
- Returns HTML text to access GoogleMap.
- RestConnection.java: Responsible for establishing the connection to the
- RestResponse.java: Holds the actual data returned from Google.
- GoogleMapsService: The class that our Servlet uses to interact with the other
classes and Google.
Discovering other web services bundled with the IDE.
There are many other web services available in the Web Service section of the IDE.
Services such as:
- Amazon: EC2 and S3
It is just a matter of checking the documentation of the service provider, and starting to code
your own implementation. Try it out!