Google Port Let

Embed Size (px)

Citation preview

  • 7/28/2019 Google Port Let

    1/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 compliantJavaServer Faces portlet application that accesses the GoogleSearch web service. The application uses the GoogleSearch web serviceto 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: theGoogleSearch page in which users type their queries, and the results page. You next set a property on the Session Bean to set adisplay 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 theJava 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 PortletsBefore 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 thedownloaded 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 andlicense 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 checkspelling 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 youenter a random string of characters, Google does not return any results.

    4. Click Close to close the dialog box.

    1

    http://developers.sun.com/prodtech/javatools/jscreator/learning/tutorials/2/webservices.htmlhttp://www.google.com/apis/http://www.google.com/apis/http://developers.sun.com/prodtech/javatools/jscreator/learning/tutorials/2/webservices.html
  • 7/28/2019 Google Port Let

    2/7

    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 followingfigure.

    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 logodisplays.

    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. Inthe Properties window, set the id property to spellButton.

    5. Drag a Message Group component onto the page and drop it beneath the text field.

    2

  • 7/28/2019 Google Port Let

    3/7

    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 dialogbox 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 pressEnter. 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. Forexample, 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 informationamong 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, typeresult for the name of the new property and then click OK. The property type isString, the default.

    3. Right-click the second Static Text component and select Bind to Data from the pop-up menu. The Bind to Data - StaticText2dialog 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.

    3

    Designing the Results Page

  • 7/28/2019 Google Port Let

    4/7

    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'sspellButton_action() method to call the Google web service to perform the user's query. Returnto PortletPage1 by clicking its tab, then double-click the Spell button. The IDE opens PortletPage1's Java view at thespellButton_action() method.

    2. Replace the spellButton_action() method with the following code (shown in bold), replacingyour_Google_key withyour 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";

    }

    4

  • 7/28/2019 Google Port Let

    5/7

    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, typesearch forthe 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, typerestart forthe 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 wordsuch as Creatar in the search field, then click Spell. The results page displays the correct spelling, similar to the followingfigure.

    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.

    5

    http://developers.sun.com/prodtech/javatools/jscreator/reference/techart/2/portlet_deploy.htmlhttp://developers.sun.com/prodtech/javatools/jscreator/reference/techart/2/portlet_deploy.html
  • 7/28/2019 Google Port Let

    6/7

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

    q Developing a Portlet Applicationq Accessing Web Servicesq Deploying a Portlet Applicationq Creating Portlets in Sun Java Studio Creator 2q David Botterill's Weblogq Greg Ziebold's Weblogq Introduction to JSR 168 - The Portlet Specificationq 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 Sun Java Studio Creator integrated development environment (IDE)q Sun Java System Application Server version number(Application Server)q Java Platform, Standard Edition technology (Java SE(tm) platform)q JavaServer(tm) Faces technologyq JavaServer Pages(tm) technology (JSP(tm) technology)q Sun Java System Web Server version number(Web Server)q Java Database Connectivity software (JDBC software)q Enterprise JavaBeans(tm) specification (EJB(tm) specification)q 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 UNIX(R) softwareq 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 itssupplements. Use is subject to license terms. Sun, Sun Microsystems, the Sun logo, Java and the Java Coffee Cup logo are trademarks orregistered trademarks of Sun Microsystems, Inc. in the U.S. and other countries.This product is covered and controlled by U.S. ExportControl laws and may be subject to the export or import laws in other countries. Nuclear, missile, chemical biological weapons or nuclearmaritime 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 nationalslists 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 notresponsible or liable for any content, advertising, products, or other materials on or available from such sites or resources. Sun will not beresponsible 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.

    6

    http://developers.sun.com/prodtech/javatools/jscreator/learning/tutorials/2/portlets.htmlhttp://developers.sun.com/prodtech/javatools/jscreator/learning/tutorials/2/webservices.htmlhttp://developers.sun.com/prodtech/javatools/jscreator/reference/techart/2/portlet_deploy.htmlhttp://developers.sun.com/prodtech/javatools/jscreator/reference/fi/2/portlets.htmlhttp://blogs.sun.com/roller/page/david?catname=%2FCreatorhttp://blogs.sun.com/gregzhttp://developers.sun.com/prodtech/portalserver/reference/techart/jsr168http://portals.apache.org/plutohttp://portals.apache.org/plutohttp://developers.sun.com/prodtech/portalserver/reference/techart/jsr168http://blogs.sun.com/gregzhttp://blogs.sun.com/roller/page/david?catname=%2FCreatorhttp://developers.sun.com/prodtech/javatools/jscreator/reference/fi/2/portlets.htmlhttp://developers.sun.com/prodtech/javatools/jscreator/reference/techart/2/portlet_deploy.htmlhttp://developers.sun.com/prodtech/javatools/jscreator/learning/tutorials/2/webservices.htmlhttp://developers.sun.com/prodtech/javatools/jscreator/learning/tutorials/2/portlets.html
  • 7/28/2019 Google Port Let

    7/7

    Sun Microsystems, Inc. dtient les droits de proprit intellectuels relatifs la technologie incorpore dans le produit qui est dcrit dans cedocument. 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 dansles autres pays. L'utilisation est soumise aux termes de la Licence. Sun, Sun Microsystems, le logo Sun, Java et le logo Java Coffee Cupsont des marques de fabrique ou des marques dposes de Sun Microsystems, Inc. aux tats-Unis et dans d'autres pays.Ce produit estsoumis 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, desarmes biologiques et chimiques ou du nuclaire maritime, directement ou indirectement, sont strictement interdites. Les exportations ourexportations vers les pays sous embargo amricain, ou vers des entits figurant sur les listes d'exclusion d'exportation amricaines, ycompris, mais de manire non exhaustive, la liste de personnes qui font objet d'un ordre de ne pas participer, d'une faon directe ouindirecte, aux exportations des produits ou des services qui sont rgis par la lgislation amricaine en matire de contrle des exportationset 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 etn'approuve pas et n'est pas responsable ou iresponsable d'aucun contenu, de la publicit, de produits, ou d'autres matriaux dessus oufournis par de tels emplacements ou ressources. Sun ne sera pas responsable ou iresponsable d'aucuns dommages ou perte causs ouallgus pour tre caus par ou en liaison avec l'utilisation de ce produit ou la confiance dans des tels contenu, marchandises, ou servicesdisponibles sur ou par des tels emplacements ou ressources.

    7