-------------------------------------------------------------------------------------
<%@ page info="Sample JSP for Con-way XML Service Center Details" %>
<%@ page import="java.text.*,java.math.*,java.net.*,java.io.*" %>
<%@ page import="javax.xml.parsers.*,org.w3c.dom.*,org.xml.sax.*" %>
<%
/*
Con-way XML Interface Sample Code for JSP - Service Center Details
-----------------
TODO:
* replace the USERNAME and PASSWORD String values in variables below
with your Con-way username and password
-----------------
Send questions to Andrew vonderLuft at vonderluft.andrew@Con-way.com
*/
//Declare variables
// Con-way authentication
// *** replace the "USERNAME" & "PASSWORD" String values with your Con-way username and password
String username = "USERNAME";
String password = "PASSWORD";
//sample variable to demonstrate how to pull values out of XML elements
//Set value to name of desired element name from the XML Response.
String myElement = "Name";
String myValue = "";
String title = "Service Center Details";
String requestType = "ServiceCenterDetailsRequest";
String conwayXmlUrl = "https://www.Con-way.com/webapp/servicecenter_app/ServiceCenterDetailsServlet";
String xmlRequest = new String(); // a string to hold XML Request document
StringBuffer xmlResponse = new StringBuffer(10000);
String localError = new String(); // a string to store any Error Messages.
String today = new SimpleDateFormat("MM/dd/yy").format(new java.util.Date());
/* Build Rate Request XML.
* In actual use, you would probably populate the Rating 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.
*/
String postalCode = "19348";
xmlRequest = "" +
"" + postalCode + "" +
"";
xmlRequest = URLEncoder.encode(xmlRequest); // converts characters to proper format for post
/* Call Con-way XML Interface.
* All the classes used here are java base classes available with any JDK.
* Set up the URL.
*/
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 = username + ":" + password;
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(requestType + "=" + xmlRequest);
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++;
xmlResponse.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
myValue = domDoc.getDocumentElement().getElementsByTagName(myElement).item(0)
.getFirstChild().getNodeValue();
// Close the stream
myInReader.close();
} catch (Exception e) {
localError = e.toString();
}
%>
Sample JSP for Con-way XML <%=title%>
Sample JSP Con-way XML <%=title%>
Here is the XML Response Data for <%=title%>:
<% if (localError.equals("")) {
out.print(xmlResponse);
} else {
out.print(localError);
}
%>
Here is the data we retrieved from the <<%=myElement%>> element in the XML Response:
<%="
" + myElement + " = " + myValue %>
-------------------------------------------------------------------------------------