The following code example handles a form that is submitted
as XML. The values that correspond to the fields named mortgageAmount, lastName,
and firstName are displayed. A user-defined method
named getNodeText is used in this quick start.
It accepts an org.w3c.dom.Document instance and
a string value that specifies the node name. This method returns
a string value that represents the value of the node. (See Handling Submitted Forms.)
/*
* Ensure that you create the Java proxy classes to use
* base64 encoding. This is required to populate a BLOB
* object with data or retrieve data from a BLOB object.
*
* For information, see "Creating Java proxy classes using Apache Axis"
* in Programming with LiveCycle.
*/
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.adobe.idp.services.BLOB;
import com.adobe.idp.services.FormsResult;
import com.adobe.idp.services.FormsService;
import com.adobe.idp.services.FormsServiceServiceLocator;
import com.adobe.idp.services.RenderOptionsSpec;
import com.adobe.idp.services.holders.BLOBHolder;
import com.adobe.idp.services.holders.FormsResultHolder;
import com.adobe.idp.services.holders.MyArrayOf_xsd_anyTypeHolder;
public class HandleDataWS extends HttpServlet implements Servlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req,resp);
}
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
try{
//Create a FormsService object
com.adobe.idp.services.FormsServiceServiceLocator sl = new FormsServiceServiceLocator();
FormsService formsOb = sl.getFormsService();
((javax.xml.rpc.Stub)formsOb)._setProperty(javax.xml.rpc.Stub.
USERNAME_PROPERTY, "administrator");
((javax.xml.rpc.Stub)formsOb)._setProperty(javax.xml.rpc.Stub.
PASSWORD_PROPERTY, "password");
//Create a PrintWriter object
PrintWriter pp = resp.getWriter();
//Create a BLOB object to pass
//to processFormSubmission
BLOB inForm = new BLOB();
//Get the input stream passed to the servlet and copy
//its contents into a byte array
int readBytes = -1;
int bufLength = req.getContentLength();
InputStream input = req.getInputStream();
byte[] buffer = new byte[bufLength];
ByteArrayOutputStream output = new ByteArrayOutputStream(bufLength);
while((readBytes = input.read(buffer, 0, bufLength)) != -1) {
output.write(buffer, 0, readBytes);
}
byte[] finalOutput = output.toByteArray();
//Populate the BLOB object
inForm.setBinaryData(finalOutput);
//Set run-time options
RenderOptionsSpec processSpec = new RenderOptionsSpec();
processSpec.setLocale("en_US");
//Specify user agent values
String agent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1;.NET CLR 1.1.4322)";
//Specify env values
String envValues = "CONTENT_TYPE=application/vnd.adobe.xdp+xml";
//Create class holders to pass to ProcessFormSubmission
BLOBHolder outProcessFormSubmissionResultDoc = new BLOBHolder();
javax.xml.rpc.holders.StringHolder clickedButton = new javax.xml.rpc.holders.StringHolder();
BLOBHolder outXML = new BLOBHolder();
BLOBHolder validationErrorsList = new BLOBHolder();
javax.xml.rpc.holders.ShortHolder action = new javax.xml.rpc.holders.ShortHolder();
MyArrayOf_xsd_anyTypeHolder attachments = new MyArrayOf_xsd_anyTypeHolder();
FormsResultHolder processFormSubmissionResult = new FormsResultHolder();
//Invoke the processFormSubmission method
formsOb.processFormSubmission(
inForm,
envValues ,
agent,
processSpec,
outProcessFormSubmissionResultDoc,
clickedButton,
outXML,
validationErrorsList,
action,
attachments,
processFormSubmissionResult);
//Get the FormResult from the FormsResultHolder
FormsResult formResult = processFormSubmissionResult.value ;
//Get the processing state
short processState = formResult.getAction();
//Get the BLOB object that represents the processed form
BLOB formOut = formResult.getOutputContent();
//Determine if the form data is ready to be processed
if (processState == 0)
{
//Determine the content type of the data
String myContentType = formResult.getContentType();
if (myContentType.equals("application/vnd.adobe.xdp+xml")){
//Create a byte array using the BLOB object
byte [] formDataOutStream = formOut.getBinaryData();
//Create an InputStream object
InputStream formInputStream = new ByteArrayInputStream(formDataOutStream);
//Create DocumentBuilderFactory and DocumentBuilder objects
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
org.w3c.dom.Document myDOM = builder.parse(formInputStream);
//Call for each field in the form
String Amount = getNodeText("mortgageAmount", myDOM);
String myLastName = getNodeText("lastName", myDOM);
String myFirstName = getNodeText("firstName", myDOM);
//Write the form data to the web browser
pp.println("<p> The form data is :<br><br>" +
"<li> The mortgage amount is "+ Amount+"" +
"<li> Last name is "+ myLastName+"" +
"<li> First name is "+ myFirstName+"") ;
}
}
}catch (Exception e) {
System.out.println("The following error occurred during this operation " +e.getMessage());
}
}
private String getNodeText(String nodeName, org.w3c.dom.Document myDOM)
{
// Get the Node by name
NodeList oList = myDOM.getElementsByTagName(nodeName);
Node myNode = oList.item(0);
NodeList oChildNodes = myNode.getChildNodes();
String sText = "";
for (int i = 0; i < oChildNodes.getLength(); i++)
{
Node oItem = oChildNodes.item(i);
if (oItem.getNodeType() == Node.TEXT_NODE)
{
sText = sText.concat(oItem.getNodeValue());
}
}
return sText;
}
}