?? mobileservlet.java
字號:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;
import java.io.DataInputStream;
public class MobileServlet extends MIDlet implements CommandListener
{
Display display;
TextField tf1,tf2;
String tf1Str,tf2Str;
Form inputForm,returnForm;
Command cmdSend,cmdBack;
private static String defaultURL = "http://192.167.0.12:8080/MobileServlet/ms_servlet";
public MobileServlet()
{
display = Display.getDisplay(this);
tf1 = new TextField("input first param:","盧東方",20,TextField.ANY);
tf2 = new TextField("input second param:","王桃群",20,TextField.ANY);
cmdSend = new Command("Send",Command.SCREEN,1);
cmdBack = new Command("Back",Command.SCREEN,1);
inputForm = new Form("pls input the param:");
inputForm.append(tf1);
inputForm.append(tf2);
inputForm.addCommand(cmdSend);
inputForm.setCommandListener(this);
tf1Str = tf1.getString();
tf2Str = tf2.getString();
}
public void startApp() throws MIDletStateChangeException
{
//display.setCurrent(inputForm);
try{ invokeServlet(defaultURL);
}catch(Exception e)
{System.out.println(e.getMessage());}
}
public void invokeServlet(String url) throws IOException
{
HttpConnection hc = null;
DataOutputStream dos = null;
DataInputStream dis = null;
try{
hc = (HttpConnection)Connector.open(url,Connector.READ_WRITE);
//設(shè)置請求屬性
hc.setRequestMethod(HttpConnection.POST);
//設(shè)置為POST請求方式,默認的請求方式是GET
hc.setRequestProperty("IF-Modified-Since","15 Oct 2003 08:47:14 GMT");
hc.setRequestProperty("User-Agent","Profile/MIDP-1.0 Configuration/CLDC-1.0");
hc.setRequestProperty("Content-Language","en-CA");
hc.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
hc.setRequestProperty("Connection","Keep-Alive");
//Connection頭可以控制MIDlet和Web服務(wù)器之間保持"keep alive"特色。
//"keep alive"特色是指在MIDlet和Web服務(wù)器間始終使用同一個HTTP連接來多次傳遞數(shù)據(jù)
//(在通常情況下,HTTP是無連接的協(xié)議,每次數(shù)據(jù)傳輸完畢后都將斷開連接,
//而下次傳遞數(shù)據(jù)之前將重新建立連接)
//發(fā)送請求參數(shù)到servlet
dos = hc.openDataOutputStream();
hc.setRequestProperty("Content_Length",Integer.toString(tf1Str.length()));
dos.writeUTF(tf1Str);
hc.setRequestProperty("Content_Length",Integer.toString(tf2Str.length()));
dos.writeUTF(tf2Str);
//用于發(fā)送請求參數(shù)給servlet
System.out.println("手機傳遞給servlet的第一個參數(shù)為:"+ tf1Str);
//主要起調(diào)試的作用,調(diào)試的結(jié)果將顯示在WTK的控制臺中
System.out.println("手機傳遞給servlet的第一個參數(shù)為:"+ tf2Str);
dos.flush();
dos.close();
dis = new DataInputStream(hc.openInputStream());
System.out.println("測試成功測試成功!");
int rc = hc.getResponseCode();
if(rc == HttpConnection.HTTP_OK)
{
//接收servlet響應(yīng)數(shù)據(jù)
String return1Str = dis.readUTF();
String return2Str = dis.readUTF();
System.out.println("手機接收到servlet端傳來的第一個參數(shù)為:" + return1Str);
//主要起調(diào)試的作用,調(diào)試的結(jié)果將顯示在WTK的控制臺中
System.out.println("手機接收到servlet端傳來的第二個參數(shù)為:" + return2Str);
returnForm = new Form("返回的結(jié)果");
returnForm.append(return1Str);
returnForm.append("\n");
//將返回的結(jié)果append到resultForm中
returnForm.append(return2Str);
returnForm.addCommand(cmdBack);
returnForm.setCommandListener(this);
} else{System.out.println(rc);}
}finally{
if (dis != null)
{dis.close();}
if (dos != null)
{dos.close();}
if (hc != null)
{hc.close();}
}
display.setCurrent(returnForm);
}
public void pauseApp(){}
public void destroyApp(boolean unconditional){}
public void commandAction(Command c,Displayable d){
if(c == cmdBack)
{display.setCurrent(inputForm);}
if(c == cmdSend)
{tf1Str = tf1.getString();
tf2Str = tf2.getString();
//try{ invokeServlet(defaultURL);
//}catch(Exception e)
// {System.out.println(e.getMessage());}
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -