> developer > dnu > courses > Web Services page 17
Web Services
April 2003
DeveloperNet University Course
Reader Rating    from ratings rate this article
View an eBook Version of this course - LARGE FILE! Send this page to a friend

Exercise 7: Build a Hello World Producer Web Service

  1. Create a java interface called Hello within a package called hello. This interface will define the object and method that we will provide as part of our Web Service. It will be used to create the WSDL file that describes our service, along with the skeleton and stubs to implement and test.

    1. From the File menu, select New.

    2. From the New File window, select Java file and make sure the Use Wizard box is checked. Click OK.

    3. Enter Hello as the class name, Remote as the Base class, and select Interface. Click Next.

    4. Add the import statements for the remote interface:


import  java.rmi.Remote;
import java.rmi.RemoteException;

    1. In the next Java Class wizard window:

    • Add to open project: Services

    • Base Directory: C:\WebServices\src

    • Package: hello

    • Select Add the files to the archive with this prefix: WEB-INF\classes

    1. Click Finish. A message will appear that states the java class was created. Click Ok.

    2. Add a method sayHello() that returns a String.

    String sayHello() throws RemoteException;
  1. Save

  2. Build and archive.

  1. We will now create the actual Web Service using the Workbench wizard to create the required xml (WSDL) and java (client, skeleton, and stub) files for us.

    1. From the File menu, select New.

    2. Click on the Web Services tab and choose New Web Service and click Ok.

    3. In the Web Service Wizard window, select interface hello.Hello. Click Next.

    4. In the next window, uncheck Generate tie-based skeletons. We will be using inheritance approach instead of the Tie approach (where we implement an interface and use generated class for the server).

    5. Change Specify the binding for the service to read http://localhost/WebServices/services/Hello. (Note: "WebServices" is the top level directory name, "services" is the WAR file name. You may need to adjust your path accordingly.

    6. Click Finish.

  2. The workbench created the stubs and skeletons source files for you. Now you need to compile them.

    1. Before we compile, take a look at C:\WebServices\src\hello\Hello_CLIENT.java. Compare to the Client file on page 3-19 in the student book. The important steps of finding the service and invoking the method on the service is split between two methods in our client vs. all in the main method in the book example.

    2. Un-comment the println method.

    3. System.out.println("Test Result = " + remote.sayHello());

    4. Save

    5. Compile by running the Build and archive command.

  3. Implement the remote interface by creating a java class that extends HelloStub.

    We need to implement a class that will actually perform the method request when a Web Service request is received by the application server.
  1. From the File menu, select New. Choose Java File and make sure the Use wizard field is checked.

  2. Enter HelloService as the class name. Enter base class as Hello_Stub. Click Next.

  3. In the next Java Class wizard window:

  • Add to open project: Services

  • Base Directory: C:\WebServices\src

  • Package: hello

  • Select Add the files to the archive with this prefix: WEB-INF\classes. Click Finish. Click OK when the file is created.

  1. Add the imports:

    import java.rmi.RemoteException;
  1. Implement the sayHello() method:


public String sayHello() throws RemoteException
{
return "Hello World!";
}

    1. Save

    2. Build and archive

  1. Update the deployment descriptor. Our Service will be deployed as J2EE Web application. In this step we will define the Servlet that will provide our service when a request is received.

    1. The deployment descriptor file web.xml is located in the C:\WebService\WEB-INF directory. Open it by double clicking on it in the navigation pane.

    2. When prompted to build the project, click yes.

    3. The visual designer should be displayed in the right pane. If it is not, select the Descriptor tab on the bottom of the editor pane.

    4. Right-click on Servlets and select Add.

    5. Right click on the generated Untitled and select Properties.

    6. In the Properties inspector, enter the following:

    • Servlet name: HelloService

    • Select Type: Servlet

    • Type in Servlet class: hello.HelloService

    1. Close the window.

    2. Right-click on Servlet Mapping and select Add.

    3. Right click on the generated Untitled and select Properties.

    4. In the Properties inspector, enter the following:

    • Servlet name: HelloService

    • URL pattern: /HelloServlet name: HelloService

    1. Close the window.

    2. Save the web.xml file by clicking the Save button.

    3. Build and archive.

  2. Create a Deployment Plan. We are deploying to the SilverStream J2EE Server and we need to a create a plan to deploy our Web Application.

    1. From the File menu, select New, click on the Deployment tab and choose SilverStream Deployment Plan.

    2. Select your project name and server version, click OK.

    3. Right-click on Web Archive and select Properties.

    4. In the Deployment Plan Properties inspector, enter the following:

    • Select True for Enabled.

    • Select LocalWebServices as the server profile.

    • Deployed object name should be services

    1. Save as MyPlan.xml under the C:\WebService\WEB-INF directory.

  3. Deploy

    1. From the Project menu, select Deploy Archive.

  4. Test the Client

    1. From within Workbench in the Project menu use the Run Web Service Client Class option.

    2. Select hello.Hello_CLIENT. Change the timeout from 5 to 20. Click Run.

    3. You should be able to see: Test Result = Hello World!

Source: Hello.java


package hello;

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface Hello extends Remote {
String sayHello() throws RemoteException;
}

Source: HelloCLIENT.java


package hello;

import javax.naming.*;

public class HelloCLIENT
{
public void process(String[] args) throws Exception
{
Hello remote = getRemote(args);
System.out.println("Test Result = " + remote.sayHello());
}

public Hello getRemote(String[] args) throws NamingException
{
String lookup = "xmlrpc:soap:hello.Hello";
InitialContext ctx = new InitialContext();
return (Hello)ctx.lookup(lookup);
}

public static void main(String[] args)
{
try
{
Hello_CLIENT client = new Hello_CLIENT();
client.process(args);
}
catch (Exception _e)
{
System.out.println("*** Error Executing Generated Test Client ***");
_e.printStackTrace();
}
}
}

Source: HelloService.java


package hello;

import java.rmi.RemoteException;

public class HelloService extends _Hello_ServiceSkeleton
{
public String sayHello() throws RemoteException
{
return "Hello World!";
}
}

Source: web.xml


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application
2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
<web-app>
<display-name>My Web Services</display-name>
<description>Web Services</description>
<servlet>
<servlet-name>HelloService</servlet-name>
<servlet-class>hello.HelloService</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name> HelloService </servlet-name>
<url-pattern>/Hello</url-pattern>
</servlet-mapping>
</web-app>

Source: MyPlan.xml


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE warJarOptions PUBLIC "-//SilverStream Software, Inc.//DTD J2EE WAR
Deployment Plan//EN" "deploy_war.dtd">
<warJarOptions isObject="true">
<warJar isObject="true">
<version type="String">1.0</version>
<warJarName>D:\webServices\services.war</warJarName>
<isEnabled type="Boolean">True</isEnabled>
<sessionTimeout type="String">5</sessionTimeout>
<urls type="StringArray">
<el>services</el>
</urls>
<deployedObject type="String">services</deployedObject>
</warJar>
</warJarOptions>

Previous Contents Next
download sample file