You are on page 1of 7

Building a GoogleSearch Web Service Portlet

January 2006 [Revision number: V2-2]


Copyright 2006 Sun Microsystems, Inc.

This tutorial shows how to use Sun Java Studio Creator's integrated development environment (IDE) to develop a JSR-168 compliant JavaServer Faces portlet application that accesses the GoogleSearch web service. The application uses the GoogleSearch web service to check the spelling of words and phrases that users enter on a Search page. To build the GoogleSearch portlet, you add the GoogleSearch web service to the application, then design two pages: the GoogleSearch page in which users type their queries, and the results page. You next set a property on the Session Bean to set a display value for the second page. Finally, you set up navigation between the two pages. The GoogleSearch web service enables querying for Google search results and is one of several sample web services bundled with the Java Studio Creator IDE. Contents - Obtaining a Google License Key - Testing the GoogleSearch Web Service - Creating the GoogleSearch Portlet Project - Designing the Search Page - Designing the Results Page - Creating a Session Bean Property - Adding the Search Code and Navigation - Doing More With Portlets Before you work through this tutorial, familiarize yourself with the application development process, portlets, and web services. Accessing Web Services is a useful introduction to accessing web services from regular web applications. If you are behind a firewall, you will need to configure the IDE's server properties to set the HTTP proxy server (file included in the downloaded ZIP file).

Obtaining a Google License Key


To use the Google web service API's, you must first create a Google account and obtain a license key. Both the Google account and license key are free. The license key allows you to make up to 1,000 automated queries each day. Before continuing with this tutorial, create a Google account and obtain a license key by going to http://www.google.com/apis/.

Testing the GoogleSearch Web Service

1. In the Servers window, expand Web Services > Samples > GoogleSearch. 2. Right-click the doSpellingSuggestion node and then choose Test Method from the pop-up menu. The Test Web Service Method dialog box displays. Note that the method requires the parameters key and phrase. 3. Copy and paste your Google license key in the key parameter field, type a misspelled word or phrase for which to check spelling in the phrase field, and click Submit. The result displays in the Results section of the dialog box. Note that if you enter a correctly spelled word or phrase, or if you enter a random string of characters, Google does not return any results. 4. Click Close to close the dialog box.

Creating the GoogleSearch Portlet Project

1. From the main menu, choose File > New Project. 2. In the New Project wizard, select Web from the Categories list and select JSR-168 JSF Portlet Project from the Projects list, then click Next. 3. Name the project GooglePortlet. 4. Click Next. The Portlet Deployment Descriptor pane opens. 5. Click Finish to accept the default values. GooglePortlet's initial page, PortletPage1, opens in the Visual Designer. 6. From the Servers window, drag Web Services > Samples > GoogleSearch and drop it on PortletPage1. googleSearchClient1 displays in the Outline window.

Designing the Search Page


In this section, you design the GoogleSearch portlet application's Search page. The finished Search page is similar to the following figure.

Figure 1: Search Page Design

1. From the Basic section of the Palette, drag an Image component onto PortletPage1 and drop it in the upper-left corner. 2. In the Properties window, set the url property to http://www.google.com/images/logo_sm.gif. The Google logo displays. 3. Drag a Text Field onto the page and drop it beneath the Google logo. In the Properties window, set the required property to True by clicking its checkbox. 4. Drag a Button component onto the page and drop it to the right of the Text Field component, type Spell, and press Enter. In the Properties window, set the id property to spellButton. 5. Drag a Message Group component onto the page and drop it beneath the text field.

Designing the Results Page


Next you create the GoogleSearch portlet application's Results page. The finished Results page is similar to the following figure.

Figure 2: Results Page Design

1. In the Projects window, right-click GooglePortlet > Web Pages and choose New > Portlet Page. The New Portlet Page dialog box opens. 2. In the File Name field, type ResultPage, then click Finish to close the dialog box. ResultPage opens in the Visual Designer. 3. Copy the Google image from PortletPage1 and paste it onto ResultPage. 4. From the Basic section of the Palette, drag a Static Text component onto the page and then type Spelling suggestion:. 5. Drag a second Static Text component onto the page and drop it to the right of the first Static Text component. 6. Drag a Button component onto the page and drop it beneath the second Static Text component, type Try Again, and press Enter. In the Properties window, set the id property to restartButton.

Creating a Session Bean Property


While developing portlet applications is similar to developing regular web applications, there are some important differences. For example, in a regular web application, you use a Request Bean to pass information among the application's web pages. However, because of differences in the portlet lifecycle, you must create a property on the Session Bean and use that property to pass information among portlet web pages. 1. In the Outline window, right-click SessionBean1 and choose Add > Property. The New Property Pattern dialog box opens. 2. In the New Property Pattern dialog box, type result for the name of the new property and then click OK. The property type is String, the default. 3. Right-click the second Static Text component and select Bind to Data from the pop-up menu. The Bind to Data - StaticText2 dialog box opens. 4. In the Bind to Data - StaticText2 dialog box, click the Bind to an Object tab. The Select binding target pane opens, as shown in the following figure.

Figure 3: Binding the result property to staticText2 5. Choose SessionBean1 > result, then click OK. Note that the Static Text component changes in the Visual Designer.

Adding the Search Code and Navigation

1. Modify PortletPage1's spellButton_action() method to call the Google web service to perform the user's query. Return to PortletPage1 by clicking its tab, then double-click the Spell button. The IDE opens PortletPage1's Java view at the spellButton_action() method. 2. Replace the spellButton_action() method with the following code (shown in bold), replacing your_Google_key with your Google APIs key. After inserting the code, you can press Ctrl-Shift-F to automatically reformat the code. Code Sample 1: GoogleSearch spellButton_action() method public String spellButton_action() throws Exception { String result = ""; String spellword = (String)this.getTextField1().getValue(); try { result = this.getGoogleSearchClient1().doSpellingSuggestion( // Replace with your Google Web APIs license key, in quotes "your_Google_key", spellword); } catch (Exception e) { error(e.getMessage()); log("Page1 failure with GoogleSearch web service", e); return null; } if (result=="") { this.getSessionBean1().setResult(spellword + " is spelled correctly."); } else { this.getSessionBean1().setResult(result); } return "search"; }

3. Click the Design tab to return to the Visual Designer. 4. Set up page navigation. Right-click anywhere in the Visual Designer and choose Page Navigation from the pop-up menu. The Page Navigation window opens. 5. Click the PortletPage1.jsp icon and drag a connector from the button on PortletPage1.jsp to ResultPage.jsp, type search for the new connector name, and then press Enter. 6. Click the ResultPage.jsp icon and drag a connector from the button on ResultPage.jsp to PortletPage1.jsp, type restart for the new connector name, and then press Enter. 7. Click the Run button on the toolbar to deploy the portlet application. The Apache Pluto portlet container displays the portlet within a preview window in your web browser. Type a misspelled word such as Creatar in the search field, then click Spell. The results page displays the correct spelling, similar to the following figure.

Figure 4: GoogleSearch Spelling Results

Doing More With Portlets


To learn how to export then deploy a portlet application to Sun Portal Server 6, see Deploying a Portlet Application.

Use steps similar to those described in this tutorial to use Google web service search results in a portlet. See Also:
q q q q q q q q

Developing a Portlet Application Accessing Web Services Deploying a Portlet Application Creating Portlets in Sun Java Studio Creator 2 David Botterill's Weblog Greg Ziebold's Weblog Introduction to JSR 168 - The Portlet Specification Pluto portal project site

This page was last modified: January 25, 2006

Sun and Third-party Trademarked Terminology


The following Sun trademarked terms might be used in the Sun Java(tm) Studio Creator tutorials:
q q q q q q q q q

Sun Java Studio Creator integrated development environment (IDE) Sun Java System Application Server version number (Application Server) Java Platform, Standard Edition technology (Java SE(tm) platform) JavaServer(tm) Faces technology JavaServer Pages(tm) technology (JSP(tm) technology) Sun Java System Web Server version number (Web Server) Java Database Connectivity software (JDBC software) Enterprise JavaBeans(tm) specification (EJB(tm) specification) Solaris(tm) Operating System software (Solaris OS software)

The following third-party trademarked terms might be used in the Sun Java Studio Creator tutorials:
q q

UNIX(R) software SPARC(R) processor

Copyright 2006 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, California 95054, U.S.A. All rights reserved. Sun Microsystems, Inc. has intellectual property rights relating to technology embodied in the product that is described in this document. In particular, and without limitation, these intellectual property rights may include one or more of the U.S. patents listed at http://www.sun. com/patents and one or more additional patents or pending patent applications in the U.S. and in other countries. U.S. Government Rights - Commercial software. Government users are subject to the Sun Microsystems, Inc. standard license agreement and applicable provisions of the FAR and its supplements. Use is subject to license terms. Sun, Sun Microsystems, the Sun logo, Java and the Java Coffee Cup logo are trademarks or registered trademarks of Sun Microsystems, Inc. in the U.S. and other countries.This product is covered and controlled by U.S. Export Control laws and may be subject to the export or import laws in other countries. Nuclear, missile, chemical biological weapons or nuclear maritime end uses or end users, whether direct or indirect, are strictly prohibited. Export or reexport to countries subject to U.S. embargo or to entities identified on U.S. export exclusion lists, including, but not limited to, the denied persons and specially designated nationals lists is strictly prohibited. Note: Sun is not responsible for the availability of third-party web sites mentioned in this document and does not endorse and is not responsible or liable for any content, advertising, products, or other materials on or available from such sites or resources. Sun will not be responsible or liable for any damage or loss caused or alleged to be caused by or in connection with use of or reliance on any such content, goods, or services available on or through any such sites or resources.

Copyright 2006 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, California 95054, tats-Unis. Tous droits rservs.

Sun Microsystems, Inc. dtient les droits de proprit intellectuels relatifs la technologie incorpore dans le produit qui est dcrit dans ce document. En particulier, et ce sans limitation, ces droits de proprit intellectuelle peuvent inclure un ou plus des brevets amricains lists l'adresse http://www.sun.com/patents et un ou les brevets supplmentaires ou les applications de brevet en attente aux tats-Unis et dans les autres pays. L'utilisation est soumise aux termes de la Licence. Sun, Sun Microsystems, le logo Sun, Java et le logo Java Coffee Cup sont des marques de fabrique ou des marques dposes de Sun Microsystems, Inc. aux tats-Unis et dans d'autres pays.Ce produit est soumis la lgislation amricaine en matire de contrle des exportations et peut tre soumis la rglementation en vigueur dans d'autres pays dans le domaine des exportations et importations. Les utilisations, ou utilisateurs finaux, pour des armes nuclaires,des missiles, des armes biologiques et chimiques ou du nuclaire maritime, directement ou indirectement, sont strictement interdites. Les exportations ou rexportations vers les pays sous embargo amricain, ou vers des entits figurant sur les listes d'exclusion d'exportation amricaines, y compris, mais de manire non exhaustive, la liste de personnes qui font objet d'un ordre de ne pas participer, d'une faon directe ou indirecte, aux exportations des produits ou des services qui sont rgis par la lgislation amricaine en matire de contrle des exportations et la liste de ressortissants spcifiquement dsigns, sont rigoureusement interdites. Sun Microsystems n'est pas responsable de la disponibilit de tiers emplacements d'enchanement mentionns dans ce document et n'approuve pas et n'est pas responsable ou iresponsable d'aucun contenu, de la publicit, de produits, ou d'autres matriaux dessus ou fournis par de tels emplacements ou ressources. Sun ne sera pas responsable ou iresponsable d'aucuns dommages ou perte causs ou allgus pour tre caus par ou en liaison avec l'utilisation de ce produit ou la confiance dans des tels contenu, marchandises, ou services disponibles sur ou par des tels emplacements ou ressources.

You might also like