LookupISBN

JSP

You say you want to pull information from a web service. Yes, there’s an app for that! This example gets the xml returned by isbndb when you provide an ISBN. Here is a screen shot.



Presumably, you would add the information to spreadsheet or to a table on a database. For now, this example just displays the retrieved record.



Here is the code. &lt;%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.io.*" import="java.net.*" import="java.sql.*" import="java.util.*" import="java.util.Properties" errorPage="" %> &lt;%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> &lt;% /* * LookupISBN.jsp * * Copyright (c) 2013 Alfred Wheeler * All rights reserved. * * This software is the proprietary * information of Alfred Wheeler * */ /**  * The purpose of this application is to retrieve * information related to an ISBN. For now, it is  * limited to 500 retrievals per day, 24 hour period... * * EXAMPLE *  LookupISBN.jsp * * @param  help *             Optional. Usage message. * * @author      8/2/2013 created by Alfred Wheeler. * * @version     0.1 * * @see         [...] * */ /*  * Initialize some variables */ String message = ""; long dt = 0; String title = null; Connection conn = null; DatabaseMetaData dbmd = null; Statement st = null; ResultSet rs = null; ResultSetMetaData rsmd = null; String sql = ""; String s = null; // miscellaneous utility variable int i = 0; // miscellaneous utility variable String isbn = null; String isbnRecord = ""; /* * Get a date time stamp. Set a page title, if needed... */ dt = System.currentTimeMillis; title = "Lookup ISBN"; /* * Get the database connection values, etc... from the properties file * ( This eventually will be externalized to reduce code clutter... ) */ String p = application.getRealPath("/") + "properties/Demo.properties"; InputStream propsFile; Properties appProp = new Properties; try { propsFile = new FileInputStream(p); appProp.load(propsFile); propsFile.close; } catch (IOException ioe) { out.println("&lt;PRE>"); out.println("I/O Exception."); ioe.printStackTrace; out.println("&lt;/PRE>"); } String dbDriver = "" + appProp.getProperty("dbDriver"); String dbUrl    = "" + appProp.getProperty("dbUrl"); String dbUid    = "" + appProp.getProperty("dbUid"); String dbUpw    = "" + appProp.getProperty("dbUpw"); String css      = "" + appProp.getProperty("css"); String isbndbKey = "" + appProp.getProperty("isbndbKey"); // My isbndb access key -- limit 500 per day //out.println("&lt;PRE>"); //out.println(dbDriver); //out.println(dbUrl); //out.println(dbUid); //out.println(dbUpw); //out.println(css); //out.println(isbndbKey); //out.println("&lt;/PRE>"); /* * Get values from http request variables */ isbn = request.getParameter( "isbn" ); if ( isbn.trim.equals("") ) { // in case "empty form" submitted... isbn = null; } /* * Retrieve info, if we have an ISBN... */ if ( isbn != null ) { URL isbndbURL = new URL("http://isbndb.com/api/books.xml?access_key=" + isbndbKey + "&index1=isbn&value1=" + isbn); BufferedReader in = new BufferedReader(new InputStreamReader(isbndbURL.openStream)); String inputLine; while ((inputLine = in.readLine) != null) { s = ""; if ( !(inputLine.trim.toUpperCase.equals("")) ) { i = 0; while ( i &lt; inputLine.length ) { if ( inputLine.substring( i, i+1 ).equals("&lt;") ) { s += "&lt;"; } 				else { s += inputLine.substring( i, i+1 ); } 				i++; } 		} 		isbnRecord += (s + "\n"); } 	in.close; /* 	 * Open the database */ 	try { Class.forName(dbDriver); conn = DriverManager.getConnection(dbUrl, dbUid, dbUpw); st = conn.createStatement; // Code to add found isbn lookup results to database table(s) would go here //message = "ISBN, " + isbn + ", has been added to the database."; } // end try catch ( SQLException ex ) { out.println ( "&lt;pre>" ); out.println ( "---" ); out.println ( "--                      FATAL ERROR IN SQL" ); out.println ( "---" ); out.println ( "-- " ); while ( ex != null ) { out.println ("-- SQLState: " + ex.getSQLState ); out.println ("-- Message: " + ex.getMessage ); out.println ("-- Vendor:  " + ex.getErrorCode ); ex = ex.getNextException ; out.println ( "-- " ); } 		out.println ( "---" ); } // end catch catch ( java.lang.Exception ex ) { /* 		 * Got some other type of exception. Dump it. */ 		out.println ( "&lt;pre>" ); out.println ( "---" ); ex.printStackTrace ; out.println("exception: " + ex.getMessage ); } // end catch finally { // No action for now } // end finally } %> &lt;!DOCTYPE html> &lt;HTML> &lt;HEAD> &lt;link rel="stylesheet" type="text/css" href="&lt;%=css%>"> &lt;TITLE>&lt;%=title%>&lt;/TITLE> &lt;SCRIPT LANGUAGE="JavaScript" SRC="angular.js">&lt;/SCRIPT> &lt;SCRIPT LANGUAGE="JavaScript" SRC="jquery.js">&lt;/SCRIPT> &lt;/HEAD> &lt;BODY> &lt;H1>&lt;%=title%>&lt;/H1> &lt;br>&lt;br>&lt;br>&lt;br> &lt;% if ( !message.equals("") ) { out.println( "&lt;p class=\"centered\">" + message + "&lt;/p>" ); } %> &lt;FORM NAME="form1" METHOD="POST"> &lt;!--Get time and pass as hidden variable in query_string to force browser to get page from server instead of cache... --> &lt;% dt = System.currentTimeMillis; %> &lt;input type="hidden" name="dt&lt;%= dt %>" value="&lt;%= dt %>"> &lt;table class="ivory" cellspacing="0" cellpadding="16"> &lt;TBODY> &lt;tr> &lt;td>&lt;b>ISBN : &lt;/B>&lt;/td>&lt;td>&lt;input type="text" name="isbn" size="31" value="">&lt;/td> &lt;/tr> &lt;tr> &lt;td> &lt;/td>&lt;td> &lt;div > &lt;input type="submit" name="submit" value="Lookup"> &lt;input type="reset" name="clear" value="Reset"> &lt;/div> &lt;/td> &lt;/tr> &lt;/table> &lt;/form> &lt;/nobr> &lt;br>&lt;br>&lt;br>&lt;br> &lt;% if ( !isbnRecord.equals("") ) { out.println("&lt;table border=\"1\">&lt;tr>&lt;td>&lt;pre>"); out.println( isbnRecord ); out.println("&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>"); } %> &lt;/BODY> &lt;/HTML>

And that's it for this example. Hopefully, it gives you some idea about how to get along with a web service. Good luck!