-------------------------------------------------------------------------------------
<%@ page info="Sample JSP for Con-way XML Shipment Status Tracking" %>
<%@ page import="java.text.*,java.math.*,java.net.*,java.io.*" %>
<%@ page import="javax.xml.parsers.*,org.w3c.dom.*,org.xml.sax.*" %>
<%
//Declare variables
String shpRequest = new String(); // a string to hold XML Shipment Status Request document
StringBuffer shpResponse = new StringBuffer(10000);
String localError = new String(); // a string to store any Error Messages.
//sample variables to demonstrate how to pull values out of XML elements
NodeList proList = null;
NodeList deliveredDateList = null;
// Con-way authentication
// *** replace the "userId" & "passWd" String values with your Con-way username and password
String userId = "userId";
String passWd = "passWd";
// *** Also add a valid CustRefNbr value in that element below
/* Build Shipment Status Request XML.
* In actual use, you would probably populate the Shipment Status Request
* parameters (Weights, Classes, Zip Codes, etc.) from data submitted
* via an on-line order form or database.
* For this sample we will just hard code some dummy data.
*/
shpRequest = "" +
"***ADD YOUR TRACKING NUMBER HERE***" +
"***ADD YOUR TRACKING NUMBER HERE***" +
"";
shpRequest = URLEncoder.encode(shpRequest); // converts characters to proper format for post
/* Call Con-way XML Shipment Status Interface.
* All the classes used here are java base classes available with any JDK.
* Set up the URL.
*/
String conwayXMLURL = "http://www.Con-way.com/XMLj/X-ShipmentStatus";
String shpRequestFormName = "ShipmentStatusRequest";
try {
// Create a URL connection
URL myurl = new URL(conwayXMLURL);
HttpURLConnection myConnection = (HttpURLConnection) myurl.openConnection();
// Setup connection parameters
myConnection.setRequestMethod("POST");
myConnection.setDoOutput(true);
myConnection.setDoInput(true);
myConnection.setUseCaches(false);
// Set the headers correctly (URL encoding, authentication)
myConnection.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
String authString = userId + ":" + passWd;
String auth = "Basic " + new sun.misc.BASE64Encoder().encode(authString.getBytes());
myConnection.setRequestProperty("Authorization", auth);
// Send the XML message is sent as a formField/value pair
DataOutputStream myOutStream = new DataOutputStream(myConnection.getOutputStream());
myOutStream.writeBytes(shpRequestFormName + "=" + shpRequest);
myOutStream.flush();
myOutStream.close();
// Now we can read the XML response. First create an InputStream
InputStream iStream = myConnection.getInputStream();
// mark the InputStream before reading so it can be reset, and re-read
iStream.mark(10000);
// Create a reader to get the entire XML response as a String
InputStreamReader myInReader = new InputStreamReader(iStream);
int chr = myInReader.read();
int responseLength = 0;
// Build the response Stringbuffer from the InputStream
while (chr != -1) {
responseLength++;
shpResponse.append(String.valueOf((char) chr));
chr = myInReader.read();
}
/* Parse the XML response to get specific element values
* You could use JAXP & DOM, JDOM or other XML parsers from Sun, IBM, etc.
* If you need only the final net charge of the quote you might find it easier
* in some cases to use a simpler method such as 'indexOf' on the response String.
*/
// Let's say you use JAXP and DOM Level 2 - here's how you would get the DOM tree
// and specific element values.
// First, reset the InputStream created above
iStream.reset();
// Then create a W3C DOM document using the InputStream
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
org.w3c.dom.Document domDoc = db.parse(iStream);
// Now you can get specific element values using the cumbersome W3C DOM methods
// First get the NodeLists for the Elements you want, to be used below for output
proList = domDoc.getDocumentElement().getElementsByTagName("PRONmbr");
deliveredDateList = domDoc.getDocumentElement().getElementsByTagName("DeliveredDate");
// Save the Error element value in case there's a problem
localError = domDoc.getDocumentElement().getElementsByTagName("Error").item(0)
.getFirstChild().getNodeValue();
// Close the stream
myInReader.close();
} catch (Exception e) {
localError = e.toString();
}
%>
Sample JSP for Con-way XML Shipment Status Tracking
Sample JSP Con-way Shipment Status Tracking
Here is the Tracking Response XML output:
<%
out.print(shpResponse);
%>
<%
// Now get the specific element data from your NodeLists, and write the results:
if (proList.getLength() > 0) {
for (int i = 0; i < proList.getLength(); i++) {
out.print("PRO Number " +
proList.item(i).getFirstChild().getNodeValue() +
" -- delivered " +
deliveredDateList.item(i).getFirstChild().getNodeValue() +
"
");
}
} else {
out.print(localError);
}
%>
-------------------------------------------------------------------------------------