1) Setup directory structure
----------------------------
In your home directory, create a "tutorial" folder. Inside this folder, create
a "service" folder. This will be the base folder for the remainder of this
tutorial, and all relative paths will assume that you are somewhere within
this folder.
Create the following directories:
build/
src/
war/
2) Write interface and implementation .java files
-------------------------------------------------
Create a "qotd" folder inside your "src/" directory.
Firstly, we have to create a Java interface class for the service. Open a new
text file, called "Qotd.java". The first thing we have to do is specify the
package and import the classes that we need:
package qotd; // This is just to keep the code neat
import java.rmi.Remote; // This class enables remote method invokation
import java.rmi.RemoreException; // This is for handling remote exceptions
Now we must define the interface itself. For our example service, this is very
simple indeed:
public interface Qotd extends Remote {
public String getQuote() throws RemoteException;
}
That's it for the interface. Save this file.
Now, we must write the class that implements the functionality that we have
just described in the interface. Open another new text file, called
"Qotd_Impl.java". This file is also very short:
package qotd; // Make sure we're in the same package as our interface
public class Qotd_Impl implements Qotd {
private static String quote = "Your quote";
public String getQuote() {
return this.quote;
}
}
Save this file. That is all of the programming that we need to do for this
(very trivial) webservice.
3) Create the WAR structure
---------------------------
The various files that we have created so far now have to be bundled up into a
WAR file, which is a JAR file with a specific folder structure so that
web application servers can deploy the software automatically.
First, create a "/service/war" folder. Then, create a "WEB-INF" subfolder
(note the capitalisation - very important).
- WEB-INF/
- WEB-INF/classes/
- WEB-INF/classes/qotd
4) Compile the code
-------------------
Before you can compile any of the Java that you have written, the CLASSPATH
environment variable must be setup. Run the "classpath.bat" file located in
your tutorial base directory (one above the "service" folder). You will have
to do this for every command prompt window that you are using to develop or
deploy your webservice.
Compile the two .java files by running this command in the "/src" folder:
javac -d ../war/WEB-INF/classes/qotd qotd/*.java
The "-d" flag and path tells the compiler to place the compiled ".class" files
in the appropriate directory of our war file structure.
5) Write the "WEB-INF/jaxrpc-ri.xml" file
-----------------------------------------
The jaxrpc-ri.xml file tells the wsdeploy utility how to generate the various
pieces of code and configuration information in order for the portable WAR to
be converted and deployed on a specific web application server.
Open a new text file called jaxrpc-ri.xml under your WEB-INF folder. Enter the
following XML:
This tells wsdeploy about the namespaces that we want to use, and defines a
webservice endpoint in terms of the Java interface that it exposes, and an
endpoint mapping which tells the application server how to translate the
endpoint into a URL.
6) Write the "WEB-INF/web.xml" file
-----------------------------------
Next we have to write the web.xml file which is used by the application server
to setup our webservice.
Open a new text file under your WEB-INF folder, called "web.xml", and enter
the following XML:
Quote of the day service
Returns some philosophical dribble
This simply tells the application server what to call our service, and the
description of what it does, both in a user-readable form (this is used
primarily for the management interface).
7) Package it all up into a portable WAR file
---------------------------------------------
Use this command line in your "war/" directory:
jar cfv temp.tar *
This packages up all of the files and directory structure into a portable WAR
file. You should be able to migrate this file to other webservices
environments if necessary.
8) Make the WAR file deployable
-------------------------------
You should now run the wsdeploy command in your "/war" folder:
wsdeploy -o Qotd.war temp.war
This tool converts the portable WAR file into an implementation specific WAR
file by generating all of the tie classes and a WSDL file from your Java
interface class.
9) Deploy your webservice
------------------------------------------------------------
The final step is to copy your deployable WAR file, Qotd.war, to the "webapps"
folder of your JWSDP Tomcat installation. This should automatically deploy the
webservice contained in the WAR file into the server. You will have to restart
the server in order for these changes to take effect. You can inspect your
service by going to "http://localhost:8080/manager/html". Your service should
be listed here.
If you now go to "http://localhost:8080/Qotd/qotd", you should be able to
inspect the services WSDL file. You should save the WSDL file so that you can
use it to write your client software in the next tutorial.