Striving for simplicity here, I built a basic multi-card WML document that simply
takes data from an input field in one card, passes it to another card, and
prints it out. Pretty simple, I know, but if you want more WML info, check out
our WML Tutorial.
Ok, here's the code:
Welcome.wml
Welcome to HelloWAP
Enter your name:
Hello, $(uname)!
Pretty cool, huh?
Copy this or type it and save it as Welcome.wml in the HelloWAP/helloWAP/presentation
directory. You don't have to delete the Welcome.html file, Enhydra will just
overlook it because of the changes we made to the Makefile in this directory.
Now, all that's left is to write the Welcome.java code so that Enhydra will serve
up the WML. There already exists a Welcome.java file in the presentation directory,
but that code is for serving up HTML pages, not WML. Rename the existing Welcome.java
file and create a new one so you can compare the changes. Here is the new code:
Welcome.java
package helloWAP.presentation;
import com.lutris.appserver.server.httpPresentation.HttpPresentation;
import com.lutris.appserver.server.httpPresentation.HttpPresentationComms;
import com.lutris.appserver.server.httpPresentation.HttpPresentationException;
import com.lutris.appserver.server.httpPresentation.HttpPresentationOutputStream;
import com.lutris.appserver.server.httpPresentation.HttpPresentationRequest;
import com.lutris.appserver.server.httpPresentation.HttpPresentationResponse;
import org.enhydra.xml.xmlc.XMLObject;
import java.io.IOException;
public class Welcome implements HttpPresentation {
public void run(HttpPresentationComms comms)
throws HttpPresentationException, IOException {
WelcomeWML welcome = (WelcomeWML)comms.xmlcFactory.create(WelcomeWML.class);
present(welcome,comms);
}
public void present(XMLObject doc, HttpPresentationComms comms)
throws HttpPresentationException, IOException {
HttpPresentationResponse response = comms.response;
response.setContentType("text/vnd.wap.wml");
HttpPresentationOutputStream out = response.getOutputStream();
String buffer = doc.toDocument();
out.println(buffer);
response.flush();
}
}
There are a few interesting things to note here. First off, I explicitly imported the
classes needed to present WML instead of just importing *, but it gives you a better
idea what classes are needed for WML to work. Now, let's analyze the code. The line:
creates a WelcomeWML (created from compiling the Welcome.wml file) object called
'welcome'. The somewhat complicated looking assignment is for automatic recompilation
of HTML (WML in our case) so that you could change the WML file and the presentation
object would change accordingly without having to recompile the whole project.
The 'present' function creates a typical servlet response object, sets the content
type for WAP, and does an out.println to send the WML to the browser. Pretty straight
forward servlet stuff, with a little Enhydra seasoning thrown in. Now, the java
code doesn't do much of anything dynamic, it just sends the WML code straight to
the browser, but this would be where you would change a tag or add additional text
or whatever.
Once you have all the code written, simply go back to the top-level HelloWAP directory,
do a
make
and you're golden. CD into HelloWAP/output, type:
./start
browse to http://localhost:9000/ (you may want to add the ending '/', some browsers
are finiky about this) with your favorite WAP-enabled browser (phone.com's UP.SDK 4.0 works well) and watch your brand-new
Enhydra-powered WAP application go. Pretty cool, huh? Well, that should get your
feet wet with Enhydra and WAP. If you want to delve deeper, check out the Enhydra
documentation or enhydra.org.