ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button
Galago Lemur from Wireless Java

Invoking JavaServer Pages from MIDlets

by Qusay Mahmoud, author of Learning Wireless Java
12/05/2001

The next big shake-up in the industry is wireless. The benefit of wireless applications, however, becomes useful for organizations when their employees can access critical business data efficiently from anywhere they go. Therefore, the interaction between wireless applications (such as MIDlets) and external enterprise systems is important, but how can this be done?

In this article, I will show you how MIDlets can interact with JSP-based enterprise systems by walking you through an example. First, however, I will discuss the HttpConnection interface, which can be used for establishing HTTP connections.

The HttpConnection Interface

The Connected Limited Device Configuration (CLDC) provides a set of classes for network connectivity, collectively known as the generic connection framework -- a platform-independent connectivity framework that provides a hierarchy of connectivity interfaces, the implementation os which is to be provided by profiles (such as the Mobile Information Device Profile (MIDP)).

The MIDP extends the CLDC generic connection framework by providing the HttpConnection framework to support HTTP. All MIDP implementations are required to have support for HTTP, and this is mainly because HTTP can either be implemented using IP-based protocols (such as TCP/IP) or non-IP protocols (such as WAP).

All connections are created using the open() method of the Connector class. If successful, this method returns an object that implements one of the generic connection interfaces. For example, the following segment of code can be used to open an HTTP connection to a URL:

String url = "http://www.ora.com/whatif.jsp";
HttpConnection connection = Connector.open(url);

Once a connection has be established, some properties can be set, and I/O streams can be established to send and receive data. For example the following snippet of code sets some properties and establishes I/O streams:

// set some HTTP properties
connection.setRequestMethod(HttpConnection.POST);
connection.setRequestProperty("IF-Modified-Since", 
	"20 Nov 2001 16:33:19 GMT");
connection.setRequestProperty("User-Agent",
	"Profile/MIDP-1.0 Configuration/CLDC-1.0");
connection.setRequestProperty("Content-Language", "en-CA");

// create I/O streams
InputStream is = connection.openInputStream();
OutputStream os = connection.openOutputStream();

Example: Invoke a JSP Script

Now, let's look at an example to see how to invoke JSP from MIDlets. The JSP page we want to invoke is shown in Listing 1.


Listing 1: today.jsp

<%! String name; %>
<%
name = request.getParameter("name");
java.util.Date today = new java.util.Date();
out.println("Got: "+name);
out.println("Date&time: "+today);
%>

This JSP page expects a value for a variable named name. Once the value is retrieved, an instance of Date is created, and then the name and date are printed to the output stream for the client.

Pages: 1, 2

Next Pagearrow