Dynamic content makes the Internet exciting. The same is true with the wireless Web. Your goal is slightly different when dealing with WAP-enabled phones; you definitely want to provide interesting content, but you always want to make sure that the content you deliver is as focused as possible.
In this 10-Minute Solution I show you how to develop an application using Active Server Pages to produce WML. For demonstrative purposes, let's develop an application that checks a user's bank account balance. Normally, we'd want to secure this application using WTLS, but to keep things simple let's focus on the basic code instead.
Assuming that the bank-account information is stored in a SQL database, with an appropriate Web server connected, we can start laying out our application.
| |
|
Figure 1: Our Login Screen
|
Our account information table is going to be quite small (unrealistically so); it includes only the account number, the customer name, the account balance, and a PIN for security. Another field we might include is the account type or a relational table that includes all prior transactions. Here we use just one table, tbl_account_info, with four fields: accountno, custname, accountbal, and accountpin.
Taking a look at our interface, we can start flowcharting how we want our application to work.
Let's say that we want our users to enter their account numbers, and then have our application prompt for the account PIN. After validating the information, it would return a welcome message along with the account balance.
From this point we can start developing our actual code. Usually when writing dynamic code, I write the static code first and then try to make it dynamic later. I've found this approach helps me keep track of where I place content.
In the first section of the code, we set up just the page that will prompt for the user input (see display screen in Figure 1).
Pay special attention to the first line of this code section; I discuss its importance later. The rest of the card is basic WML code:
<% Response.ContentType = "text/vnd.wap.wml" %>
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN"
"http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<card id="login" title="WAP Banking!">
<p>
Welcome to WAP Bank! <br/>
Enter Acct No: <input name="accountno" type="text" maxlength="6" />
Enter PIN: <input name="accountpin" type="password" maxlength="4" />
<do type="accept" label="Check Bal">
<go href="checkbal.asp" method="post">
<postfield name="accountno" value="$Accountno" />
<postfield name="accountpin" value="$Accountpin" />
</go>
</do>
</p>
</card>
Having set up the mechanism by which our users enter the appropriate information, we can now move on to the validation of the entered information. Normally, we would do additional data validation on the client side (for example, making sure all account numbers entered are six digits and PINs are entered) to minimize the amount of traffic required to complete a transaction. Remember, cell phones have notoriously bad connections and slow connection speeds, so anything we can do to reduce the potential frustration factor is helpful.
Next, we actually connect to the database and send out a standard SQL query that will return a recordset. We then take the information from that recordset and populate fields in the next card that shows up. This is the basis for delivering dynamic content: taking an information request from the user and showing him or her what he or she has requested to see. The content is therefore "personalized":
<card id="Login" title="Login">
<p>
<%
txtSQL = "SELECT * FROM tbl_account_info WHERE accountno='"
Request.Form("AccountNo") & "' AND accountpin='"
Request.Form("AccountPIN") & "'"
set rs = conn.Execute(txtSQL)
if rs.EOF then
Response.Write "Invalid Login"
Response.Write "<do type='accept' label='Retry'>"
Response.Write " <go href='index.asp'/>"
Response.Write "</do>"
else
Response.Write "Welcome, " & rs.Fields("custname") & " <br/>"
Response.Write"Your account balance is: " & rs.Fields("accountbal")
%>
| |
|
Figure 2: Displaying Query Results
|
After receiving the information from the server, we post that to the card and present it to the user (see Figure 2 at left).
This quick venture into the design and implementation of dynamic-content delivery has illustrated some basic principles:
- Keep the content short and brief. The information we provide based on users' input must be concise and it must quickly answer their requests.
- Using ASP pages is a great way to leverage your current skill sets in a new way. The code is virtually the same; just keep the design issues in mind and make some minor modifications to ensure you are sending the right content type (the first line of our code, remember?).
Using existing technologies to support ventures into the wireless worldpretty exciting stuff, huh?
Return to Get Help with Wireless Dev Page
Return to Main Get Help Page