|
Serving Dynamic WAP
Content
With Java
In this article I will show you how easy it is to start creating dynamic content for WAP-enabled mobile phones using the Wireless Markup Language (WML) and Java Server API. Rather than list and explain all of the WML tags we will examine a sample application that will demonstrate some of the main language features. A Brief Introduction to WAP and WML
WML, the Wireless Markup Language, is designed for mobile
applications. WML is a markup language that is based on XML (eXtensible Markup
Language).
Dynamic WML documents for wireless devices can be easily developed using Java servlets. Once you know the WML syntax, building WAP applications using Java servlets can be an easy task. A servlet processes a client request that is passed from
| |
|
GENERATING
OUTPUT TO WAP CLIENTS Writing the response header information | |
|
setContentType()
For
the user agent to interpret the received content correctly, we need to set the
con-tent-
response.setContentType("text/vnd.wap.wml");
To
set a header field with a string value, use the setHeader() method. Use Writing
the response body
After
setting the header information, the servlet is responsible for generating all
the PrintWriter out
= response.getWriter(); Subsequently,
use the print() method or the println() method to write the "PrintWriter
out = response.getWriter();"
Creating
a WAP application using a servlet Here is an Example source code for HelloServlet.java /*
Generate a greeting: HelloServlet.java */ import
java.io.*; import
javax.servlet.*; import
javax.servlet.http.*; public
class HelloServlet extends HttpServlet { public
void init(ServletConfig config) throws ServletException { super.init
(config); } public
void doGet (HttpServletRequest request, HttpServletResponse
response) throws
ServletException, IOException { doPost
(request, response); } public
void doPost (HttpServletRequest request, HttpServletResponse
response) throws
ServletException, IOException { response.setContentType("text/vnd.wap.wml"); PrintWriter
out = response.getWriter(); out.println
("<?XML version=\"1.0\"?>"); out.println
("<!DOCTYPE wml PUBLIC \"-//WAPFORUM//DTD WML1.1//EN\""); out.println
("\"http://www.wapforum.org/DTD/wml_1.1.xml\">"); out.println("<wml>"); out.println("<card
id=\"start\">"); out.println("<p>"); out.println("Hello,
here's a dynamically generated greeting!"); out.println("</p>"); out.println("</card>"); out.println("</wml>"); out.close(); } } Code comments
Invoking A Java Servlet
Although
you can invoke a servlet by entering its URL directly in the browser inter-face,
To
call a servlet from a card, use the WML <go> or <a> element. Using
the WML <go> element
To
see how the WML <go> element works, we will use our HelloServlet example
| |
|
| |
|
In
our example, if a client invokes the servlet using the HTTP
GET method, the request
is first passed to the doGet() method,
which in turn calls the doPost() method
to service the request. In this way, we need only to furnish the code for
doPost(),
which handles POST requests directly
and the GET requests via
redirection. If no
appropriate service method can be found in the servlet, the typical error code
issued by the origin server is HTTP
Error 405.
| |
|
Using the WML <a> element
The WML deck shows how a servlet can be invoked using the WML <a> element:
Figures
24.6 and 24.7 show what the WML code looks like
using UP.Browser. | |
| |
|
In general, there is no difference
between the use of <go> and <a>
unless you need
to pass parameters to the referenced servlet as will be discussed next.
| |
The way to pass parameters from the calling document to the servlet depends on how the servlet is invoked. Using explicit query string One straightforward means to pass parameter values to the referred servlet is to append the variable’s name and value pairs as a query string after the URL of the servlet. Here are four ways of using a query string to pass the values of two parameters, myname and mypwd:
http://localhost/servlet/Logon?myname=susan&mypwd=wap
<a href="http://localhost/servlet/Logon?myname=susan&mypwd=wap"> My login </a>
<go href="http://localhost/servlet/Logon?myname=susan&mypwd=wap" /> <option onpick="http://localhost/servlet/Logon?myname=susan&mypwd=wap" /> My login </option> | |
|
Using the <postfield> element
Another way of passing parameter values from a WML deck to a servlet is through the WML <postfield> element, which is used with the <go>element. This code invokes a servlet with the user-input values of two variables, Name and Passwd, using the <postfield> element for each value to be passed to the servlet: <do type="accept" label="servlet"> <go href="http://myHost:port/servlet/HelloServlet" method="post"> <postfield name="myname" value="$Name" /> <postfield name="mypwd" value="$Passwd" /> </go> </do> Code comments
| |
The way a servlet processes a request from a WML client is identical to the way it handles a request from an HTML client. Typically, a servlet is initialized when it is invoked. It then processes individual requests using the appropriate service method such as doGet() or doPost(). Retrieving header information Just as there are HttpServletResponse methods that a servlet can use to set HTTP header information, there are methods that a servlet can use to retrieve HTTP. There are four HttpServletRequest methods capable of retrieving informa-tion about the headers in an incoming request: | |
|
2. WML
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. | |
|
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. | |
|
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:
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 fil. | |
Summary
Wireless networks are mainly used for voice communication. Wireless operators,
however, are interested in delivering data over wireless networks. Having become
a new buzzword, WAP (Wireless Application Protocol) is designed specifically for
delivering Internet data over wireless networks. This article introduces you to
WAP and its related technologies (WML, WMLScript, and so forth), and shows you
how to develop wireless applications using JavaTM
technolog. we recapture
the main tasks involved in generating dynamic content
using
server-side technology. Specifically, we see how a Java servlet can be invoked
| |
Reference | |
| |
| |
|