<article xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:noNamespaceSchemaLocation='file:///Users/jbarnum/source/DescriptorHTML5Web/WebContent/articles/xsl/article.xsd'>

  <title>A Hello World Web Service Using Metro, Tomcat and Eclipse</title>       
  <short-title>Metro, Tomcat Eclipse Web Service</short-title>
  <creation-date>2011-05-17</creation-date>
  <version>1.0</version>
  <author>Joel Barnum</author>                   
  <organization>Descriptor Systems</organization>
  <description>
    This article describes the steps to install, configure and run
    a "Hello, World" Web service using Apache Tomcat 6.x, Metro 2.1 and
    Eclipse Helios using Java 6.
  </description>
  <prereqs>
    <ul>
      <li>Java 6 JRE</li>
      <li>Apache Ant 1.7 or later, with its "bin" directory installed into
      the system PATH</li>
    </ul>
  </prereqs>
  <keywords>Metro Tomcat Eclipse Web Service</keywords>
  <body>
    <h2>Installing Tomcat</h2>
    <p>
      Download the latest Version 6 of Tomcat from 
      <a href="http://tomcat.apache.org/download-60.cgi">http://tomcat.apache.org/download-60.cgi</a>,
      then unzip it to a directory on your hard disk. We will refer to that directory
      as the <b>{Tomcat Installation Directory}</b>.
    </p>
    <h2>Installing Eclipse</h2>
    <p>
      Download the latest Eclipse (we used Eclipse Helios) from 
      <a href="http://www.eclipse.org/downloads/">http://www.eclipse.org/downloads/</a>.
      Be sure to download the <b>Eclipse IDE for Java EE Developers</b>, not
      the <i>Eclipse IDE for Java Developers</i>. Unzip it to a directory on your
      hard disk. We will refer to it as the <b>{Eclipse Installation Directory}</b>.
    </p>
    <h2>Installing Metro</h2>
    <p>
      Download the latest 2.1 version of Metro from 
      <a href="http://metro.java.net/2.1/">http://metro.java.net/2.1/</a>, then 
      unzip it to a directory on your hard disk. We will refer to it as the
      <b>{Metro Installation Directory}</b>.
    </p>
    <h2>Configuring Tomcat with Metro</h2>
    <p>
      Next, you need to run an Ant script that configures Tomcat with
      the Metro libraries.
    </p>
    <p>
      Open a command-prompt or terminal window and change to the <b>{Metro Installation
      Directory}</b>. Then run the following Ant script, substituting the correct
      directory for the <b>{Tomcat Installation Directory}</b>:
    </p>
    <pre>
 ant -Dtomcat.home={Tomcat Installation Directory} -f metro-on-tomcat.xml install    
    </pre>
    <p>
      The script should execute with no errors.
    </p>
    <h2>Creating the Web Service in Eclipse</h2>
    <p>
      Start Eclipse from the <b>{Eclipse Installation Directory}</b>, then choose
      <i>Window - Open Perspective - Other - Web</i> to switch to the Web
      perspective.
    </p>
    <p>
      Choose <i>File - New - Dynamic Web Project</i> to
      create a new <b>Dynamic Web Project</b> named <b>TestMetroWeb</b>, using
      all of the defaults in the wizard. Ignore any request to switch to the
      JEE Perspective.
    </p>
    <p>
      Right-click on the new project and choose <i>New - Class</i> to create
      a new class named <b>MyService</b> in the <b>sample</b> package, then
      complete the class so it looks like:
    </p>
    <codelisting>package sample;

import javax.jws.WebService;

@WebService
public class MyService
{
  public String hello(String name)
  {
    return "Hello, " + name;
  }
}</codelisting>
   <p>
     Open the <b>WEB-INF/web.xml</b> deployment descriptor into the Eclipse
     XML editor, then add the following above the <i>welcome-file</i> element:
   </p>
   <codelisting><![CDATA[<listener>
  <listener-class>
   com.sun.xml.ws.transport.http.servlet.WSServletContextListener
  </listener-class>
</listener>
<servlet>
  <servlet-name>WebServicePort</servlet-name>
  <servlet-class>
    com.sun.xml.ws.transport.http.servlet.WSServlet
   </servlet-class>
  <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
  <servlet-name>WebServicePort</servlet-name>
  <url-pattern>/myservice</url-pattern>
</servlet-mapping>]]></codelisting>
    <p>
      Create a new file in the <b>WEB-INF</b> folder named
      <b>sun-jaxws.xml</b> and complete it:
    </p>
    <codelisting><![CDATA[<?xml version="1.0" encoding="UTF-8"?>
<endpoints version="2.0" 
   xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime">
  <endpoint implementation="sample.MyService" name="MyService" 
     url-pattern="/myservice"/>
</endpoints>]]></codelisting>
  <p>
    Save all of the files and ensure that there are no errors.
  </p>
  <h2>Deploying the Service to Tomcat</h2>
  <p>
    In Eclipse, right-click on the TestMetroWeb project and choose
    <i>Export - WAR File</i> and export TestMetroWeb.war to the 
    <b>{Tomcat Installation Directory/webapps</b> directory.
  </p>
  <h2>Starting the Service</h2>
  <p>
    In a command-prompt or terminal window, change to the <b>{Tomcat
    Installation Directory}</b> and start Tomcat (on Windows, use
    <b>catalina.bat</b> instead of <i>catalina.sh</i>):
  </p>
  <pre>
bin/catalina.sh run  
  </pre>
  <p>
    Tomcat should start and display messages similar to the following:
  </p>
  <pre>
INFO: WSSERVLET12: JAX-WS context listener initializing
May 16, 2011 6:25:17 AM com.sun.xml.ws.server.MonitorBase createRoot
INFO: Metro monitoring rootname successfully set to: ....
May 16, 2011 6:25:17 AM com.sun.xml.ws.transport.http.servlet.WSServletDelegate
INFO: WSSERVLET14: JAX-WS servlet initializing  
  </pre>
  <h2>Testing that the Service is Running</h2>
  <p>
    Open a Web browser, and navigate to <b>http://localhost:8080/TestMetroWeb/myservice</b>.
    You should see a Web page describing the service, and should be able to 
    click on the WSDL link to see the service's description.
  </p>
  <h2>What's Next?</h2>
  <p>
    You should now be able to write a client in Java or some other language
    that invokes the service. For a Java client, see the <i>Developing Web
    Service Client</i> article at 
    <a href="http://java.dzone.com/articles/jax-ws-hello-world">http://java.dzone.com/articles/jax-ws-hello-world</a>.
  </p>
  </body>
</article>
