// 设置 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文档。
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/374079/viewspace-131048/ ,如需转载,请注明出处,否则将追究法律责任。