DB/2 Client

This is the client code which runs as a stand-alone application. It expects an input file name as well as a four digit code on the command line. This code demonstrates my personal coding approach; I include a static void main routine in order to facilitate debugging. The main routine creates an instance of the class and passes the appropriate arguments to the methods. The existence of the main method generally doesn't cause any conflicts once the class is included in a jar file.

Note how simple and elegant the client can be! I've implemented solutions which function as servlets and call other servlets on the same or other servers. Open an HttpURLConnection to the other machine and you obtain convenient location independence. This code even handles those situations where a non-standard (200) response is received from the webserver. Note that we make two calls to getResponseCode, the first one contained in a try block. An exception will be thrown to the top level if the first attempt isn't prepared to catch it. The second call is only executed if the first one fails. In either case, we process the response appropriately.

import	java.io.*;
import	java.net.*;
import	java.util.*;

public class DB2XMLSendDoc {

	int	rc;
	String	response;

	public static void main( String args[] ) {
		Reader	rdr = null;
		int	fourDigit = -1;
		DB2XMLSendDoc	app = null;

		if( args.length != 2 ) {
			System.err.println( "Usage: DB2XMLSendDoc filename " +
			  "fourdigit" );
			System.exit( 12 );
		}
		try {
			rdr = new FileReader( args[0] );
		}
		catch( IOException e ) {
			System.err.println( "DB2XMLSendDoc: " + args[0] +
			  ": " + e.toString() );
			System.exit( 12 );
		}
		try {
			fourDigit = Integer.parseInt( args[1] );
		}
		catch( NumberFormatException e ) {
			System.err.println( "DB2XMLSendDoc: " + args[1] +
			  ": fourdigit must be numeric" );
			System.exit( 12 );
		}
		app = new DB2XMLSendDoc( rdr, fourDigit );
		System.out.println( "rc = " + app.getReturnCode() );
		System.out.println( "response = '" + app.getResponse() + "'" );
	}

	public DB2XMLSendDoc( Reader in, int fourDigit ) {
		URL			url = null;
		HttpURLConnection	conn = null;
		BufferedReader		br = null;
		PrintWriter		pw = null;
		String			lineIn = null;
		StringBuffer		sb = new StringBuffer();

		try {
			br = new BufferedReader( in );
			url = new URL( "http://localhost/servlet/DB2XMLProvServlet"
			  + "?fourdigit=" + fourDigit );
			conn = (HttpURLConnection) url.openConnection();
			conn.setDoOutput( true );
			pw = new PrintWriter( new OutputStreamWriter(
			  conn.getOutputStream() ) );
			while( ( lineIn = br.readLine() ) != null )
				pw.println( lineIn );
			pw.flush();
			pw.close();
			br.close();
			try {
				rc = conn.getResponseCode();
			}
			catch( Exception e ) {
			}
			if( rc < 0 )
				rc = conn.getResponseCode();
			if( rc == 200 ) {
				br = new BufferedReader( new InputStreamReader(
				  conn.getInputStream() ) );
				while( ( lineIn = br.readLine() ) != null )
					sb.append( lineIn + "\n" );
				br.close();
			}
			else {
				sb.append( conn.getResponseMessage() );
			}
			if( sb.toString().length() > 0 )
				response = sb.toString();
		}
		catch( Exception e ) {
			rc = -1;
			response = e.toString();
		}
	}

	public int getReturnCode() {
		return( rc );
	}

	public String getResponse() {
		return( response );
	}
}

Copyright © 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 by Phil Selby
All rights reserved internationally.