HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Java Unplugged"
Vol. 5, Issue 9, p. 70

	

Listing 1 

1: <?xml version="1.0"?> 
2: <!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" 
     "http://www.wapforum.org/DTD/wml_1.1.xml"> 
3: <wml> 
4:   <card id="splash" title="JavaCafe" newcontext="true" 
     ontimer="#main"> 
5:     <timer value="30"/> 
6:     <p> 
7:     <table columns="2"> 
8:       <tr> 
9:         <td><img src="duke.wbmp" alt="duke"/></td> 
10:         <td><small>Welcome to the JavaCafe</small></td> 
11:       </tr> 
12:     </table> 
13:     </p> 
14:   </card> 
15:   <card id="main" title="JavaCafe"> 
16:     <p> 
17:     UserId: <input name="userid" title="UserId:" format="*M"/> 
18:     Pin: <input name="pin" title="PIN:" format="NNNN" /> 
19:     Purchase: 
20:     <select name="purchase" title="Purchase" multiple="true"> 
21:       <option value="COFFEE">Coffee</option> 
22:       <option value="MOCHA">Mocha</option> 
23:       <option value="LATTE">Latte</option> 
24:       <option value="TEA">Tea</option> 
25:     </select> 
26:     Total: $(totalPurchase) 
27:     <do type="accept" label="Calculate Total"> 
28:       <go href="JavaCafe.wmls#calculateTotal('$(purchase)')"/> 
29:     </do> 
30:     <do type="accept" label="Check Out"> 
31:       <go href="http://localhost:7001/Checkout" 
          method="post"> 
32:         <postfield name="userid" value="$(userid)"/> 
33:         <postfield name="pin" value="$(pin)"/> 
34:         <postfield name="purchase" value="$(purchase)"/> 
35:       </go> 
36:     </do> 
37:     </p> 
38:   </card> 
39: </wml>
 
Listing 2 

1: extern function calculateTotal(inputPurchase) { 
2:   var total = 0; 
3:   var purchase = URL.unescapeString(inputPurchase); 
4:   var currentItem; 
5:   for (var i=0; i<String.elements(purchase, ";"); i++) { 
6:     currentItem = String.elementAt(purchase, i, ";"); 
7: 
8:     if(currentItem == "COFFEE") { 
9:       total += 1.25; 
10:     } else if(currentItem == "MOCHA") { 
11:       total += 2.25; 
12:     } else if(currentItem == "LATTE") { 
13:       total += 2.00; 
14:     } else if(currentItem == "TEA") { 
15:       total += 0.85; 
16:     } 
17:   } 
18:   WMLBrowser.setVar("totalPurchase", 
       String.format("$%5.2f",total) ); 
19:   WMLBrowser.refresh(); 
20: } 

Listing 3 

1: import javax.servlet.*; 
2: import javax.servlet.http.*; 
3: import java.io.*; 
4: public class Checkout extends HttpServlet { 
5:   public void doPost(HttpServletRequest req, HttpServle- 
     tResponse res) 
6:        throws IOException { 
7:     // Must set the content type first 
8:     res.setContentType("text/vnd.wap.wml"); 
  
9:     // Now we can obtain a PrintWriter 
10:     PrintWriter out = res.getWriter(); 
11:     out.println("<?xml version=\"1.0\"?>"); 
12:     out.println("<!DOCTYPE wml PUBLIC \"-//WAPFORUM//DTD 
        WML 1.1//EN\""+ 
         "\"http://www.wapforum.org/DTD/wml_1.1.xml\">"); 
13:     out.println("<wml>"); 
14:     out.println("<card id=\"checkout\" title=\"JavaCafe\" >"); 
15:     out.println("<p align=\"center\">Thank You "+ 
        req.getParameter("userid") +"!</p>"); 
16:     out.println("</card>"); 
17:     out.println("</wml>"); 
18:   } 
19: } 

  
 
 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.