EGEE Introduction to Webservices Course Practicle ------------------------------------------------- This practical will show you how to write a client for a pre-defined webservice, in this case a "Quote of the day" service, located at http://webservices.codingtheweb.com/bin/qotd. Step 0) Create a directory for you to work in. Call it "client", and make it in your user directory. Step 1) WSDL Get the WSDL from http://www.xmethods.com/ or, if that doesn't work a we can supply a local copy. Copy this into your "client" directory. Step 2) Generate the glue components First, create a file called config.xml, and add the following lines to it: This is the configuration file for the wscompile utility that will generate all of the glue code. The file tells wscompile which WSDL file to use, and what package name we would like the glue code placed in. Next, we run the wscompile utility itself: wscompile -gen:client -keep -d . config.xml This gets wscompile to generate the client code (-gen:client), keep the generated source code so we can examine it later (-keep), work in the current directory (-d .), using the configuration described in config.xml. This will create a "qotd" directory, which will contain a number of .class and .java files that allow us to talk to the webservice. Step 3) Write the client Create a new file called qotdClient.java in your "client" directory. First, we must import the various RPC libraries and glue code that we have just generated: // Standard libraries import javax.xml.rpc.Service; import javax.xml.rpc.Stub; // Our glue code import qotd.Qotd_Impl; import qotd.QotdPortType; Next, we create our client class and give it two methods: public class qotdClient { public static String getQuote() throws Exception { } public static void main(String[] args) { } } Now, we'll write the getQuote function so that it calls a remote function via webservices. Add the following to the getQuote function: Qotd_Impl service = new Qotd_Impl(); QotdPortType port = service.getQotdPort(); The Qotd_Impl class can be seen as the "class" of the webservice, and QotdPortType can be seen as an "instance" of that class. Now we call the remote "getQuote" method, as defined in our WSDL, and return it to our caller: String quote = port.getQuote(); return (quote); And that is all that our getQuote client method needs. Now, we'll write some code in our "main" method so that it calls our getQuote method: try{ String quote = getQuote(); System.out.println(quote); } catch (Exception e) {e.printStackTrace();} That should now be all of the code that is required. Compile the code with: javac qotdClient.java Step 4) Run the Client Try the client out with: java qotdClient If the client seems to hang, it could be because the remote service is down (welcome to the world of webservices! ;-)