WAP for Java developers

Develop WAP applications with Java servlets and JSP

1 2 Page 2
Page 2 of 2

Listing 3 shows an example of a simple servlet that displays the current date and time on a wireless device when invoked.

Listing 3. MobileDate.java

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
/**
 * This is a simple servlet that will run on a cell-phone. It displays the   
 * current date and time. 
 *
 * @author <a href="mailto:qmahmoud@javacourses.com">Qusay H. Mahmoud</a>
 */
public class MobileDate extends HttpServlet {
public void service (HttpServletRequest request, HttpServletResponse 
response) throws ServletException, IOException {
      // set content type for wireless data
      response.setContentType("text/vnd.wap.wml");
// get the communication channel with the requesting client
      PrintWriter out = response.getWriter();
      // write the data
      out.println("<?xml version=\"1.0\"?>");
      out.println("<!DOCTYPE wml PUBLIC \"-//WAPFORUM//DTD WML 1.1//EN\"");
      out.println(" \"http://www.wapforum.org/DTD/wml_1.1.xml\">");
      out.println("<wml>");
      out.println("<card title=\"MobileDate\">");
      out.println(" <p align=\"center\">");
      out.println("Date and Time Service<br/>");
      out.println("Date is: "+ new java.util.Date());
      out.println("</p>");
      out.println("</card>");
      out.println("</wml>");
  }
}

Most of the preceding code uses pure WML tags, with the exception of the line:

Response.setContentType("text/vnd.wap.wml");

This line ensures that the correct MIME type is set for the WML document. Please refer to the Web Server Configuration section, which follows, for more information on how to add MIME types. The rest of Listing 1 basically outputs a WML document. When this servlet is invoked from a mobile phone, it displays the current date and time as shown in Figure 8. For this example, I have used Apache and JServ.

Figure 8. A servlet running on the Ericsson's simulator

By the way, cookies are useful for maintaining state and keeping track of users' sessions. Cookies are part of the WAP specification, but unfortunately, are not yet implemented by all WAP browsers. The Nokia 7110 doe s not yet support cookies. However, Phone.com's UP.Simulator does support them.

WAP and JSP

Java Server Pages lets you embed Java statements within HTML documents. When JSP is invoked, it is compiled into a Java servlet and executed by the server to create a dynamic HTML document. In the case of WAP, however, you want to create dynamic WML documents. Therefore, developing WAP applications using JSP can be done easily once you know the syntax of WML.

The following example is similar to the preceding servlet example; it displays the current date and time on a wireless device.

Listing 4. mobiledate.jsp

<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN"
"http://www.wapforum.org/DTD/wml_1.1.xml">
<%
response.setContentType("text/vnd.wap.wml");
out.println("<wml>");
out.println("<card title=\"MobileDate\">");
out.println(" <p align=\"center\">");
out.println("Date and Time Service<br/>");
out.println("Date is: "+ new java.util.Date());
out.println("</p>");
out.println("</card>");
out.println("</wml>");
%>

Again, you need to set the MIME type correctly to make sure that the mobile phone browser is able to parse the contents. The following line sets the MIME type to a WAP/WML document:

<% response.setContentType("text/vnd.wap.wml") %>

Once this JSP document is invoked, it displays the current date and time as shown in Figure 9.

Figure 9. JSP page running on Ericsson's simulator

JAWAP

JAWAP (which was initially known as JAFFA) is Ericsson's Java Framework for WAP. It is based on RMI and servlets. I personally found JAWAP to be a bit complex to use; for example, you have to develop several classes to get a simple application up and running. Further, it doesn't seem to be stable. Using servlets or JSP to develop WAP applications is much easier than using JAWAP. If you are curious about JAWAP, however, you can download it from Ericsson's Website in Resources.

Web server configuration

When a regular Web browser receives a page, it has to distinguish between HTML, image data, audio, and video. To enable that, with every response from the Web server, a piece of header information with every file comes down to the browser. This piece of information is known as MIME, which stands for Multipurpose Internet Mail Extension. Some of the common MIME types include text/html for HTML files and image/gif for gif files.

To enable the Web server to serve WAP documents, it needs some new MIME types. The types that WAP requires are:

  • text/vnd.wap.wml for .wml files (WML source files)
  • application/vnd.wap.wmlc for .wmlc files (WML compiled files)
  • text/vnd.wap.wmlscript for .wmls files (WMLScript source files)
  • application/vnd.wap.wmlscriptc for .wmlsc files (WMLScript compiled files)
  • image/vnd.wap.wbmp for .wbmp files (wireless bitmaps)

Adding these MIME types varies from server to server. Check the documentation for the Web server you are running, and you'd be surprised how easy it is to add these new MIME types. In Apache, the preceding MIME types should be added to the httpd.conf file.

WAP emulators and development tools

In order to access WAP services, you need a WAP-enabled phone like the Nokia 7110, Siemens S25, Ericsson R380, or Alcatel OneTouch. However, as far as I know, there are no WAP-enabled phones in the North American market yet. The Nokia 7110, the first WAP-enabled phone, has been available in Europe, Asia, and Africa for several months now. The joke in the American wireless market is that WAP stands for Where Are the Phones. Still, I think you will start seeing WAP-enabled phones in the American market sometime this summer.

If you are in North America and you'd like to start with WAP, you can use an emulator for now. There are several emulators available. In this article, I have used the ones from Ericsson and Nokia. But there are others available from Motorola and Phone.com. See Resources.

As for development tools, I have used Nokia's WAP ToolKit and Ericsson's WapIDE, and I liked them both. They are basically used to assist in developing WAP applications. But the major advantage of them is that they come with WAP simulators with which you can test your WAP application. Again, other development tools are available in Resources.

Conclusion

WAP is an open specification for delivering Internet content to wireless devices. In this article, you have learned the basics of WAP, WML, and WMLScript. I have provided examples of sample WAP applications to give you a flavor of WAP, and I have demonstrated how you can develop WAP applications in Java using servlets and JSP. WAP is a new technology with a promising future (or so I hope). If you would like to discuss your experiences or ask questions about using WAP and Java, please join my short-term community discussion on this topic. Hope to hear from you there!

Qusay H. Mahmoud lives in Ottawa, Canada. He provides Java and WAP consulting and training services. Qusay has published dozens of articles on Java, and is the author of Distributed Programming with Java (Manning Publications, 1999).

This story, "WAP for Java developers" was originally published by JavaWorld.

Related:

Copyright © 2000 IDG Communications, Inc.

1 2 Page 2
Page 2 of 2