import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;
import java.util.*;
public class Midlet extends MIDlet implements CommandListener ,Runnable{
Display display;
String tf1Str,tf2Str;
TextField tf1,tf2;
Form scen,inputForm,returnForm;
Command cmdSend,cmdBack,cmdExit,con;
String defaultURL;
String query=null;
public Midlet(){
tf1=new TextField(" 学号", "", 10, TextField.NUMERIC);
tf2=new TextField(" 密码", "", 10, TextField.PASSWORD);
con=new Command("进入",Command.SCREEN, 1);
cmdExit=new Command("退出", Command.EXIT, 0);
cmdSend=new Command("确认", Command.SCREEN, 1);
cmdBack=new Command("返回", Command.SCREEN, 1);
Ticker tk=new Ticker("欢迎使用学生成绩查询系统");
scen=new Form(" 众志成城 战胜灾难");
inputForm=new Form(" 用户登陆");
try{ Image img=Image.createImage("/1.png");
Image img2=Image.createImage("/3.jpg");
inputForm.append(img);
scen.append(img2);
}catch(IOException e){ System.out.println("图象加载出错 : "+e.getMessage());}
inputForm.append(" ");
inputForm.append(tf1);
inputForm.append(tf2);
inputForm.setTicker(tk);
scen.addCommand(cmdExit);
scen.addCommand(con);
inputForm.addCommand(cmdExit);
inputForm.addCommand(cmdSend);
inputForm.setCommandListener(this);
scen.setCommandListener(this);
}
public void startApp() throws MIDletStateChangeException {
display=Display.getDisplay(this);
display.setCurrent(scen);
}
public void pauseApp(){}
public void destroyApp(boolean unconditional){
notifyDestroyed();
}
public void commandAction(Command c,Displayable d){
if(c==cmdExit){destroyApp(true);}
if(c==con){display.setCurrent(inputForm);}
if(c==cmdBack){display.setCurrent(inputForm);}
if(c==cmdSend){
query="";
defaultURL="http://localhost:8080/ServletApp";
tf1Str=tf1.getString();
tf2Str=tf2.getString();
query="num="+tf1Str+"&"+"code="+tf2Str;
defaultURL+="?"+query;
try{
Thread t=new Thread(this);
t.start();
System.out.println("线程运行正常吗 : " +t.isAlive());
System.out.println("手机传递给SERVLET的查询参数为:"+tf1Str+"\n");
} catch(Exception e) {System.out.println(e.getMessage());}
}
}
public void run(){
try{ invokeServlet(defaultURL);
}catch(Exception ee){ ee.printStackTrace();}
}
public void invokeServlet(String url) throws IOException{
HttpConnection hc=null;
DataInputStream dis=null;
StringBuffer sb=null;
try{
hc=(HttpConnection) Connector.open(url,Connector.READ_WRITE);
hc.setRequestMethod(HttpConnection.GET);
hc.setRequestProperty("User-Agent", "Profile/MIDP-1.0 Configuration/CLDC-1.0");
hc.setRequestProperty("Content-Language", "en-CA");
hc.setRequestProperty("Content-Type", "application/GBK");
dis= (DataInputStream) hc.openDataInputStream();
try
{
int ch;
sb=new StringBuffer();
while((ch=dis.read())!=-1) {
sb.append((char)ch);
}
}
catch(EOFException e)
{//如果此输入流在读取所有字节之前到达末尾
System.out.println("str is null,exit!---" +e.getMessage());
}
System.out.println("手机接收到SERVLET传来的查询结果为:"+sb.toString()+"\n");
String str=new String(sb.toString().getBytes("ISO8859_1"),"gb2312");
System.out.println("转换后的查询结果为 :"+str);
returnForm=new Form("以下是您查询的结果");
Ticker tik=new Ticker("书山有路勤为径");
returnForm.setTicker(tik);
if(str.equals("false")){
str="\n"+" 对不起,您输入的密码或用户名有误"+" "+"请核对后重新输入";
}
returnForm.append(str);
returnForm.addCommand(cmdBack);
returnForm.setCommandListener(this);
}
finally{
if (dis!=null){dis.close();
}
if(hc!=null){hc.close();
}
display.setCurrent(returnForm);
}
}
}