以文本方式查看主题

-  课外天地 李树青  (http://njcie.com/bbs/index.asp)
--  JavaME移动开发课件  (http://njcie.com/bbs/list.asp?boardid=18)
----  程序代码——Web登录认证  (http://njcie.com/bbs/dispbbs.asp?boardid=18&id=588)

--  作者:admin
--  发布时间:2008/10/30 21:26:13
--  程序代码——Web登录认证

JSP网页:
<%@page c  session="true"%>
<%
//获取用户名称和密码
String username=request.getParameter("username");
String password=request.getParameter("password");

//如果两者相等,表示认证成功
if(username.equals(password)) {
    out.println("success");
} else {
    out.println("error");
}
%>

MIDLET程序:
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

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.TextBox;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;

public class Exec extends MIDlet implements CommandListener {
    Form form = new Form("练习");
    Display display = null;
    TextBox tb = null;
    TextField tfUser = new TextField("用户名称", "", 20, TextField.ANY);
    TextField tfPassword = new TextField("用户名称", "", 20, TextField.ANY);
    InputStream in = null;
    Command ok = new Command("开始", Command.OK, 1);

    public Exec() {
        display = Display.getDisplay(this);
    }

    public void startApp() {
        form.append(tfUser);
        form.append(tfPassword);
        form.addCommand(ok);
        form.setCommandListener(this);
        display.setCurrent(form);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    public void commandAction(Command com, Displayable disp) {
        Downloader downloader = new Downloader(tfUser.getString(),tfPassword.getString(), this);
        Thread thread = new Thread(downloader);
        thread.start();
    }

    public void setTb(TextBox tb) {
        this.tb = tb;
        display.setCurrent(tb);
    }
}

class Downloader implements Runnable {
    String username;
    String password;
    Exec midlet;

    public Downloader(String username, String password, Exec midlet) {
        this.username = username;
        this.password = password;
        this.midlet = midlet;
    }

    public void run() {
        InputStream is = null;
        HttpConnection c = null;
        StringBuffer sb = new StringBuffer();
        String result = null;
        try {
            c = (HttpConnection) Connector
                    .open("http://localhost:8084/WebApplication2/action.jsp?username="
                            + username + "&password=" + password);
            c.setRequestMethod(HttpConnection.GET);
            int status = c.getResponseCode();
            if (status != HttpConnection.HTTP_OK)
                throw new IOException("Response code not ok");
            else {
                is = c.openInputStream();// 打开输入流,读入数据
                int ch;
                while ((ch = is.read()) != -1) {
                    sb.append((char) ch);
                }
            }
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        } finally {
            if (is != null)
                try {
                    is.close();
                } catch (IOException e) {
                    System.out.println(e.getMessage());
                }
            if (c != null)
                try {
                    c.close();
                } catch (IOException e) {
                    System.out.println(e.getMessage());
                }
        }
        try {
            result = new String(sb.toString().getBytes("ISO-8859-1"), "GB2312");
        } catch (UnsupportedEncodingException e) {
            System.out.println(e.getMessage());
        }
        TextBox tb = new TextBox("测试", result, result.length(), TextField.ANY);
        midlet.setTb(tb);
    }
}

[此贴子已经被作者于2010-12-12 18:44:41编辑过]