Learning Center
Plans & pricing Sign in
Sign Out

1 Homework Building a Web Servic


									Homework: Building a Web Service Client for a Stock Quote Service

1. Objectives

   •   Become familiar with Web Services technologies;
   •   Use a combination of HTML, CSS, DOM, XML and Java Servlets;
   •   Provide a web interface to obtaining real-time stock quotes from an online stock
       quotes web service.

2. Background

2.1 Web Services

A Web service is any piece of software that makes itself available over the Internet and
uses a standardized XML messaging system. XML is used to encode all communications
to a Web service. Communication is performed via the Simple Object Access Protocol
(SOAP). A Web Service can have a public interface, defined in a common XML
grammar. The interface describes all the methods available to clients and specifies the
signature for each method. Currently, interface definition is accomplished via the Web
Service Description Language (WSDL).

2.2 Stock Quotes Web Service is a web service of InveShare, providing real-time stock quotes and stock
indexes. Using the Stock Quotes Web Service, web developers can obtain
real-time stock quotes and market indexes and embed them into their web sites. Access to
the Stock Quotes Web Service can be accomplished in any one of three ways:

   1. Using direct SOAP message communication with the Stock Quotes Web Service;
   2. Using Java code produced from the WSDL definition file, which describes all
      supported SOAP methods;
   3. Using URL-based REST request-response transactions.

For this exercise, only method 2 above will be acceptable. The WSDL of the service is available at the URL:

and is also downloadable from the class website at the URL:

The web service performs 4 operations:

/* Enter symbols, separated by space, Quotes delayed in 20 minutes. */
any GetQuotes(string symbols)

/* Enter one symbol, quote delayed in 20 minutes. */
any GetQuote(string symbol)

/* Get Dow, Nasdaq, S&P500 index. */
any GetMarketIndex()

/* Enter symbols, separated by space, Real-Time ECN quote. */
any GetECNQuotes(string symbols)

The operations are documented at:

3. Description of the Exercise

In this homework assignment you will build a Web Services client that queries the Stock Quotes Web Service and returns the 15-minute delayed stock quote
for any stock listed on one of the major stock exchanges, using the function GetQuote().

Web Service Client for Stock Market Quotes

You will install Apache Axis 1.1 in your Tomcat servlet-container and will use
WSDL2Java (included with Axis) to generate Java stubs, skeleton and data types from
the WSDL at stockquotes.wsdl. You will then implement a Java Servlet that uses the
GetQuote()operation to return the 15-minute delayed stock quote of a user-selected
stock. You should provide all of the following components:

   1. A top level Start Page containing the following components:

           1. A text box which will contain a user-selected stock symbol;
           2. A “Get Stock Quote” button;

   2. A Response Page displaying an HTML table containing a translation of all the
      components returned in the XML-based result.

For example, if the XML result returned by the web service is as follows:

  <Company>APPLE COMPUTER</Company>
  <Time>12:59PM ET</Time>
    <img width="10" height="14" border="0"
    alt="Up"> <b style="color:#008800;">0.56 (0.85%)</b>
  <Bid>66.24<small> x 400</small></Bid>

  <Ask>66.25<small> x 500</small></Ask>
  <DayRange>65.50 - 66.60</DayRange>
  <YearRange>33.11 - 86.40</YearRange>
  <DivYield>N/A (N/A)</DivYield>
    <a href=""></a>
Apple Computer, Inc. engages in the design, manufacture, and marketing
of personal computers [.. other information omitted ..] The company is
headquartered in Cupertino, California.

The servlet should output the HTML table on the following page:

The HTML that produced the table above is available at:

Notice that HTML contained inside XML tags has been “embedded” in the table result
(see Price, Change and WebSite).

4. Implementation Hints

To do the homework, you must make use of Java Servlets. It is expected you will use
Jakarta-Tomcat. You may either use your own account with the server running on
csci571, csci571b or some other machine that is Internet accessible.

To implement the homework, you should use HTML to design the Top-level page. You
can also design one Java Servlet class, which is responsible for interfacing with the
GetQuote() Web Services operation and displaying its result in table format as outlined

   1. Step 1: Create the Web Service stubs and interfaces using Axis WSDL2Java

         If you will be installing your servlets using the built-in Tomcat examples
         directories, you should execute this step from the webapps/examples/WEB-
         INF/classes      subdirectory, after having copied the WSDL file,
         stockquotes.wsdl, in the same directory. You should invoke WSDL2Java as

         java -classpath ${CLASSPATH} org.apache.axis.wsdl.WSDL2Java -D

         After you execute WSDL2Java, the following set of files will be created in the
         subdirectory com/invesbot/ws:


2. Step 2: Compile Axis WSDL stub files

     You must move up to the webapps/examples/WEB-INF/classes subdirectory
     to compile all the stubs produced in Step 1 using javac. For example:

     javac com/invesbot/ws/

     Corresponding .class files will be generated for each of the stubs and

3. Step 3: Writing your Servlet to interface with the Web Service

     You     should     now      create    your    servlet    in    com/invesbot/ws,,      for example, which invokes methods from the produced
     interfaces and stubs. To help you out, we have created a Javadoc documentation
     database of all the stubs and interfaces from Step 2, available at:

     The Java Servlet should do all of the following:

     a. Instantiate a service “locator” using StockQuotesLocator() ;
     b. Set the service “port” using getStockQuotesSoap();
     c. Invoke the web service operation, such as calling getQuote(stock) and
        retrieving the result.

     See the provided Javadocs for details on the parameters and return values of the
     above method.

4. Step 4: Use the Java Servlet to process the XML result

     All operations of the web service define the results in the WSDL as follows:

                  <s:element minOccurs="0" maxOccurs="1"
                    <s:complexType mixed="true">

     By using a <sequence> of <any>, the operations can return results consisting
     of arbitrary XML fragments. You can call the get_any() method of the result
     object to extract the entire XML and convert it to an array of

     org.apache.axis.message.MessageElement.       For example, you could output
     the entire XML document that was returned by executing these lines:

     org.apache.axis.message.MessageElement[] msg = result.get_any();

5. Step 5: Use Java Servlets to convert the XML into HTML

     You can then create one more servlet,, called when
     clicking the button from the top-level HTML form below:

     The servlet will invoke the previously built servlet, retrieve the XML output
     and parse and “translate” the data; converting the XML data into HTML format,
     as described in section 3. You can use several techniques to do this: use JAXM,
     JAXP, or JDOM. One technique that we recommend is using JAXP and TraX
     (Transformation for XML) and an XSLT style sheet to produce the HTML

     Apache Xalan-Java is an XSLT processor for transforming XML documents
     into HTML, text, or other XML document types. Xalan-Java implements the
     javax.xml.transform interface in Java API for XML Processing (JAXP) 1.3, and
     includes the transformation API for XML (TrAX). Documentation and
     download instructions for obtaining Xalan-Java version 2.7 are available at:

     Important Note: The Java Servlets should handle exceptions such as
     MalformedURLException,              IOException,            Exception,
     javax.xml.rpc.ServiceException,         java.rmi.RemoteException   and
     possibly other exceptions, such as ParserConfigurationException,
     depending on your choice of XML Parser.

   6. Step 6: Deploy the Application

         See section “6, Deployment Structure” below for the set of steps to follow to
         deploy your servlet(s).

The above is just one way to implement your program. You can try your own method, as
long as it works and meets our requirement of using Web Services and SOAP.

A Reference implementation is available at:

Important Note: implementing this homework using the Representational State Transfer
(REST) web-based interface that uses XML and HTTP without the extra abstractions of
the Web Services SOAP protocol is not acceptable.

5. Prerequisites

This homework requires the use of all of the following components:

   1. A servlet-based web server, Tomcat 4.1.27. Instructions on how to load Tomcat
      4.1.27 can be found here: http://www- A tar version of Tomcat
      4.1.27 can be found here:
   2. The Java Servlet library, which has functionality similar to Perl’s LWP library, to
      perform HTTP transactions using methods such as doGet() or doPost() from
   3. A Java XML parser library. You may use the JDOM 1.0, an object model that
      uses XML parsers to build documents, available in the Download section of the
      class website. Additional information on JDOM is available at You may also use JAXP, the Java API for XML Parsing,
      version 1.1, included in the Java JDK 1.4 (import javax.xml.parsers.*) and
      documented at: A good tutorial on
      JAXP is available at
   4. A SOAP engine and tools such as Apache Axis. A tar version of Apache Axis 1.1
      can be found here:
      Instructions on how to properly install and setup Axis are included in the
      documentation. Additional documentation and setup for this project can be found
   5. A WSDL to Java translator to create Java stubs. The WSDL2Java tool is included
      in Apache Axis 1.1.

6. Deployment Structure

To write your own Java Servlets program using Tomcat 4.1.27, you need to:

   1. Successfully install Tomcat 4.1.27 on your machine.
   2. Go to $CATALINA_HOME/webapps/examples subdirectory.
   3. Place the HTML, CSS and JavaScript (.js) files in the Tomcat servlets
   4. Place your Java Servlets class files (.class) in the /WEB_INF/classes/
      com/invesbot/ws subdirectory.
   5. Place the Axis libraries (axis.jar, commons-logging.jar, log4j-1.2.8.jar,
      wsdl4j.jar, commons-discovery.jar, jaxrpc.jar, saaj.jar) in the
      /WEB-INF/lib subdirectory. You may have to create this directory, if it does not
      already exist.
   6. Add appropriate sections to the WEB-INF/web.xml file, as in:

       <!-- Stock Quote Servlet -->
           <display-name>Stock Quotes Application</display-name>

   7. To avoid UTFDataFormatException during file IO operation, you have to use
      JDK 1.4 or later for Tomcat. In the .cshrc file under your home directory, add
      the entries:

       setenv JAVA_HOME /usr/j2se

       setenv PATH /usr/j2se/bin:${PATH}

   8. Before you issue a request to your Java Servlet file, you need to compile it. You
      might need a Java Servlet class to compile your Java code, so open the .cshrc file,
      and add “ /home/scf-22/csci571/servlet_classfiles/j2ee.jar ” to your
      CLASSPATH variable:

       setenv CLASSPATH /home/scf-

   9. Then run “source .cshrc”, and compile your 4 Java classes, as in:

       javac com/invesbot/ws/


       Important Note: since the Axis stubs and interfaces are created as part of a Java
       “package”, you will have to include the following import at the beginning of your
       servlet above:


   10. Restart your Tomcat server and access your servlets from your HTML code. The
      path your Servlet file is:

   http://server_name:port/examples/servlet/servlet-name,             as in:


7. Material You Need to Submit

On your course homework page, your link for this homework should go to a page that
includes your HTML program (the Top-level page mentioned above). You should submit
all source code files including HTML (.html), Cascading Style Sheets (.CSS), JavaScript
(.js), Java Servlets (.java) and a README file electronically to the csci571 account so
that it can be graded and compared to all other students' code via the MOSS code
comparison tool.


To top