-----------------------------------------------------------------------------
<%@ page info="Sample JSP for Con-way XML Bill of Lading" %>
<%@ page import="java.text.*,java.math.*,java.net.*,java.io.*" %>
<%@ page import="javax.xml.parsers.*,org.w3c.dom.*,org.xml.sax.*" %>
<%
//Declare variables
String bolRequest = new String(); // a string to hold XML bolRequest document
StringBuffer bolResponse = new StringBuffer(50000);
String localError = new String(); // a string to store any Error Messages.
String status = ""; //sample variable to demonstrate how to pull values out of XML elements
String conwayXMLURL = "http://www.Con-way.com/XMLj/X-BOL";
String bolRequestFormName = "BOLrequest";
// Con-way authentication
// *** replace the "userId" & "passWd" String values with your Con-way username and password
String userId = "userId";
String passWd = "passWd";
String today = new SimpleDateFormat("MM/dd/yy").format(new java.util.Date());
/* Build Bill of Lading Request XML.
* In actual use, you would probably populate the BOL 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.
*/
bolRequest="" +
"" + userId + "" +
"P" +
"" +
"" +
"3338889" +
"3338890" +
"3213A" +
"789283" +
"" +
"" +
"Alan Shipley" +
"1234 NE Main" +
"Portland" +
"OR" +
"97202" +
"503.450.6055" +
"web.support@Con-way.com" +
"" +
"" +
"" +
"Albert Cod" +
"1234 NE Main" +
"Portland" +
"OR" +
"97202" +
"" +
"4444.44" +
"" +
"" +
"883885" +
"503.450.6436" +
"web.support@Con-way.com" +
"" +
"- " +
"44" +
"667" +
"widget-arms" +
"775" +
"" +
"N" +
"
" +
"- " +
"11" +
"789" +
"cam-shafts" +
"100" +
"" +
"N" +
"
" +
"GUR" +
"DID" +
"DST" +
"TEST TEST TEST" +
"" +
"" +
"" + today + "" +
"4:00 pm" +
"7:00 pm" +
"Frank" +
"Franklin Arms" +
"(333)444-4321" +
"" +
"" +
"";
bolRequest = URLEncoder.encode(bolRequest); // converts characters to proper format for post
/* Call Con-way XML Bill Of Lading Interface.
* All the classes used here are java base classes available with any JDK.
* Set up the URL.
*/
try {
// Create a 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(bolRequestFormName + "=" + bolRequest);
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(50000);
// 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++;
bolResponse.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 a specific element value using the cumbersome W3C DOM methods
try {
status = domDoc.getDocumentElement().getElementsByTagName("Status").item(0)
.getFirstChild().getNodeValue();
} catch (Exception ex) {
// If BOL was not successful, get the Error message instead
status = domDoc.getDocumentElement().getElementsByTagName("Error").item(0)
.getFirstChild().getNodeValue();
}
// Close the stream
myInReader.close();
} catch (Exception e) {
localError = e.getMessage();
}
%>
Sample JSP for Con-way XML Bill of Lading
Sample JSP Con-way XML Bill of Lading
Here is the Bill of Lading Response XML output:
<% if (localError.equals("")) {
out.print(bolResponse);
} else {
out.print(localError);
}
%>
<%
// Print the tag value we extracted
out.print("Here is the data extracted from an XML tag: BOL Request Status: " + status + "");
%>
-----------------------------------------------------------------------------