J2ME/J2EE实现用户登录交互
时间:2011-07-28
实现功能:
用手机客户端进行登录服务器,然后返回消息进行交互.
服务器代码:
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();
}
}
J2ME/J2EE糞?喘薩鞠村住札(2)
扮寂:2011-07-28
返字人薩極旗鷹:
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 = n
|