最新更新 sitemap 网站制作设计本站搜索
网页设计
国外网站 韩国网站 个人主页 手提袋设计 CSS 网页特效 平面设计 网站设计 Flash CMS技巧 服装网站 php教程 photoshop 画册 服务器选用 数据库 Office
虚拟主机 域名注册 云主机 网页设计 客服QQ:8208442
当前位置:首页 > 编程开发 > jsp教程

J2ME开发中如何从MIDlet中调用JSP页面

日期:12-10    来源:中国设计秀    作者:cnwebshow.com

首先,我将讨论一下HttpConnection接口,这个接口可以用来建立Http连接nQx中国设计秀

HttpConnection 接口nQx中国设计秀

Connected Limited Device Configuration(有限连接设备配置。简称CLDC)。提供了一套用于网络连接的类,就是普通连接框架?一种平台独立连接框架,提供了一种分层的连接接口,它的实现操作系统由具体的设备简表提供(比如Mobile Information Device PRofile(MIDP))。nQx中国设计秀

MIDP通过提供支持HTTP的HttpConnection 框架来实现扩展CLDC的一般连接框架的作用。所有MIDP的应用程序实现都要求支持HTTP,这主要是因为HTTP即可以通过使用基于ip的协议(如TCP/IP)也可以通过使用非IP协议(如WAP)来实现。nQx中国设计秀

所有的连接都是使用Connector类的open()方法来创建的,如果连接成功的话,这个方法就返回一个实现某种普通连接借口的对象,举一个例子吧,下面的代码段可以用来打开一个到某个URL的HTTP连接。nQx中国设计秀

String url = "http://www.ora.com/whatif.jsp";;nQx中国设计秀

HttpConnection connection = Connector.open(url);nQx中国设计秀

一旦一个连接被建立后,就可以设置属性了,然后就可以建立I/O流来发送或接收数据。举个例子,请看下面的这一小段代码,用来设置属性并建立输入/输出流。nQx中国设计秀

// 设置 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(); nQx中国设计秀
 nQx中国设计秀

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

代码1:nQx中国设计秀

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);%>nQx中国设计秀
 nQx中国设计秀

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

现在,让我们看看如何写一个MIDlet来调用这个JSP页面,我们将使用POST请求方法来调用它,这就意味着被传送到JSP页面的数据不是使用URL编码的,而是以一段单独的信息传入,这段MIDlet代码如代码段2所示。nQx中国设计秀

代码2:nQx中国设计秀

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) {;}; };};}; nQx中国设计秀
 nQx中国设计秀

nQx中国设计秀
InvokeJSPMidlet代码指定了要被调用的JSP页面的URL,然后就创建了两个命令按钮,然后创建一个text字段,可以让用户在里面输入姓名。在InvokeJSP()方法中,将建立一个到这个URL的HTTP连接,然后再建立I/O流,MIDlet使用输出流来发送数据到JSP页面,接着再使用输入流从JSP页面中接收数据,注意,在本例中我们将发送姓名到JSP页面中,其实它也只是向你演示一下数据如何在MIDlet和页面之间流通。nQx中国设计秀

在代码段2中,应当注意的事情是为了使JSP页面使用getParameter()从name变量中取得数据的值,

本文引用地址:/bc/article_46708.html
网站地图 | 关于我们 | 联系我们 | 网站建设 | 广告服务 | 版权声明 | 免责声明