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.

Getting ready

It is required 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:

http://netbeans.org

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 found at:

http://code.google.com/apis/maps/signup.html

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:

http://localhost:8080

Or the correct port in which GlassFish is registered.

Then click on Generate API key.

Getting ready

The generated key looks something like:

ABQIAFDAc4cEkV3R2yqZ_ooaRGXD1RT8M0brOpm-All5BF9Po1KBxRWWERQsusT9yyKEXQAGcYfTLTyArx88Uw

Save this key, we will be using it later.

How to do it...

Creating the Java Web Project

  1. Click File and then New Project or Press Ctrl+Shift+N.
  2. For New Project: On the Categories side, choose Java Web and on the Projects side, select WebApplication.
  3. Click Next.
  4. For Name and Location, under Project Name, enter WebServiceClient.
  5. Tick the box on Use Dedicated Folder for Storing Libraries.
  6. Now, either type the folder path or select one by clicking on browse.
  7. After choosing the folder, we can proceed by clicking Next.
  8. For Server and Settings: Under Server, choose GlassFish Server 3.1.
  9. Leave the other options with their default values and click Finish.

Creating Servlet

Right-click on the WebServiceClient project, and select New and then Servlet....

  1. For New Servlet: Under Class Name, enter WSClientServlet.
  2. And under package, enter servlet.
  3. 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:

How to do it...

Drag and drop getGoogleMap into our Servlets processRequest method.

A new window, Customize getGoogleMap SaaS Service, pops-up.

  1. 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).
  2. Click OK.
How to do it...

When the new block of code is written by NetBeans, uncomment the following line:

//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:

api_key=ABQIAFDAc4cEkV3R2yqZ_ooaRGXD1RT8M0brOpm-All5BF9Po1KBxRWWERQsusT9yyKEXQAGcYfTLTyArx88Uw

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 to do it...

How it works...

After dragging and dropping the Google Web Service to our class, a folder structure is created by NetBeans:

How it works...

Let's check what is in our folder structure:

  • 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 Google servers.
  • RestResponse.java: Holds the actual data returned from Google.
  • GoogleMapsService: The class that our Servlet uses to interact with the other classes and Google.

There's more...

Discovering other web services bundled with the IDE.

Other services

There are many other web services available in the Web Service section of the IDE.

Services such as:

  • Amazon: EC2 and S3
  • Flickr
  • WeatherBug

It is just a matter of checking the documentation of the service provider, and starting to code your own implementation. Try it out!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.218.105