Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your profile (name, country/region, and company) is displayed to the public and will accompany any content you post. You may update your IBM account at any time.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

developerWorks Community:

  • Close [x]

Hello World (updated): Rational Application Developer V7

Create Java, Web service, and database applications

Jane Fung (mailto:jcyfung@ca.ibm.com), Software Developer, IBM Toronto Lab
Jane Fung works on the WebSphere Studio Application Developer Integration Edition tools team. Jane earned a Bachelor of Applied Science degree in Electrical Engineering at the University of Waterloo, Ontario, and is a Sun Java 2 Certified Programmer. You can reach Jane at jcyfung@ca.ibm.com .

Summary:  Try some practical exercises that show how to create a Java™ application, create and invoke a Web service, and create a Web application that can access a Derby database using a Relational Record List. Learn about J2EE application development using IBM Rational Application Developer. The original version of this tutorial for Rational Application Developer V6 is still available.

View more content in this series

Date:  23 Jan 2007
Level:  Introductory PDF:  A4 and Letter (1363 KB | 37 pages)Get Adobe® Reader®

Activity:  50505 views
Comments:  

Creating and invoking a Web service

This exercise demonstrates how to create a Web service and invoke it from JavaServer Pages (JSP) files. A Web service is based on the open standard interface, Web Service Description Language (WSDL). Each Web service is described by a WSDL interface. This interface contains the definitions for port types, operations, and message types for the Web service. The port type contains a collection of operations that this service supports. Each operation has input and output messages defined. In Java terms, a port type is like a class definition, an operation is like a method, and the input and output messages are like the input parameter or return types to a method.

Creating a Web service is pretty straightforward. The Web Service wizard automatically generates everything for you. In this example, create a Java class that returns a hard-coded IBM stock price. Then use the Web Service wizard to make it into a Web service. In that process, a WSDL file is created and to be used later by the JSP client to invoke the Web service.

To invoke a Web service that is hosted on your local or on a remote server, obtain a WSDL file from the Web service provider and then generate a proxy in the tooling. Use the proxy to invoke the Web service.

Web service providers can advertise their Web services using Web Services Inspection Language (WSIL). A WSIL file typically lists all the Web services provided by the service provider. It contains service descriptions and the WSDL file access information. A requestor can use the WSIL file to discover what Web services are available from the service provider.

Web services can also be posted to a public registry called the Universal Description, Discovery and Integration (UDDI). UDDI provides a centralized repository for Web services. Web service providers can publish their Web services, including links to WSDL documents, into a UDDI repository for users to discover and subsequently use.

Would you like to see these steps demonstrated for you?

Show me Show me

Creating a new Web project

Create a new Web project that only contains the Java class and the generated Web service. A Web project is basically a Web component in the J2EE specification. You'll create the JSP client in another Web project later.

  1. From the workbench, select File > New > Project.
  2. Select Web > Dynamic Web Project > Next.
  3. Type MyDemoWebServiceProject as the project name.
  4. Click Finish. Select Yes if prompted to switch to the Web perspective.

Creating a Java class

Create a Java class that acts as a back end of the Web service. This Java class does not really do anything. In reality, a Web service back end could potentially do anything, such as processing information through an Enterprise Java Bean (EJB), connecting to database, or even invoking another Web service.

  1. In the Package Explorer view, expand MyDemoWebServiceProject, right-click on Java Resources, and click New > Class.
  2. Enter demo as the package and DemoWebService as the file name.
  3. Make sure the public static void main (String[] args) box is not checked.
  4. Click Finish and the Java editor opens.
  5. Add the code shown in bold in Listing 2 and modify the code as follows:

    Listing 2. Code for the demo.DemoWebService class
                                
    package demo;
       public class DemoWebService {
    	
         public double getQuote (String symbol){
                                if (symbol.equals ("IBM")){
                                return 120;
                                }
                                return 0;
                                }
    }

  6. Click Save.

Creating a Web service

Creating a Web service is extremely simple. To create a Java Web service:

  1. Right-click on DemoWebService.java Java class > New > Other.
  2. In the New Wizard window, select the check box Show All Wizards to see all the available wizards for the capabilities that are not activated.
  3. Expand Web Services and select Web Service. Click Next.
  4. By default Bottom up Java bean Web Service should be selected as the Web service type.
  5. Click Finish. The Web Service wizard starts the WebSphere Application Server and deploys the generated Web service to the server. Now the Web service is already up and running.


    Figure 8. Creating a Web Service
    Figure 8: Creating a Web Service

    When the wizard is complete, you have successfully created your first Web service. You might see some new artifacts were created. They are DemoWebService_SEI.java and the DemoWebService.wsdl. Use the WSDL file in the next step when invoking this Web service.



    Figure 9. Demo Web service
    Figure 9: Demo Web Service


Creating a client Web project

Create another Web project that hosts the JSP client, which acts as a client to invoke the Web service.

  1. From the workbench, select File > New > Project.
  2. Select Web > Dynamic Web Project > Next. Select OK if prompted to enable Web Development capability.
  3. Type MyWebServiceClientProject as the project name.
  4. In the EAR Membership section, click New to create a new EAR Application Project. Type MyWebServiceClientProjectEAR as the project name. Click Finish. Select No if prompted to switch to the J2EE perspective.
  5. Click Finish. Select Yes if prompted to switch to the Web perspective.

Copying the WSDL file from the host project

Copy the WSDL file from the MyDemoWebServiceProject to your client Web project.

  1. In the Project Explorer view, expand MyDemoWebServiceProject > WebContent > WEB-INF.
  2. Copy and paste the wsdl folder from the MyDemoWebServiceProject / WebContent / WEB-INF location to the MyWebServiceClientProject / WebContent / WEB-INF location.

    Make sure the wsdl file is copied over to MyWebServiceClientProject under the WebContent folder.



    Figure 10. Copying the wsdl file to MyWebServiceClientProject
    Figure 10: Copy wsdl file to MyWebServiceClientProject

In "real-life" work settings, if you are invoking an external Web service, request the WSDL file from the service provider.


Generating a proxy

Generate a Web service client from the WSDL file that you copied previously.

  1. Expand the wsdl folder under MyWebServiceClientProject/META-INF folder. Right-click DemoWebService.wsdl > New > Other.
  2. In the New Wizard window, select the Show All Wizards check box to see all the available wizards, including those for the capabilities not currently activated.
  3. Expand Web Services and select Web Service Client as shown below. Click Next. Click OK if you are asked to enable the Web Services Development capability.


    Figure 11. Creating a Web service client
    Figure 11: Create a Web Service Client

  4. In the resulting window, make sure Java proxy is selected. Click Finish. The wizard creates a Java proxy, which is simply a Java class that can invoke the Web service.
  5. Click Yes if you are asked to replace the web.xml file. If you get an exception, it is normal and safe to ignore it.
  6. The proxy named DemoWebServiceProxy is generated in the Java Resources folder. In the next step, create a JSP file to use this proxy class to call the Web service.

Creating a JSP file

  1. In the Project Explorer view, right-click MyWebServiceClientProject > New > Web Page.
  2. Enter MyWebServiceClient as the file name and make sure JSP is selected as the template. Click Finish.
  3. Switch to the Source view of the JSP editor and enter the code shown in bold as follows:

    Listing 3. Code for MyWebServiceClient.jsp
                                
    <BODY>
    <%
                                double quote =0;
                                try {
                                demo.DemoWebServiceProxy proxy = 
                                    new demo.DemoWebServiceProxy();
                                quote = proxy.getQuote("IBM");
                                }catch (Exception e) {
                                e.printStackTrace();
                                }
                                %>
                                <%="Quote: " + quote %>
    </BODY>
    

  4. Save the file by pressing Ctrl-S.

Running the JSP file in a browser

  1. Start the server from the Server view if it is not already started, then run the JSP file using the URL: http://localhost:9080/MyWebServiceClientProject/MyWebServiceClient.jsp. Alternatively, right-click the JSP file and select Run As > Run on Server.
  2. In the Project Explorer view, right-click the MyWebServiceClient.jsp file and select Run As > Run on Server.
  3. Select an existing server and click Finish to automatically add the project to the server and run the JSP file in the internal browser.


    Figure 12. Running the MyCurrencyClient.jsp
    Figure 12: Running the MyCurrencyClient.jsp

  4. The JSP file calls the proxy, which in turn invokes the Web service. In this exercise, both the Web service and the client run on the same WebSphere Application Server, but they can also run on different servers. Because the client only needs the WSDL file to invoke the Web service, it doesn't matter where it is hosted.


    Figure 13. Running the MyWebServiceClient.jsp
    Figure 13: Running the MyWebServiceClient.jsp

5 of 9 | Previous | Next

Comments



static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Rational, Java technology
ArticleID=188449
TutorialTitle=Hello World (updated): Rational Application Developer V7
publish-date=01232007
author1-email=mailto:jcyfung@ca.ibm.com
author1-email-cc=