2652338705561663627  
package example;
import java.io.DataInputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class HTTPMIDlet extends MIDlet implements CommandListener {
private Form frm=new Form("HTTP測試");
private TextField tfAcc=new TextField("輸入帳號","",10,TextField.ANY );
private TextField tfPass=new TextField("輸入密碼","",10,TextField.PASSWORD);
private Command cmdLogin=new Command("登陸",Command.BACK,1);
private Command cmdExit=new Command("退出",Command.SCREEN,1);
private Display display;
private HttpConnection hc=null;
private DataInputStream dis=null;
public HTTPMIDlet() {
display=Display.getDisplay(this);
display.setCurrent(frm);
frm.append(tfAcc);
frm.append(tfPass);
frm.addCommand(cmdLogin);
frm.addCommand(cmdExit);
frm.setCommandListener(this);

}

public void commandAction(Command c, Displayable d) {
if(c==cmdLogin)
{
ValidataThread vt =new ValidataThread();
vt.start();
return;
}
if(c==cmdExit)
{
this.notifyDestroyed();
return;
}
}

protected void startApp() throws MIDletStateChangeException {
}

protected void pauseApp() {
}

protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
try{
hc.close();
dis.close();
}catch(Exception ex){}
}

class ValidataThread extends Thread{
public void run(){
try{
String acc=tfAcc.getString(); //帳號字元竄
String pass=tfPass.getString(); //密碼帳號
//打開物件
String url="HTTP://127.0.0.1:9999/login.jsp?acc="+acc+"&pass="+pass;
hc=(HttpConnection)Connector.open(url);
//打開資料輸入流
dis=hc.openDataInputStream();
int length=(int)hc.getLength(); //獲得輸入流的長度
byte[] b= new byte[length]; //定義長度為length長度的位元組陣列
dis.read(b); //將資料輸入流存放在位元組陣列中
frm.append(new String(b)); //把位元組陣列轉化為字串,追加到Form中
}catch(Exception ex){ex.printStackTrace();}
finally{
if(hc!=null){
hc=null;
}
if(dis!=null){
dis=null;
}
}
}
}

}

jsp代碼如下:
<%
request.setCharacterEncoding("UTF-8");
String acc=request.getParameter("acc");
String pass=request.getParameter("pass");
if(acc.equals(pass)){
out.print("登陸成功"+"\n");
}
else{
out.print(acc+"登陸失敗"+"\n");
}
%>
創作者介紹
創作者 shadow 的頭像
shadow

資訊園

shadow 發表在 痞客邦 留言(0) 人氣()