----------------------------------------------------------------------------- <%@ page info="Sample JSP for Con-way XML Bill of Lading" %> <!-- Imports required for page compilation --> <%@ page import="java.text.*,java.math.*,java.net.*,java.io.*" %> <!-- Imports required for XML parsing with JAXP --> <%@ 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="<BOLrequest testmode=\"Y\">" + "<RequesterUserId shipcode=\"S\">" + userId + "</RequesterUserId>" + "<ChargeCode>P</ChargeCode>" + "<PRONmbr></PRONmbr>" + "<CustRefNmbrs>" + "<PurchaseOrderNmbr>3338889</PurchaseOrderNmbr>" + "<PurchaseOrderNmbr>3338890</PurchaseOrderNmbr>" + "<OtherRefNmbr refcode=\"SKU\" refdesc=\"SKU Number\">3213A</OtherRefNmbr>" + "<OtherRefNmbr refcode=\"UPC\" refdesc=\"UPC number\">789283</OtherRefNmbr>" + "</CustRefNmbrs>" + "<Shipper>" + "<ShipperName>Alan Shipley</ShipperName>" + "<ShipperAddr>1234 NE Main</ShipperAddr>" + "<ShipperCity>Portland</ShipperCity>" + "<ShipperState>OR</ShipperState>" + "<ShipperZip country=\"US\">97202</ShipperZip>" + "<ShipperPhone extension=\"6055\">503.450.6055</ShipperPhone>" + "<ShipperEmail>web.support@Con-way.com</ShipperEmail>" + "</Shipper>" + "<COD>" + "<CODremitTo>" + "<CODremitToName>Albert Cod</CODremitToName>" + "<CODremitToAddr>1234 NE Main</CODremitToAddr>" + "<CODremitToCity>Portland</CODremitToCity>" + "<CODremitToState>OR</CODremitToState>" + "<CODremitToZip country=\"US\">97202</CODremitToZip>" + "</CODremitTo>" + "<CODamount pmttype=\"CustomerCheck\" chargecode=\"P\">4444.44</CODamount>" + "</COD>" + "<Consignee>" + "<ConsigneeCustNmbr>883885</ConsigneeCustNmbr>" + "<ConsigneePhone extension=\"6666\">503.450.6436</ConsigneePhone>" + "<ConsigneeEmail>web.support@Con-way.com</ConsigneeEmail>" + "</Consignee>" + "<Item>" + "<Quantity pkgtype=\"PLT\">44</Quantity>" + "<Weight unit=\"lbs\">667</Weight>" + "<Description>widget-arms</Description>" + "<CmdtyClass>775</CmdtyClass>" + "<NMFClass></NMFClass>" + "<HazMat>N</HazMat>" + "</Item>" + "<Item>" + "<Quantity pkgtype=\"PCS\">11</Quantity>" + "<Weight unit=\"lbs\">789</Weight>" + "<Description>cam-shafts</Description>" + "<CmdtyClass>100</CmdtyClass>" + "<NMFClass></NMFClass>" + "<HazMat>N</HazMat>" + "</Item>" + "<Accessorial chargecode=\"P\">GUR</Accessorial>" + "<Accessorial chargecode=\"C\">DID</Accessorial>" + "<Accessorial chargecode=\"C\">DST</Accessorial>" + "<ShippingRemarks>TEST TEST TEST</ShippingRemarks>" + "<EmergencyContact></EmergencyContact>" + "<PickupRequest>" + "<PickupDate>" + today + "</PickupDate>" + "<PickupReadyTime>4:00 pm</PickupReadyTime>" + "<DockCloseTime>7:00 pm</DockCloseTime>" + "<ContactName>Frank</ContactName>" + "<ContactCompany>Franklin Arms</ContactCompany>" + "<ContactPhone>(333)444-4321</ContactPhone>" + "</PickupRequest>" + "<SendBOLemail/>" + "</BOLrequest>"; 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(); } %> <HTML> <HEAD> <TITLE>Sample JSP for Con-way XML Bill of Lading</TITLE> </HEAD> <BODY> <CENTER> <B><FONT face="arial" size=+1><I>Sample JSP Con-way XML Bill of Lading</I></FONT></B> </CENTER> <BR> <b>Here is the Bill of Lading Response XML output:</b> <BR> <code> <% if (localError.equals("")) { out.print(bolResponse); } else { out.print(localError); } %> </code> <% // Print the tag value we extracted out.print("<B>Here is the data extracted from an XML tag: BOL Request Status: " + status + "</B>"); %> </BODY> </HTML> -----------------------------------------------------------------------------