A Sample Servlet Application (WML, Java)
Openwave WAP Push Library provides Java APIs that allow developers to send WAP Pushes to any WAP 1.2.1 compliant device. This technical note and companion servlet application describe how to manipulate a MySQL database using select, insert and update operations, and send WAP Pushes in batch to registered subscribers.
The sample application allows mobile phone users to register to receive offers from a ficticious online store that sells CDs and DVDs. The administrator of this store can send alerts to all subscribers when special offers and/or news becomes available by using a simple HTML form.
The application is made up of three Java classes: registerWAP.java, sendPush.java, push.java, an HTML front end for sending alerts: alert.html, and two MySQL tables: user and alertText. The "user" table contains the following columns: subscriberID, host (gateway/up.link) name and user name. The alertText table contains a single column: alert text.
Note: this sample application is designed to send a WAP Push service indication. For details on how to send WAP Push in other content types, such as Service Load, or Cache Operation, please refer to WAP Push Developer Guide for more information.
Let's take a look at the Java classes that make up the backbone of this application:
registerWAP.java (used to get subscribers into the application)
registerWAP retrieves subscriberID, host name and user name from HTTP headers and querystring. Then it inserts these data to user table that is used to send WAP Push, and return the confirmation of registration to the user. The flow of the application is as follows:
Step 1: obtain subscriberID, host name (from HTTP headers), and user name that is input from a wml file and is passed to registerWAP.java on the query string. Here is the code that caputeres the data from within the servlet:
String subno = request.getHeader("x-up-subno"); String host = request.getHeader("x-up-uplink"); String name = request.getParameter("first");
Step 2: make a database connection and insert subscriber ID, host name and user name to user table. Note that the driver and connection are based on mysql as the back end. This can be replaced with the appropriate JDBC driver for the for the database of your choice.
Class.forName("org.gjt.mm.mysql.Driver"); String url = "jdbc:mysql://localhost:3306/copDB"; Connection con = DriverManager.getConnection (url, "", ""); Statement stmt = con.createStatement (); stmt.executeUpdate("insert into user values('"+subno+"', '"+host+"','"+name+"')");
Step 3: send the confirmation in WML format back to the user.
alert.html provides an interface for the administrator to send WAP Push to all subscribers. It will allows the administrator to select the type of alter to be sent (Music or DVD), and this will be used for the alert title. It also allows the user to input text to be used in the WML deck that will be refered to when a user recieves and views the WAP Push. When the adminstrator submits the form, sendPush.java is called passing these data with post method.
sendPush sends a WAP Push (service indication) to all subscribers who have registered in the database. The flow of the application is as follows:
Step 1: retrieve alert title and text from POST data
Step 2: send a response in HTML format back to the browser
Step 3: store the alert text in alertText table for the further use
Step 4: retrieve all subscribers' information including subscriberID and host name from user table and send WAP Push in batch within a while loop. In this application, the key component is the method plSubmitMag() that implements the sending push task. The procedure for sending the alert is as follows:
- Declare PPG URL, recipient address, push message ID, and SI URL. Since every push submission must have a unique identifier, a random number is assigned to the push message ID.
- Instantiate the PPG object. Set the alert title caught by alert.html as the text to be displayed on the device when the alert is received.
- Instantiate the service indication object and set the value of the href attribute that specifies the URI used to access the indicated service.
- Set the action to signalHigh that instructs the PPG to present the alert as soon as possible.
- Instantiate a plPushMessage object to build the push submission.
- Instantiate a plQualityOfService object to set the quality of service to high and confirm always. As well set this object to the alert message.
- Send the push submission to the PPG and instantiate an object to hold the response from the PPG.
- The pertinent information returned from the PPG is printed on console.
push.java is pointed by SI URI. It makes a database connection and retrieves the alert text from alertText table. Then returns the alert text to the client device.