First, I will discuss the HttpConnection interface, which can be used to establish an Http connection.
HttpConnection Interface
Connected Limited Device Configuration (Limited connection Device Configuration. CLDC ). Provides a set of classes for network connections, that is, the common connection framework? A platform independent connection framework that provides a hierarchical connection interface. The operating system is provided by a specific Device table (such as Mobile Information Device Profile (MIDP )).
MIDP extends the General connection framework of CLDC by providing an HTTP-supported HTTP connection framework. All MIDP application implementations require support for HTTP, mainly because HTTP can also be achieved through the use of IP-based protocols (such as TCP/IP) or through the use of non-IP protocols (such as WAP).
All connections are created using the open () method of the Connector class. If the connection is successful, this method returns an object that implements some common connection excuse. For example, the following code snippet can be used to open an HTTP connection to a URL.
String url = "http://www.ora.com/whatif.jsp (preferred for SUN Enterprise Applications )";;
HttpConnection connection = Connector. open (url );
Once a connection is established, you can set properties, and then you can create an I/O Stream to send or receive data. For example, see the following code to set attributes and create an input/output stream.
// Set the HTTP property connection. setRequestMethod (HttpConnection. POST); connection. setRequestProperty ("IF-Modified-Since", "22 Dec 2001 16:33:19 GMT"); connection. setRequestProperty ("User-Agent", "Profile/MIDP-1.0 Configuration/CLDC-1.0"); connection. setRequestProperty ("Content-Language", "en-CA"); // create an I/O Stream InputStream is = connection. openInputStream (); OutputStream OS = connection. openOutputStream ();
Next let's take an example to learn how to call jsp from the MIDlet (the preferred choice for SUN Enterprise applications) and call jsp (the preferred choice for SUN Enterprise applications) the Procedure Section 1 of the page code is as follows:
Code 1:
Today. jsp (preferred for SUN Enterprise Applications) <%! 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 (preferred for SUN Enterprise Applications) also wants to obtain a value of the variable named name. Once this value is obtained, an instance of Date will be created, then the values of name and date will be hit in the output stream of the client.
Now, let's see how to write a MIDlet to call this jsp (preferred for SUN Enterprise Applications) page. We will use the POST Request Method to call it, this means that the data transmitted to the jsp (the preferred choice for SUN Enterprise Applications) page is not URL encoded, but is passed in with a separate piece of information. This section of MIDlet code is shown in code segment 2.