API Quick Starts (Code Examples) > Forms Service API Quick Starts > Quick Start: Handling submitted forms using the web service API

Quick Start: Handling submitted forms using the web service API
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 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/pdf&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 attachments in submitted posted form 
			//Use the MyArrayOf_xsd_anyTypeHolder object
			//that is passed to processFormSubmission			  
			Object[] myAttachments = attachments.value; 
			
			if (myAttachments != null)
			{
				BLOB myatt = (BLOB) myAttachments[0];
			}
						  			 
			//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+"")	;
				  }
		else if (myContentType.equals("application/pdf")){
		
			//Create a PDF file
			File myPDFFile = new File("C:\\Adobe\\tempPDF.pdf");
					
			//Create a byte array using the BLOB object
	  		byte [] pdfOutputStream = formOut.getBinaryData();
		
			//Create a Java FileOutputStream object
			FileOutputStream myOutput = new FileOutputStream(myPDFFile) ;
 
			//Write the byte array contents to the file
			myOutput.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 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;
			}
	}
 

API Quick Starts (Code Examples) > Forms Service API Quick Starts > Quick Start: Handling submitted forms using the web service API

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