Web Services Example 1

After creating a successful environment for web services in Eclipse, it’s time to take things to another level by writing a service that actually does something. Here, we create a web service (still a simple one) which takes the salaries of employees, including allowances, and calculates their taxes. The client sends an XML document and receives the same document back except the taxes are now filled in. Later in our SOA course, we’ll see how these documents can be encrypted and signed to ensure integrity and privacy. Anyway, let’s get on with the web service.

First, follow the tutorial in which we set up the environment for web services in eclipse. In that tutorial, you write a class on the service provider side that has two function — getNums and getNums2. In that step, create a class file with one function. The code follows:

package edu.serg.samples;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class ProcessTax {
    public Document processTax(Document employees) {
        try {
            NodeList nl = employees.getFirstChild().getChildNodes();

            for (int i = 0; i < nl.getLength(); i++)
                calculateTax(employees, nl.item(i));
        } catch (Exception e) {
        return employees;

    private void calculateTax(Document emps, Node emp) {
        try {
            if (emp == null)
            NodeList nl = emp.getChildNodes();
            if (nl == null)

            float total = 0;
            for (int i = 0; i < nl.getLength(); i++) {
                Node nn = nl.item(i);
                Node nc = nn.getFirstChild();
                if (nn != null) {
                    if (nn.getNodeName().equals("basic")
                            || nn.getNodeName().equals("allowance"))
                        total += Integer.parseInt(nc.getNodeValue());
                    else if (nn.getNodeName().equals("tax")) {
                        if (nc != null)
                            nc.setNodeValue(Float.toString(total * 12 / 100));
                        else {

                            nc = emps.createTextNode(Float
                                    .toString(total * 12 / 100));
        } catch (Exception e) {

Now, follow the rest of the tutorial and create a client. At the very end of the tutorial, you create a client SOATestClient. Here, we create a TaxClient:

package edu.serg.samples;

import java.net.URL;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;

public class TaxClient {

     * @param args
    public static void main(String[] args) throws Exception {
        // create the source document from file
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse("src/edu/serg/samples/employees.xml");

        // call the web service to process the file
        ProcessTaxSoapBindingStub srv = new ProcessTaxSoapBindingStub(new URL(
                new ProcessTaxServiceLocator());
        Document res = srv.processTax(doc);

        // output the new file
        DOMSource src = new DOMSource(res);
        StreamResult sr = new StreamResult(System.out);
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer t = tf.newTransformer();

        t.transform(src, sr);


This code may look daunting if you’ve only been working with DOM for a week but you need to figure out what the different classes do. The rest is pretty straight forward. Note that you may need to change the xml file path and end point (URL) of your web service on your system. These settings are for my system.

The employees.xml file which we’ve used in the code above is this:

<?xml version="1.0" encoding="UTF-8"?>
        <name>employee 1</name>
        <name>employee 1</name>
        <name>employee 1</name>

1 Comment

  1. The individual who is haunting the location may have died unexpectedly and wasn’t ready to.
    Now don’t get me wrong, I love my son dearly, but as any parent can testify, there are
    moments that you wonder if a full on straight jacket isn’t
    call for. Ooty (Udhagamandalam) is popularly known as
    the “Queen of hill stations” among the tourist

Leave a Reply

Your email address will not be published.


© 2016 recluze

Theme by Anders NorenUp ↑