Quick Start (EJB mode): Handling HTML forms submitted as XML using the Java API

The following code example handles an HTML form that is submitted as XML data. The content type value passed to the processFormSubmission method is CONTENT_TYPE=application/x-www-form-urlencoded.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.)

/* 
    * This Java Quick Start uses the following JAR files 
    * 1. adobe-forms-client.jar 
    * 2. adobe-livecycle-client.jar 
    * 3. adobe-usermanager-client.jar 
    *  
    *  (Because Forms quick starts are implemented as Java servlets, it is  
    *  not necessary to include J2EE specific JAR files - the Java project 
    *  that contains this quick start is exported as a WAR file which 
    *  is deployed to the J2EE application server) 
    *   
    *  These JAR files are located in the following path: 
    * <install directory>/sdk/client-libs/common 
    *  
    * For complete details about the location of these JAR files,  
    * see "Including LiveCycle library files" in Programming with LiveCycle 
    */ 
import java.io.IOException; 
import javax.servlet.Servlet; 
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import com.adobe.livecycle.formsservice.client.*; 
import java.util.*; 
import java.io.DataInputStream; 
import java.io.File; 
import java.io.InputStream; 
import java.io.PrintWriter; 
import com.adobe.idp.Document; 
import com.adobe.idp.dsc.clientsdk.ServiceClientFactory; 
import com.adobe.idp.dsc.clientsdk.ServiceClientFactoryProperties; 
 
//Import DOM libraries 
import org.w3c.dom.NodeList; 
import org.w3c.dom.Node; 
import javax.xml.parsers.*; 
 
/* 
    * This quick start handles data submitted as XML from a rendered HTML form  
    */ 
public class HandleSubmittedHTMLForm 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{ 
            PrintWriter pp = resp.getWriter();      
             
            //Set connection properties required to invoke LiveCycle                                 
            Properties connectionProps = new Properties(); 
            connectionProps.setProperty(ServiceClientFactoryProperties.DSC_DEFAULT_EJB_ENDPOINT, "jnp://localhost:1099"); 
            connectionProps.setProperty(ServiceClientFactoryProperties.DSC_TRANSPORT_PROTOCOL,ServiceClientFactoryProperties.DSC_EJB_PROTOCOL);           
            connectionProps.setProperty(ServiceClientFactoryProperties.DSC_SERVER_TYPE, "JBoss"); 
            connectionProps.setProperty(ServiceClientFactoryProperties.DSC_CREDENTIAL_USERNAME, "administrator"); 
            connectionProps.setProperty(ServiceClientFactoryProperties.DSC_CREDENTIAL_PASSWORD, "password"); 
             
            //Create a ServiceClientFactory object 
            ServiceClientFactory myFactory = ServiceClientFactory.createInstance(connectionProps); 
                 
            //Create a FormsServiceClient object 
            FormsServiceClient formsClient = new FormsServiceClient(myFactory);  
         
            //Get Form data to pass to the processFormSubmission method 
            Document formData = new Document(req.getInputStream());  
                         
            //Set run-time options 
             RenderOptionsSpec processSpec = new RenderOptionsSpec();  
             processSpec.setLocale("en_US"); 
             
            //Invoke the processFormSubmission method 
            FormsResult formOut = formsClient.processFormSubmission(formData, 
            "CONTENT_TYPE=application/x-www-form-urlencoded", 
            "", 
            processSpec); 
             
            //Get the processing state 
            short processState = formOut.getAction(); 
                         
            //Determine if the form data is ready to be processed 
            //This code example checks only for submitted data (value is 0) 
            if (processState == 0)  
            { 
             
                    //Get the form data 
                    Document formOutput = formOut.getOutputContent();  
                    InputStream formInputStream = new DataInputStream(formOutput.getInputStream());  
                                                     
                    //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) { 
                 e.printStackTrace(); 
              } 
        } 
             
        //This method returns the value of the specified node 
        private String getNodeText(String nodeName, org.w3c.dom.Document myDOM) 
        { 
          //Get the XML 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; 
        } 
    }