js| page First, I'll discuss the Httpconnection interface, which can be used to establish an HTTP connection
Httpconnection Interface
Connected Limited Device Configuration (limited connection device configuration. Referred to as CLDC). Provides a set of classes for network connections, the common connection framework? A platform-independent connectivity framework that provides a layered interface for implementing the operating system provided by a specific device summary (such as Mobile Information Device profile (MIDP)).
MIDP implements the extended CLDC General connection framework by providing a httpconnection framework that supports HTTP. All MIDP application implementations require HTTP support, primarily because HTTP can be achieved by using ip-based protocols such as TCP/IP, or by using a non-IP protocol such as WAP.
All connections are created using the open () method of the connector class, and if the connection succeeds, this method returns an object that implements a common connection excuse, for example, where the following code snippet 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 is established, you can set the properties, and then you can establish an I/O stream to send or receive data. For example, take a look at this little piece of code that sets the properties and builds an input/output stream.
Set HTTP Properties
Connection.setrequestmethod (Httpconnection.post);
Connection.setrequestproperty ("If-modified-since", "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 I/O flow
InputStream is = Connection.openinputstream ();
Let's look at an example of how to invoke a JSP from MIDlet, where we call the JSP page code in section 1, as follows:
Code 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 also wants to get a value of a variable named name, and once that value is obtained, an instance of date is created, and the value of name and date is then hit to the output stream in the client.
Now, let's look at how to write a MIDlet to invoke this JSP page, we will invoke it using the Post request method, which means that the data that is passed to the JSP page is not encoded using a URL, but rather a separate piece of information, which is MIDlet code as shown in code Snippet 2.
Code 2:
Invokejspmidlet.java
Import javax.microedition.lcdui.*;
Import javax.microedition.midlet.*;
Import javax.microedition.io.*;
Import java.io.*;
public class Invokejspmidlet extends MIDlet implements Commandlistener {;
display display = NULL;
Name field
TextField name = NULL;
Form form;
String url = "http://127.0.0.1:8080/examples/jsp/today.jsp";;
Static final Command callcommand = new command ("date?", Command.ok, 2);
Static final Command clearcommand = new Command ("clear", Command.stop, 2);
String myname;
Public Invokejspmidlet () {;
display = Display.getdisplay (this);
name = new TextField ("Name:", "", Textfield.any);
form = new Form ("Invoke JSP");
};
public void startApp () throws midletstatechangeexception {;
Form.append (name);
Form.addcommand (Clearcommand);
Form.addcommand (Callcommand);
Form.setcommandlistener (this);
Display.setcurrent (form);
};
public void Pauseapp () {;
};
public void Destroyapp (Boolean unconditional) {;
Notifydestroyed ();
};
void invokejsp (String url) throws IOException {;
Httpconnection c = null;
InputStream is = null;
OutputStream OS = null;
StringBuffer B = new StringBuffer ();
TextBox t = null;
try {;
c = (httpconnection) connector.open (URL);
C.setrequestmethod (Httpconnection.post);
C.setrequestproperty ("If-modified-since", "Dec 2001 15:17:19 GMT");
C.setrequestproperty ("User-agent", "profile/midp-1.0 configuration/cldc-1.0");
C.setrequestproperty ("Content-language", "En-ca");
C.setrequestproperty ("Content-type", "application/x-www-form-urlencoded");
OS = C.openoutputstream ();
Os.write (("Name=" +myname). GetBytes ());
Os.flush ();
is = C.opendatainputstream ();
int ch;
while (ch = is.read ())!=-1) {;
B.append ((char) ch);
System.out.print ((char) ch);
};
t = new TextBox ("Date", b.tostring (), 1024, 0);
T.setcommandlistener (this);
}; finally {;
if (is!= null) {;
Is.close ();
};
if (OS!= null) {;
Os.close ();
};
if (c!= null) {;
C.close ();
};
};
Display.setcurrent (t);
};
public void Commandaction (Command C, displayable D) {;
String label = C.getlabel ();
if (Label.equals ("clear")) {;
Destroyapp (TRUE);
}; else if (label.equals ("date?") {;
MyName = Name.getstring ();
try {;
invokejsp (URL);
};catch (IOException e) {;};
};
};
};
The Invokejspmidlet code specifies the URL of the JSP page to be invoked, creates two command buttons, and then creates a text field that allows the user to enter a name inside. In the Invokejsp () method, will create an HTTP connection to this URL, and then build I/O stream, MIDlet use the output stream to send data to the JSP page, and then use the input stream to receive data from the JSP page, note that in this example we will send the name to the JSP page, In fact, it just shows you how the data flows between MIDlet and pages.
In code snippet 2, it should be noted that in order for the JSP page to use GetParameter () to get the value of the data from the name variable, you must set the Content-type property to application/x-www-form-urlencoded.
Summary
This article only demonstrates how to invoke JSP pages from MIDlet, Invokejspmidlet can also be easily modified to implement the purpose of invoking other JSPs. But note that JSP is primarily used in conjunction with HTML, but if your mobile device's browsers can't handle HTML, then XML is a great choice because MIDlet can parse XML documents.