A Hello World Web Service Using Metro, Tomcat and Eclipse

Publish date: 2011-05-17

If you liked this article, go to www.descriptor.com and learn about our world-class training!

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.

Prerequisites

Installing Tomcat

Download the latest Version 6 of Tomcat from http://tomcat.apache.org/download-60.cgi, then unzip it to a directory on your hard disk. We will refer to that directory as the {Tomcat Installation Directory}.

Installing Eclipse

Download the latest Eclipse (we used Eclipse Helios) from http://www.eclipse.org/downloads/. Be sure to download the Eclipse IDE for Java EE Developers, not the Eclipse IDE for Java Developers. Unzip it to a directory on your hard disk. We will refer to it as the {Eclipse Installation Directory}.

Installing Metro

Download the latest 2.1 version of Metro from http://metro.java.net/2.1/, then unzip it to a directory on your hard disk. We will refer to it as the {Metro Installation Directory}.

Configuring Tomcat with Metro

Next, you need to run an Ant script that configures Tomcat with the Metro libraries.

Open a command-prompt or terminal window and change to the {Metro Installation Directory}. Then run the following Ant script, substituting the correct directory for the {Tomcat Installation Directory}:

 ant -Dtomcat.home={Tomcat Installation Directory} -f metro-on-tomcat.xml install    
    

The script should execute with no errors.

Creating the Web Service in Eclipse

Start Eclipse from the {Eclipse Installation Directory}, then choose Window - Open Perspective - Other - Web to switch to the Web perspective.

Choose File - New - Dynamic Web Project to create a new Dynamic Web Project named TestMetroWeb, using all of the defaults in the wizard. Ignore any request to switch to the JEE Perspective.

Right-click on the new project and choose New - Class to create a new class named MyService in the sample package, then complete the class so it looks like:

package sample;

import javax.jws.WebService;

@WebService
public class MyService
{
  public String hello(String name)
  {
    return "Hello, " + name;
  }
}

Open the WEB-INF/web.xml deployment descriptor into the Eclipse XML editor, then add the following above the welcome-file element:

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

Create a new file in the WEB-INF folder named sun-jaxws.xml and complete it:

<?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>

Save all of the files and ensure that there are no errors.

Deploying the Service to Tomcat

In Eclipse, right-click on the TestMetroWeb project and choose Export - WAR File and export TestMetroWeb.war to the {Tomcat Installation Directory/webapps directory.

Starting the Service

In a command-prompt or terminal window, change to the {Tomcat Installation Directory} and start Tomcat (on Windows, use catalina.bat instead of catalina.sh):

bin/catalina.sh run  
  

Tomcat should start and display messages similar to the following:

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  
  

Testing that the Service is Running

Open a Web browser, and navigate to http://localhost:8080/TestMetroWeb/myservice. 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.

What's Next?

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 Developing Web Service Client article at http://java.dzone.com/articles/jax-ws-hello-world.