日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
如何從MIDlet中調(diào)用JSP頁(yè)面

首先,我將討論一下HttpConnection接口,這個(gè)接口可以用來(lái)建立Http連接

HttpConnection 接口

Connected Limited Device Configuration(有限連接設(shè)備配置。簡(jiǎn)稱CLDC)提供了一套用于網(wǎng)絡(luò)連接的類,就是普通連接框架。一種平臺(tái)獨(dú)立連接框架,提供了一種分層的連接接口,它的實(shí)現(xiàn)操作系統(tǒng)由具體的設(shè)備簡(jiǎn)表提供(比如Mobile Information Device Profile(MIDP))。

MIDP通過(guò)提供支持HTTP的HttpConnection 框架來(lái)實(shí)現(xiàn)擴(kuò)展CLDC的一般連接框架的作用。所有MIDP的應(yīng)用程序?qū)崿F(xiàn)都要求支持HTTP,這主要是因?yàn)镠TTP即可以通過(guò)使用基于IP的協(xié)議(如 TCP/IP)也可以通過(guò)使用非IP協(xié)議(如WAP)來(lái)實(shí)現(xiàn)。

所有的連接都是使用Connector類的open()方法來(lái)創(chuàng)建的,如果連接成功的話,這個(gè)方法就返回一個(gè)實(shí)現(xiàn)某種普通連接借口的對(duì)象,舉一個(gè)例子吧,下面的代碼段可以用來(lái)打開(kāi)一個(gè)到某個(gè)URL的HTTP連接。

String url =http://www.ora.com/whatif.jsp;;

HttpConnection connection = Connector.open(url);

一但一個(gè)連接被建立后,就可以設(shè)置屬性了,然后就可以建立I/O流來(lái)發(fā)送或接收數(shù)據(jù)。舉個(gè)例子,請(qǐng)看下面的這一小段代碼,用來(lái)設(shè)置屬性并建立輸入/輸出流。 // 設(shè)置 HTTP 屬性

 
 
 
  1. // 設(shè)置 HTTP 屬性  
  2. connection.setRequestMethod(HttpConnection.POST);  
  3. connection.setRequestProperty("IF-Modified-Since","22 Dec 2001 16:33:19 GMT");  
  4. connection.setRequestProperty("User-Agent","Profile/MIDP-1.0 Configuration/CLDC-1.0");  
  5. connection.setRequestProperty("Content-Language", "en-CA");  
  6. // 創(chuàng)建I/O流  
  7. InputStream is = connection.openInputStream();  
  8. OutputStream os = connection.openOutputStream(); 

這個(gè)JSP里面希望取得一個(gè)名為name 的變量的值,一旦這個(gè)值被取得,就會(huì)創(chuàng)建一個(gè)Date的實(shí)例,然后name和date的值就會(huì)被打到客戶端中的輸出流中。

現(xiàn)在,讓我們看看如何寫(xiě)一個(gè)MIDlet來(lái)調(diào)用這個(gè)JSP頁(yè)面,我們將使用POST請(qǐng)求方法來(lái)調(diào)用它,這就意味著被傳送到JSP頁(yè)面的數(shù)據(jù)不是使用URL編碼的,而是以一段單獨(dú)的信息傳入,這段MIDlet代碼如代碼段2所示。

代碼2:

 
 
 
  1. InvokeJSPMidlet.java  
  2. import javax.microedition.lcdui.*;  
  3. import javax.microedition.midlet.*;  
  4. import javax.microedition.io.*;  
  5. import java.io.*;  
  6. public class InvokeJSPMidlet extends MIDlet implements CommandListener {;  
  7. Display display = null;  
  8. // name 字段  
  9. TextField name = null;  
  10. form form;  
  11. String url = "http://127.0.0.1:8080/examples/jsp/today.jsp";;  
  12. static final Command callCommand = new Command("date?", Command.OK, 2);  
  13. static final Command clearCommand = new Command("clear", Command.STOP, 2);  
  14. String myname;  
  15. public InvokeJSPMidlet() {;  
  16. display = Display.getDisplay(this);  
  17. name = new TextField("Name:", " ", 25, TextField.ANY);  
  18. form = new form("Invoke JSP");  
  19. };  
  20. public void startApp() throws MIDletStateChangeException {;  
  21. form.append(name);  
  22. form.addCommand(clearCommand);  
  23. form.addCommand(callCommand);  
  24. form.setCommandListener(this);  
  25. display.setCurrent(form);  
  26. };  
  27. public void pauseApp() {;  
  28. };  
  29. public void destroyApp(boolean unconditional) {;  
  30. notifyDestroyed();  
  31. };  
  32. void invokeJSP(String url) throws IOException {;  
  33. HttpConnection c = null;  
  34. InputStream is = null;  
  35. OutputStream os = null;  
  36. StringBuffer b = new StringBuffer();  
  37. TextBox t = null;  
  38. try {;  
  39. c = (HttpConnection)Connector.open(url);  
  40. c.setRequestMethod(HttpConnection.POST);  
  41. c.setRequestProperty("IF-Modified-Since", "29 Dec 2001 15:17:19 GMT");  
  42. c.setRequestProperty("User-Agent","Profile/MIDP-1.0 Configuration/CLDC-1.0");  
  43. c.setRequestProperty("Content-Language", "en-CA");  
  44. c.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");  
  45. os = c.openOutputStream();  
  46. os.write(("name="+myname).getBytes());  
  47. os.flush();  
  48. is = c.openDataInputStream();  
  49. int ch;  
  50. while ((ch = is.read()) != -1) {;  
  51. b.append((char) ch);  
  52. System.out.print((char)ch);  
  53. };  
  54. t = new TextBox("Date", b.toString(), 1024, 0);  
  55. t.setCommandListener(this);  
  56. }; finally {;  
  57. if(is!= null) {;  
  58. is.close();  
  59. };  
  60. if(os != null) {;  
  61. os.close();  
  62. };  
  63. if(c != null) {;  
  64. c.close();  
  65. };  
  66. };  
  67. display.setCurrent(t);  
  68. };  
  69. public void commandAction(Command c, Displayable d) {;  
  70. String label = c.getLabel();  
  71. if(label.equals("clear")) {;  
  72. destroyApp(true);  
  73. };   
  74. else if (label.equals("date?")) {;  
  75. myname = name.getString();  
  76. try {;  
  77. invokeJSP(url);  
  78. };catch(IOException e) {;  
  79. };  
  80. };  
  81. };  
  82. }; 

InvokeJSPMidlet代碼指定了要被調(diào)用的JSP頁(yè)面的URL,然后就創(chuàng)建了兩個(gè)命令按鈕,然后創(chuàng)建一個(gè)text字段,可以讓用戶在里面輸入姓名。在InvokeJSP()方法中,將建立一個(gè)到這個(gè)URL的HTTP連接,然后再建立I/O流,MIDlet使用輸出流來(lái)發(fā)送數(shù)據(jù)到JSP頁(yè)面,接著再使用輸入流從JSP頁(yè)面中接收數(shù)據(jù),注意,在本例中我們將發(fā)送姓名到JSP頁(yè)面中,其實(shí)它也只是向你演示一下數(shù)據(jù)如何在MIDlet和頁(yè)面之間流通。

在代碼段2中,應(yīng)當(dāng)注意的事情是為了使JSP頁(yè)面使用getParameter()從name變量中取得數(shù)據(jù)的值,你必須設(shè)置Content-Type屬性為application/x-www-form-urlencoded.

小結(jié)

本文只是演示如何從MIDlet中調(diào)用JSP頁(yè)面,InvokeJSPMidlet還可以很容易的修改來(lái)實(shí)現(xiàn)調(diào)用其他的JSP的目的。但是注意,JSP主要和HTML配合使用,但是如果你的移動(dòng)設(shè)備中的瀏覽器不能處理HTML的話,那么XML也是一個(gè)非常好的選擇,因?yàn)镸IDlet可以解析XML文檔。


本文題目:如何從MIDlet中調(diào)用JSP頁(yè)面
網(wǎng)頁(yè)鏈接:http://www.dlmjj.cn/article/dpojicp.html