|
实现功能:
用手机客户端进行登录服务器,然后返回消息进行交互.
服务器代码:
loginservlet:
package com;
import java.io.bytearrayoutputstream;
import java.io.dataoutputstream;
import java.io.ioexception;
import java.io.outputstream;
import javax.servlet.servletexception;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
/** *//*******************************************************************************
*
* @author zdw
*
*/
@suppresswarnings("serial")
public class loginservlet extends httpservlet
{
public void doget(httpservletrequest request, httpservletresponse response)
throws servletexception, ioexception
{
this.dopost(request, response);
}
public void dopost(httpservletrequest request, httpservletresponse response)
throws servletexception, ioexception
{
// 得到客户端传入的数据(用户名和密码)
string username = request.getparameter("username");
string password = request.getparameter("password");
// 构建输出流
bytearrayoutputstream baos = new bytearrayoutputstream();
dataoutputstream dos = new dataoutputstream(baos);
// 逻辑操作(这里写你的逻辑判断)
if ("zdw".equals(username) && "admin".equals(password))
{
// 响应数据
dos.writeutf("true");
} else
{
// 响应数据
dos.writeutf("false");
}
//
byte[] data = baos.tobytearray();
// 设置服务器响应参数
response.setstatus(httpservletresponse.sc_ok);
response.setcontentlength(data.length);
response.setcontenttype("application/octet-stream");
outputstream os = response.getoutputstream();
os.write(data);
os.close();
}
}
手机客户端代码:
loginform:
package com;
import java.io.datainputstream;
import java.io.ioexception;
import java.io.outputstream;
import javax.microedition.io.connector;
import javax.microedition.io.httpconnection;
import javax.microedition.lcdui.alert;
import javax.microedition.lcdui.alerttype;
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;
/** *//**
* 用http方式与服务器交互
*
* @author zdw
*
*/
public class loginform extends midlet implements commandlistener
{
private form form = null;
private display display = display.getdisplay(this);;
private command login = null;
private command exit = null;
private textfield username = null;
private textfield password = null;
private alert alert = null;
private alert error = null;
public loginform()
{
form = new form("用户登录");
display.setcurrent(form);
login = new command("登录", command.screen, 1);
exit = new command("退出", command.exit, 1);
form.addcommand(login);
form.addcommand(exit);
username = new textfield("用户名", "", 20, textfield.any);
password = new textfield("密码", "", 20, textfield.password);
form.append(username);
form.append(password);
form.setcommandlistener(this);
}
public void initalertok()
{
alert = new alert("提示", "登录成功!!\r\n您的用户名为:" username.getstring()
"\r\n密码为:" password.getstring(), null, alerttype.info);
alert.settimeout(alert.forever);
display.setcurrent(alert);
}
public void initalerterror()
{
error = new alert("提示", "登录失败,用户名或密码错误", null, alerttype.error);
display.setcurrent(error);
}
protected void startapp() throws midletstatechangeexception
{
}
/** *//**
* 事件处理
*/
public void commandaction(command cmd, displayable dis)
{
// 点击退出按钮事件
if (cmd.getcommandtype() == command.exit)
{
system.out.println("exit");
this.notifydestroyed();
}
if (cmd == login)
{
// 必须开启独立线程来处理http请求,否则会造成死锁
new thread(new runnable()
{
public void run()
{
try
{
inturnserver();
} catch (exception e)
{
e.printstacktrace();
}
}
}).start();
}
}
/** *//***************************************************************************
* 与服务器交互相关代码
*/
public void inturnserver()
{
try
{
// 服务器请求地址
string url = "http://localhost:8888/loginweb/loginservlet";
// 用户输入的用户名
string username = this.username.getstring();
// 用户输入的密码
string password = this.password.getstring();
// 用url建立一个http连接(安全的)
httpconnection conn = (httpconnection) connector.open(url);
// 设置请求类型为post
conn.setrequestmethod(httpconnection.post);
// 设置一般的请求属性
conn.setrequestproperty("content-type",
"application/x-www-form-urlencoded");
conn.setrequestproperty("user-agent",
"profile/midp-1.0 configuration/cldc-1.0");
conn.setrequestproperty("content-language", "en-us");
conn.setrequestproperty("accept", "application/octet-stream");
conn.setrequestproperty("connection", "close");
// 要发送的数据
string formdata = "username=" username "&password=" password;
// 转换显字节流
byte[] data = formdata.getbytes();
// 设置写入流的长度
conn.setrequestproperty("content-length", integer
.tostring(data.length));
outputstream os = conn.openoutputstream();
os.write(data);
os.close();
// 得到http响应代码
int rc = conn.getresponsecode();
// 正常响应
if (rc == httpconnection.http_ok)
{
// 构建输入流
datainputstream dism = new datainputstream(conn
.openinputstream());
// 读取服务器返回的字节流
string result = dism.readutf();
dism.close();
// 判断
if (result.equals("true"))
{
// 显示登录成功
this.initalertok();
}
if (result.equals("false"))
{
// 显示登录失败
this.initalerterror();
// 将输入框置空
this.username.delete(0, this.username.getstring().length());
this.password.delete(0, this.password.getstring().length());
}
}
} catch (ioexception e)
{
e.printstacktrace();
}
}
protected void destroyapp(boolean arg0) throws midletstatechangeexception
{
}
protected void pauseapp()
{
}
}
源码下载:
注意此工程为myeclipse工程,您需要安装wtk和tomcat才能正常运行此程序.
登录图:
|