# Step 2: Processing Data with Page Flows

In this step you will create the data processing elements of your page flow. The data processing of your page flow consists of three parts: (1) accepting user requests for a credit report, (2) the retrieval of credit reports from a web service, and (3) the storage of the credit reports within the page flow.

(1) The first task is accomplished through a Form Bean. Form Beans act as intermediaries between the requests that users enter into JSP pages and your page flow. The data that users submit is typically stored in a Form Bean, which is then passed to an Action method for further processing.

(2) The second task is accomplished through an Action method which requests and retrieves credit reports from a web service.

(3) The third task is accomplished through a member variable. Member variables are Java objects that you place on your page flow, typically to store some data content. In this case you will store the credit reports retrieved from the web service, so that they can be displayed to the user.

The tasks in this step are:

- To generate a WSDL file and a web service control

- To add a web service control
- To add an Action method and Form Bean
- To edit the Form Bean
- To edit the Action method
- To add a member variable
- To re-arrange the Flow View icons

To Generate a WSDL File and a Web Service Control

In this step you will create a control for the InvestigateSync web service, which allows your page flow to request and receive credit-worthiness reports from the web service.

1. On the Application tab, within the folder PageFlowTutorial/InvestigateWebService/investigateJWS, right-click the web service InvestigateSync.jws and select Generate WSDL File.
   The WSDL file InvestigateSyncContract.wsdl is created and placed into the folder investigateJWS.
2. On the Application tab, hold the mouse button down over the InvestigateSyncContract.wsdl file and drag it into the PageFlowTutorial/PageFlowTutorialWeb/investigateJPF folder.
3. On the Application tab, right-click the WSDL file InvestigateSyncContract.wsdl and select Generate Service Control.
   The control file, InvestigateSyncControl.jcx, is created and placed into the folder investigateJPF.

To Add a Web Service Control

In this task you connect the page flow and the web service through the web service control you created in the previous task.

1. Confirm that the investigateJPFController.jpf file is displayed in the main work area.
2. Click the Action View tab.
3. On the Application tab, click the InvestigateSyncControl.jcx file and drag and drop it into Action View.
   A new web service control is add to your page flow.
Action View shows you the controls and their methods available to your page flow file. In this case, one control is available containing four methods: cancelInvestigation, getCreditReport, isReportReady, and requestCreditReport.
4. Press Ctrl+S to save your work.

To Add an Action Method and a Form Bean

In this task you will add an Action method that processes user requests for credit reports. This Action method receives user input, retrieves a credit report from the InvestigateSync web service, and then forwards the user to a JSP page where the credit report is displayed.

You will also add a Form Bean. Form Beans are used to separate the data presentation layer from the data processing layer of your web application. Form Beans are used as intermediaries between the data that users input into JSP pages and the Action methods. Instead of submitting the data directly to the Action method, the data is first databound to a Form Bean and this Form Bean is then passed as a parameter to the Action method. This helps to separate the presentation layer (i.e., the user input forms on the JSP pages) and the processing layer (i.e., the Action methods), because the Form Bean can play a flexible, intermediary role.

1. Confirm that the investigateJPFController.jpf file is open and displayed.
2. Click the Action View tab.
3. Right-click within the grey area of the page flow picture and select New Action.
4. In the New Action dialog, in the Action section, in the Create New field enter pollForCreditReport.
   In the Form Bean section, check Create New and enter PollForCreditReportForm.
5. Click Ok.
   A new Action method is added to your page flow file.
   Note the Action method icon displays a small box on its lower right-hand corner. This indicates that the Action method takes a Form Bean parameter. Form Beans are used as intermediaries between user input and an Action method. When a user submits data from a JSP page, this data is loaded into a Form Bean, and then this Form Bean is passed as a parameter to an Action method.

Edit the Form Bean

In this task you will add a taxID field to the Form Bean. This field will carry the data that users input into the JSP page (a page not yet created).

1. In Action View, right-click the icon for the method pollForCreditReport and select Edit Form Bean.
2. In the Edit Form Bean dialog, in the Property Name field, enter taxID.
3. Click Ok.

Before you move on to the next task, click the Source View tab, to see the code that has been generated. You have just added one Action method and one Form Bean shown below.

Notice that the Action method pollForCreditReport takes the Form Bean parameter PollForCreditReportForm form. Also notice that the Form Bean takes the form of an ordinary Java class with fields, and setter and getter methods on those fields.

To Edit the Action Method

In this task you will add code to the pollForCreditReport Action method. The code you add has two purposes: (1) it polls the web service for a credit report and (2) it controls user navigation.

Polling is a form of synchronous communication between a client and a web service. When a client polls a web service it first asks the web service to begin some process. The client then waits some predetermined amount of time after which it asks the web service if the process is complete. If the process is complete then the client retrieves the result of the process; if the process is not complete, then the client continues to wait for the process to be completed.

1. Confirm that investigateJPFController.jpf is displayed in the main work area.
2. Click the Action View tab.
3. On the Action View tab, click the link text for the pollForCreditReport method.
   The source code for the pollForCreditReport method is displayed in Source View.
4. Edit the pollForCreditReport to look like the following. Code to edit appears in red. (Note that the code below mentions the "reponse.jsp" page, which will be added in the next tutorial step.)

        /**
         * @jpf:action
         * @jpf:forward name="response" path="response.jsp"
         */
        protected Forward pollForCreditReport(PollForCreditReportForm form)
            throws java.lang.InterruptedException
        {
            /**
             * Request a credit report from the InvestigateSync
             * web service.
             */
            investigateSyncControl.requestCreditReport(form.taxID);

            /**
             * Poll the isReportReady method 20 times, once a second,
             * to check if the result is ready.
             */
            for(int i=0; i<20; i++)
            {
                /*
                 * If the report is ready, get the credit report,
                 * store it in m_applicant, and forward the user to
                 * response.jsp.
                 */
                if(investigateSyncControl.isReportReady() == true)
                {
                    m_applicant = investigateSyncControl.getCreditReport();
                    return new Forward("response");
                }
                else
                {
                    /*
                     * If the report is not ready, wait one second before
                     * polling again.
                     */
                    try
                    {
                        Thread.sleep(1000,0);
                    }
                    catch (java.lang.InterruptedException e)
                    {
                    }
                }
            }

            /*
             * The credit report was not delivered in the time allowed.
             * Store a message in the applicant object and Forward the user
             * to the response.jsp page.
             */
            m_applicant.message = "The Investigate Web Service did not respond it the time allowed.";
            return new Forward("response");
        }

5. Press Ctrl+S to save your work. (Note that you will see red jagged lines under some of the elements of this code. This indicates that the compiler does not recognize these elements. In the next task you will remove these red lines.)

To Add a Member Variable

In this step you add a member variable to your page flow file. This member variable will store the credit report provided by the InvestigateSync web service.

1. Confirm that the investigateJPFController.jpf file is open and displayed.
2. Click the Source View tab.
3. Add the following code to the body of investigateJPFController. (Note that the comment attached to the code below mentions the "response.jsp" page. This JSP page will be added in the next step of the tutorial.)

        /**
         * The m_applicant object stores the credit report provided
         * by the InvestigateSync web service.
         * The tag on response.jsp is data bound to m_applicant.
         */
        public InvestigateSyncControl.Applicant m_applicant;

4. Click Ctrl+S to save your work.

Re-arrange the Flow View Icons

This task does not change the functionality of your page flow, but it does make it easier to understand how the different parts of the page flow are related to one another.

1. Confirm that investigateJPFController.jpf is displayed in the main work area.
2. Click the Flow View tab.
3. By selecting and dragging Action method and JSP icons, arrange the elements as shown below.
   Notice that the JSP page response.jsp is greyed out. This indicates that response.jsp is referred to by the page flow file, but that it does not yet exist within the page flow folder. In the next task you will create the response.jsp page within the page flow folder.
   Also notice that there is no way for a user to interact with the pollForCreditReport Action. In the next step you will add a JSP page which will allow the user to submit requests to the pollForCreditReport Action.

Related Topics

A Detailed Page Flow Example

Using Form Beans to Encapsulate Data

Using Polling as an Alternative to Callbacks

Tutorial: Web Services, Step 3: Add Support for Synchronous Communication

Click the arrow to navigate to the next step in the tutorial.