Java资源网

| JAVA基础 | 环境配置 | JDBC | 线程技术 | Socket编程 | JavaMail | JAVA与XML | 设计模式 | 技术新闻 | Java认证 | 程序人生 软件下载
| JSP&Servlet | Spring | Struts | Hibernate | JBuilder | Eclipse | WebService | EJB技术 | J2ME开发 | 应用服务器 | JXTA | Ajax
Articles search文章搜索
   关键字:
   类 别:
       
New download 最新下载
· [组件]HTML Parser 1.5
· [教程]WebSphere Studio应用教程
· [组件]JDom 1.0
· [工具]Junit3.8.1
· [教程]EJB编程及J2EE系统架构和设计
· [教程]EJB教程
· [教程]J2EE Tutorial中文版
· [教程]Java编程思想2(英文)
· [教程]java编程思想(完整版)
· [教程]Java网络编程
New articles 最新文章
· 设计移动 Web 服务
· 解析XML的时候完全忽略DTD
· 理解XML Schema XML Schema 初步
· 标签库的深入研究
· 提升JSP应用程序的七大绝招
· 如何使用JDOM对XML文件进行操作
· 处理XML字符串中特殊字符
· 利用Digester把XML转换成为Java对象
· 使用WebService 和RMI远程协作
· 使用Axis开发Web Service程序
Articles top 热门文章
· Eclipse基础--plugin插件安装(6644)
· eclipse+tomcat+lomboz的安装配置说明(4774)
· Java程序员就业前景(4584)
· Windows下JAVA环境变量的设置祥解(3788)
· Tomcat下JSP、Servlet和JavaBean环境的配置(3716)
· 使用links方式安装Eclipse插件(3698)
· 一个老程序员的心理话(3533)
· linux下jdk的安装与配置(3459)
· 初学者入门:Structs中基本配置入门(3334)
· Eclipse 运行命令行参数大全(3084)
您的位置:首页>>JSP和Servlet>>如何从MIDlet中调用JSP页面
如何从MIDlet中调用JSP页面
2006-04-05   来源:ChinaITLab  作者:ChinaITLab
  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 属性

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();

  下面让我们来研究一个例子,了解一下如何从MIDlet中调用JSP,我们调用JSP页面代码的程序段1如下所示:

  代码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);

%>

  这个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 = "80/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文档。

  --相关文章--
· 漫步j2ee之jsp技术(1) (2007-04-13)
· 提升JSP应用程序的七大绝招 (2007-04-13)
· 如何在JSP中处理中文 (2007-04-13)
· 第一章 taglibnbsp;原理和实现 (2007-04-13)
· 用jsp动态输出excel文档和中文乱码问题的解决 (2007-04-13)
· 可以自动跳转到出错页面的servlet jsp框架 (2007-04-13)

版权所有©2005-2006 JAVA资源网 渝ICP备05007591号 虚拟主机 | 关于我们 | 联系方式 | 广告业务 | 网站地图 | 友情链接