如何从MIDlet中调用JSP页面

开发 后端
本文只是演示如何从MIDlet中调用JSP页面,InvokeJSPMidlet还可以很容易的修改来实现调用其他的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 属性

  1. // 设置 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. // 创建I/O流  
  7. InputStream is = connection.openInputStream();  
  8. OutputStream os = connection.openOutputStream(); 

这个JSP里面希望取得一个名为name 的变量的值,一旦这个值被取得,就会创建一个Date的实例,然后name和date的值就会被打到客户端中的输出流中。

现在,让我们看看如何写一个MIDlet来调用这个JSP页面,我们将使用POST请求方法来调用它,这就意味着被传送到JSP页面的数据不是使用URL编码的,而是以一段单独的信息传入,这段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代码指定了要被调用的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文档。

【编辑推荐】

  1. JSP bean代码优化
  2. 详细介绍JSP环境配置方案
  3. 在JSP JSTL中使用存储过程
  4. ASP.NET、JSP和PHP究竟哪个好
  5. JSP相关软件介绍
责任编辑:佚名 来源: IT168
相关推荐

2009-02-05 17:09:02

动态图片JSPTomcat

2009-07-02 14:24:02

JSP读取数据库

2009-07-03 17:33:06

JSP中调用JavaB

2009-07-02 09:25:41

JSP实现页面跳转

2009-07-07 11:18:59

JSP Servlet

2009-06-10 17:03:36

JSP动态生成

2009-06-29 17:09:49

JavaBeanJSP

2011-05-04 09:29:22

2009-06-30 15:22:55

JSP页面

2009-07-09 09:46:24

激活Servlet

2009-07-03 18:12:49

JSP页面

2009-07-06 09:34:19

JSP页面

2009-07-01 18:50:29

Dreamweaver

2009-06-25 16:36:31

JBPM流程图

2009-03-05 10:10:52

饼图数据库JSP

2009-07-06 10:00:31

JSP页面传值

2012-05-04 09:40:01

JSP时间控件Java

2009-03-16 15:07:20

JSP分页window.openJSP表单

2009-07-06 18:23:56

Struts和JSPJSP页面

2009-07-03 17:48:34

JSP页面翻译
点赞
收藏

51CTO技术栈公众号