The following code example handles a submitted form. If form data is submitted as XML data, then each field is retrieved. If form data is submitted as PDF data, then the submitted form is saved as a PDF file. (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 ES.*/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 objectcom.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 objectPrintWriter pp = resp.getWriter();//Create a BLOB object to pass//to processFormSubmissionBLOB inForm = new BLOB();//Get the input stream passed to the servlet and copy//its contents into a byte arrayint 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 objectinForm.setBinaryData(finalOutput);//Set run-time optionsRenderOptionsSpec processSpec = new RenderOptionsSpec();processSpec.setLocale("en_US");//Specify user agent valuesString agent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1;.NET CLR 1.1.4322)";//Specify env valuesString envValues = "CONTENT_TYPE=application/pdf&CONTENT_TYPE=application/vnd.adobe.xdp+xml";//Create class holders to pass to ProcessFormSubmissionBLOBHolder 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 methodformsOb.processFormSubmission(inForm,envValues ,agent,processSpec,outProcessFormSubmissionResultDoc,clickedButton,outXML,validationErrorsList,action,attachments,processFormSubmissionResult);//Get the FormResult from the FormsResultHolderFormsResult formResult = processFormSubmissionResult.value ;//Get the attachments in submitted posted form//Use the MyArrayOf_xsd_anyTypeHolder object//that is passed to processFormSubmissionObject[] myAttachments = attachments.value;if (myAttachments != null){BLOB myatt = (BLOB) myAttachments[0];}//Get the processing stateshort processState = formResult.getAction();//Get the BLOB object that represents the processed formBLOB formOut = formResult.getOutputContent();//Determine if the form data is ready to be processedif (processState == 0){//Determine the content type of the dataString myContentType = formResult.getContentType();if (myContentType.equals("application/vnd.adobe.xdp+xml")){//Create a byte array using the BLOB objectbyte [] formDataOutStream = formOut.getBinaryData();//Create an InputStream objectInputStream formInputStream = new ByteArrayInputStream(formDataOutStream);//Create DocumentBuilderFactory and DocumentBuilder objectsDocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();DocumentBuilder builder = factory.newDocumentBuilder();org.w3c.dom.Document myDOM = builder.parse(formInputStream);//Call for each field in the formString Amount = getNodeText("mortgageAmount", myDOM);String myLastName = getNodeText("lastName", myDOM);String myFirstName = getNodeText("firstName", myDOM);//Write the form data to the web browserpp.println("<p> The form data is :<br><br>" +"<li> The mortgage amount is "+ Amount+"" +"<li> Last name is "+ myLastName+"" +"<li> First name is "+ myFirstName+"") ;}else if (myContentType.equals("application/pdf")){//Create a PDF fileFile myPDFFile = new File("C:\\Adobe\\tempPDF.pdf");//Create a byte array using the BLOB objectbyte [] pdfOutputStream = formOut.getBinaryData();//Create a Java FileOutputStream objectFileOutputStream myOutput = new FileOutputStream(myPDFFile) ;//Write the byte array contents to the filemyOutput.write(pdfOutputStream);myOutput.close();}}}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 nameNodeList 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;}}
| Programming with LiveCycle ES (LiveDocs) |
| Adobe LiveCycle ES Update 1 |
Send me an e-mail when comments are added to this page | Comment Report
Current page: http://livedocs.adobe.com/livecycle/8.2/programLC/programmer/help/000081.html