📄 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);
//设置请求属性
hc.setRequestMethod(HttpConnection.POST);
//设置为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服务器之间保持"keep alive"特色。
//"keep alive"特色是指在MIDlet和Web服务器间始终使用同一个HTTP连接来多次传递数据
//(在通常情况下,HTTP是无连接的协议,每次数据传输完毕后都将断开连接,
//而下次传递数据之前将重新建立连接)
//发送请求参数到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);
//用于发送请求参数给servlet
System.out.println("手机传递给servlet的第一个参数为:"+ tf1Str);
//主要起调试的作用,调试的结果将显示在WTK的控制台中
System.out.println("手机传递给servlet的第一个参数为:"+ tf2Str);
dos.flush();
dos.close();
dis = new DataInputStream(hc.openInputStream());
System.out.println("测试成功测试成功!");
int rc = hc.getResponseCode();
if(rc == HttpConnection.HTTP_OK)
{
//接收servlet响应数据
String return1Str = dis.readUTF();
String return2Str = dis.readUTF();
System.out.println("手机接收到servlet端传来的第一个参数为:" + return1Str);
//主要起调试的作用,调试的结果将显示在WTK的控制台中
System.out.println("手机接收到servlet端传来的第二个参数为:" + return2Str);
returnForm = new Form("返回的结果");
returnForm.append(return1Str);
returnForm.append("\n");
//将返回的结果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());}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -