Communicating with the Server

You can even use the dataset tag to communicate with an HTTP server. For example, the application in Listing 8.12 is a page that accepts user registration. Upon the user registering, the application will consult the checkUserName.jsp (in Listing 8.13) to enquiry if the user name has been taken.

Listing 8.12. Registration page
<canvas width="280">
    <dataset name="myDataset"
          src="http://localhost:8080/lps-
       4.0.x/app08/checkUserName.jsp"
    >
    </dataset>
    <datapointer id="mydp" xpath="myDataset:/response[1]">
        <method event="ondata">
            if (mydp.getNodeText() == "OK") {
                screen1.setAttribute('visible', false);
                screen2.setAttribute('visible', true);
            } else {
                userNameErrorText.setAttribute('text',
                        'User name already taken.'),
                userNameErrorView.setAttribute('visible', true);
            }
        </method>
    </datapointer>

    <view id="screen1" x="10" y="20">
        <simplelayout axis="y"/>
        <view>
            <text x="10">User Name:</text>
            <edittext id="userName" x="140"/>
        </view>
        <view visible="false" id="userNameErrorView">
            <text fgcolor="red" resize="true" id="userNameErrorText"
                    x="10"/>
        </view>
        <view>
            <text x="10">Password:</text>
            <edittext id="password" password="true" x="140"/>
        </view>
        <view visible="false" id="passwordErrorView">
            <text fgcolor="red" resize="true" id="passwordErrorText"
                    x="10"/>
        </view>
        <view align="right">
            <button onclick="validateUser()">Register
                <method name="validateUser">
                    var validated = true;
                    userNameErrorView.setAttribute('visible',
                            false);
                    passwordErrorView.setAttribute('visible',
                            false);
                    if (userName.text.length &lt; 4) {
                        userNameErrorText.setAttribute('text',
                        'User name must be at least 4 characters.'),
                        userNameErrorView.setAttribute('visible',
                                true);
                        validated = false;
                    }
                    if (password.text.length &lt; 4) {
                        passwordErrorText.setAttribute ('text',
                        'Password must be at least 4 characters.'),
                        passwordErrorView.setAttribute('visible',
                                true);
                        validated = false;
                    }
                    if (validated) {
                        myDataset.setQueryString("userName=" +
                                userName.text);
                        myDataset.doRequest();
                    }
                </method>
            </button>
        </view>
    </view> <!-- end of screenl -->
    <view id="screen2" visible="false" x="10" y="20">
        <text>Thank you for registering.</text>
    </view>
</canvas>

Listing 8.13. The checkUserName.jsp page
<response>
<%
    String userName = request.getParameter("userName");
    if (userName!=null && !userName.equals("pandora")) {
        out.println("OK");
    } else {
        out.print("Error");
    }
%>
</response>

The checkUserName.jsp page is not connected to the database. In fact, it hardcodes the user name pandora as the only user name that has been used. Therefore, you can register using any name but pandora.

To test the registration application, use the following URL:

http://localhost:8080/lps-4.0.x/app08/registration.lzx

Figure 8.8 shows the result.

Figure 8.8. Registration page


Figure 8.9. A failed registration attempt


As another example, the code in Listing 8.14 offers a customer form that takes the user’s name and email. Upon registration, a message will be displayed.

Listing 8.14. The customer form
<canvas height="600" width="500">
    <dataset name="myData" type="http"
            src="http:contact.jsp"/>
    <simplelayout axis="x"/>
    <form>
        <submit name="contact" data="${myData}"/>
        <text>Customer Name</text>
        <edittext name="customerName"/>
        <text>Email</text>
        <edittext name="email"/>
        <radiogroup name="customerType">
            <radiobutton>Individual</radiobutton>
            <radiobutton>Organization</radiobutton>
        </radiogroup>
        <button isdefault="true"
                onclick="parent.contact.submit()">Submit</button>
    </form>

    <view layout="axis:y">
        <text multiline="true" width="400"
                datapath="myData:/response/message/text()"/>
    </view>
</canvas>

Listing 8.15. The contact.jsp page
<response>
    <message>
        <![CDATA[
Thank you <b><%=request.getParameter("customerName")%></b>.
Our &quot;<%=request.getParameter("customerType")%> Customer&quot;
       manager
will contact you at <%=request.getParameter("email")%>.
        ]]>
    </message>
</response>

You can use the following URL to test and run the application in Listing

http://localhost:8080/lps-4.0.x/app08/customerForm.lzx

Figure 8.10 shows the generated application.

Figure 8.10. The first customer form


If you click the Submit button, you will see something similar to Figure 8.11.

Figure 8.11. Registration confirmation


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset