Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

The data melting pot

Building a business-to-business application with XML

/*-- 

 Copyright (C) 2000 Brett McLaughlin. All rights reserved.
 
 Redistribution and use in source and binary forms, with or without modifica-
 tion, are permitted provided that the following conditions are met:
 
 1. Redistributions of source code must retain the above copyright notice,
    this list of conditions, and the following disclaimer.
 
 2. Redistributions in binary form must reproduce the above copyright notice,
    this list of conditions, the disclaimer that follows these conditions,
    and/or other materials provided with the distribution.
 
 3. Products derived from this software may not be called "Java and XML", nor may
    "Java and XML" appear in their name, without prior written permission from
    Brett McLaughlin (brett@newInstance.com).
 
 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
 JDOM PROJECT  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT, INDIRECT, 
 INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT 
 NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
 OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
 ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
 (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
 This software was originally created by Brett McLaughlin <brett@newInstance.com>. 
 For more  information on "Java and XML", please see <http://www.oreilly.com/catalog/javaxml/>
 or <http://www.newInstance.com>.
 
 */
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.InputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URL;
import java.util.Properties;
import javax.servlet.*;
import javax.servlet.http.*;

import com.oreilly.servlet.HttpMessage;

/**
 * <p>
 *  <b><code>GotCreditClient</code></b> demonstrates how
 *    to interact with the <code>{@link com.oreilly.xml.XmlSqlServlet}</code>.
 * </p>
 *
 * @author <a href="brett@newInstance.com">Brett McLaughlin</a>
 * @version 1.0
 */
public class GotCreditClient extends HttpServlet {

    /** URL to send HTTP request to */
    private static final String GOTCREDIT_URL = 
        "http://localhost:8080/javaxml/servlet/com.oreilly.xml.XmlSqlServlet";

    /** XML request */
    private static final String XML_SQL_REQUEST =
        "<?xml version=\"1.0\"?>\n" +
        "<query type=\"select\">" +
        " <resultField>FIRST_NAME</resultField>" +
        " <resultField>LAST_NAME</resultField>" +
        " <resultField>CREDITOR</resultField>" +
        " <resultField>BALANCE</resultField>" +
        " <table>CREDITORS c</table>" +
        " <table>DEBTORS d</table>" +
        " <table>DEBTS b</table>" +
        " <criteria>c.CREDITOR_ID = b.CREDITOR_ID</criteria>" +
        " <criteria>d.DEBTOR_ID = b.DEBTOR_ID</criteria>" +
        "</query>";

    public void service(HttpServletRequest req, HttpServletResponse res)
        throws ServletException, IOException {

        PrintWriter out = res.getWriter();
        res.setContentType("text/plain");

        try {
            URL url = new URL(GOTCREDIT_URL);
            HttpMessage msg = new HttpMessage(url);

            Properties p = new Properties();
            p.put("QUERY", XML_SQL_REQUEST);

            InputStream in = msg.sendPostMessage(p);

            DataInputStream result = 
                new DataInputStream(new BufferedInputStream(in));

            String line;
            while ((line = result.readLine()) != null) {
                out.println(line);
            }

            in.close();
            out.close();
        } catch (Exception e) {
            e.printStackTrace(out);
        }
    }
}