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 for short) provides a set of classes for network connection, which is a 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 ;;
HttpConnection connection = Connector. open (url );
Once a connection is established, you can set attributes and then 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 HTTP attributes
- // Set HTTP attributes
- 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
- InputStreamIs=Connection. OpenInputStream ();
- OutputStreamOS=Connection. OpenOutputStream ();
In this JSP, you want to obtain the value of a variable named name. Once this value is obtained, a Date instance 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 page. We will use the POST Request Method to call it, this means that the data transmitted to the JSP 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.
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 {;
- DisplayDisplay=Null;
- // Name field
- TextFieldName=Null;
- Form;
- StringUrl="Http: // 127.0.0.1: 8080/examples/jsp/today. jsp";;
- Static final CommandCallCommand=NewCommand ("date? ", Command. OK, 2 );
- Static final CommandClearCommand=NewCommand ("clear", Command. STOP, 2 );
- String myname;
- Public InvokeJSPMidlet (){;
- Display=Display. GetDisplay (this );
- Name=NewTextField ("Name:", "", 25, TextField. ANY );
- Form=NewForm ("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 ){;
- Yydestroyed ();
- };
- Void invokeJSP (String url) throws IOException {;
- HttpConnectionC=Null;
- InputStreamIs=Null;
- OutputStreamOS=Null;
- StringBufferB=NewStringBuffer ();
- TextBoxT=Null;
- Try {;
- C= (HttpConnection) Connector. open (url );
- C. setRequestMethod (HttpConnection. POST );
- C. setRequestProperty ("IF-Modified-Since", "29 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=NewTextBox ("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 ){;
- StringLabel=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 called, creates two command buttons, and creates a text field, allowing you to enter a name in it. In the InvokeJSP () method, an HTTP connection to this URL is established, and then an I/O Stream is established. The MIDlet uses the output stream to send data to the JSP page, 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 data can flow between the MIDlet and page.
In code segment 2, you should note that the JSP page uses getParameter () to obtain data values from the name variable, you must set the Content-Type attribute to application/x-www-form-urlencoded.
Summary
This article only demonstrates how to call JSP pages from the MIDlet. InvokeJSPMidlet can also be easily modified to call other JSPs. However, note that JSP is mainly used in combination with HTML, but if the browser on your mobile device cannot process HTML, XML is also a good choice, because MIDlet can parse XML documents.
- JSP bean code optimization
- Detailed introduction to JSP environment configuration Scheme
- Use stored procedures in JSP JSTL
- Which of ASP. NET, JSP, and PHP is better?
- JSP-related software