?? invokejspmidlet.java
字號(hào):
package ch09.section10;
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;
//jsp頁(yè)面位于本地機(jī)
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");
}
//程序啟動(dòng)時(shí),創(chuàng)建窗體
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();
}
//訪問(wèn)JSP頁(yè)面
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);
//設(shè)置屬性
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");
//寫(xiě)入數(shù)據(jù)
os = c.openOutputStream();
os.write( ("name=" + myname).getBytes());
os.flush();
is = c.openDataInputStream();
int ch;
//讀取數(shù)據(jù)
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);
}
//響應(yīng)按鈕事件
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) {
}
}
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -