如何從MIDlet中調用JSP頁面
首先,我將討論一下HttpConnection接口,這個接口可以用來建立Http連接
HttpConnection 接口
Connected Limited Device Configuration(有限連接設備配置。簡稱CLDC)提供了一套用于網絡連接的類,就是普通連接框架。一種平臺獨立連接框架,提供了一種分層的連接接口,它的實現操作系統由具體的設備簡表提供(比如Mobile Information Device Profile(MIDP))。
MIDP通過提供支持HTTP的HttpConnection 框架來實現擴展CLDC的一般連接框架的作用。所有MIDP的應用程序實現都要求支持HTTP,這主要是因為HTTP即可以通過使用基于IP的協議(如 TCP/IP)也可以通過使用非IP協議(如WAP)來實現。
所有的連接都是使用Connector類的open()方法來創建的,如果連接成功的話,這個方法就返回一個實現某種普通連接借口的對象,舉一個例子吧,下面的代碼段可以用來打開一個到某個URL的HTTP連接。
String url =http://www.ora.com/whatif.jsp;;
HttpConnection connection = Connector.open(url);
一但一個連接被建立后,就可以設置屬性了,然后就可以建立I/O流來發送或接收數據。舉個例子,請看下面的這一小段代碼,用來設置屬性并建立輸入/輸出流。 // 設置 HTTP 屬性
- // 設置 HTTP 屬性
- 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");
- // 創建I/O流
- InputStream is = connection.openInputStream();
- OutputStream os = connection.openOutputStream();
這個JSP里面希望取得一個名為name 的變量的值,一旦這個值被取得,就會創建一個Date的實例,然后name和date的值就會被打到客戶端中的輸出流中。
現在,讓我們看看如何寫一個MIDlet來調用這個JSP頁面,我們將使用POST請求方法來調用它,這就意味著被傳送到JSP頁面的數據不是使用URL編碼的,而是以一段單獨的信息傳入,這段MIDlet代碼如代碼段2所示。
代碼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 字段
- 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:", " ", 25, 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", "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 = 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) {;
- };
- };
- };
- };
InvokeJSPMidlet代碼指定了要被調用的JSP頁面的URL,然后就創建了兩個命令按鈕,然后創建一個text字段,可以讓用戶在里面輸入姓名。在InvokeJSP()方法中,將建立一個到這個URL的HTTP連接,然后再建立I/O流,MIDlet使用輸出流來發送數據到JSP頁面,接著再使用輸入流從JSP頁面中接收數據,注意,在本例中我們將發送姓名到JSP頁面中,其實它也只是向你演示一下數據如何在MIDlet和頁面之間流通。
在代碼段2中,應當注意的事情是為了使JSP頁面使用getParameter()從name變量中取得數據的值,你必須設置Content-Type屬性為application/x-www-form-urlencoded.
小結
本文只是演示如何從MIDlet中調用JSP頁面,InvokeJSPMidlet還可以很容易的修改來實現調用其他的JSP的目的。但是注意,JSP主要和HTML配合使用,但是如果你的移動設備中的瀏覽器不能處理HTML的話,那么XML也是一個非常好的選擇,因為MIDlet可以解析XML文檔。
【編輯推薦】