J2ME/J2EE實(shí)現(xiàn)用戶登錄交互 實(shí)現(xiàn)代碼
更新時(shí)間:2009年07月01日 22:45:49 作者:
用手機(jī)客戶端進(jìn)行登錄服務(wù)器,然后返回消息進(jìn)行交互.
服務(wù)器代碼:
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
{
// 得到客戶端傳入的數(shù)據(jù)(用戶名和密碼)
String username = request.getParameter("username");
String password = request.getParameter("password");
// 構(gòu)建輸出流
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
// 邏輯操作(這里寫你的邏輯判斷)
if ("zdw".equals(username) && "admin".equals(password))
{
// 響應(yīng)數(shù)據(jù)
dos.writeUTF("true");
} else
{
// 響應(yīng)數(shù)據(jù)
dos.writeUTF("false");
}
//
byte[] data = baos.toByteArray();
// 設(shè)置服務(wù)器響應(yīng)參數(shù)
response.setStatus(HttpServletResponse.SC_OK);
response.setContentLength(data.length);
response.setContentType("application/octet-stream");
OutputStream os = response.getOutputStream();
os.write(data);
os.close();
}
}
源碼下載:點(diǎn)此下載
注意此工程為MyEclipse工程,您需要安裝wtk和tomcat才能正常運(yùn)行此程序.
登錄圖:
LoginServlet:
package com;
復(fù)制代碼 代碼如下:
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
{
// 得到客戶端傳入的數(shù)據(jù)(用戶名和密碼)
String username = request.getParameter("username");
String password = request.getParameter("password");
// 構(gòu)建輸出流
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
// 邏輯操作(這里寫你的邏輯判斷)
if ("zdw".equals(username) && "admin".equals(password))
{
// 響應(yīng)數(shù)據(jù)
dos.writeUTF("true");
} else
{
// 響應(yīng)數(shù)據(jù)
dos.writeUTF("false");
}
//
byte[] data = baos.toByteArray();
// 設(shè)置服務(wù)器響應(yīng)參數(shù)
response.setStatus(HttpServletResponse.SC_OK);
response.setContentLength(data.length);
response.setContentType("application/octet-stream");
OutputStream os = response.getOutputStream();
os.write(data);
os.close();
}
}
源碼下載:點(diǎn)此下載
注意此工程為MyEclipse工程,您需要安裝wtk和tomcat才能正常運(yùn)行此程序.
登錄圖:
您可能感興趣的文章:
- 實(shí)戰(zhàn) J2EE 開發(fā)購物網(wǎng)站 - 創(chuàng)建數(shù)據(jù)庫
- J2EE 開發(fā)購物網(wǎng)站 經(jīng)驗(yàn)篇 - 建表
- 實(shí)戰(zhàn) J2EE 開發(fā)購物網(wǎng)站 二
- MyEclipse5.0M1注冊(cè)碼+eclipse3.2[J2EE]
- SUN的《AJAX與J2EE》全文譯了
- 終于了解了下.net 和 j2ee的區(qū)別
- 經(jīng)常聽朋友說什么J2EE,終于知道點(diǎn)什么是J2EE了,汗一個(gè)
- J2EE項(xiàng)目代碼編寫規(guī)范分享
- 圖解Eclipse j2ee開發(fā)環(huán)境的搭建過程
- JDK、J2EE、J2SE、J2ME四個(gè)易混淆概念區(qū)分
- 分享J2EE的13種核心技術(shù)
相關(guān)文章
JSP中使用JDBC連接MySQL數(shù)據(jù)庫的詳細(xì)步驟
本文主要介紹了JSP中使用JDBC連接MySQL數(shù)據(jù)庫的詳細(xì)步驟,對(duì)于初學(xué)者具有很好的參考價(jià)值,需要的朋友一起來看下吧2016-12-12
淺談request.getinputstream只能讀取一次的問題
下面小編就為大家?guī)硪黄獪\談request.getinputstream只能讀取一次的問題。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-03-03
JSP 開發(fā)之Spring BeanUtils組件使用
這篇文章主要介紹了JSP 開發(fā)之Spring BeanUtils組件使用的相關(guān)資料,這里提供實(shí)例幫助大家理解如何使用Spring BeanUtils組件,需要的朋友可以參考下2017-08-08
Spring 中 @Service 和 @Resource 注解的區(qū)別
這篇文章主要介紹了Spring @Service 和 @Resource 注解的區(qū)別的相關(guān)資料,需要的朋友可以參考下2017-03-03
用jsp頁面生成隨機(jī)的驗(yàn)證數(shù)字碼示例
生成隨機(jī)的驗(yàn)證數(shù)字碼有很多種方法,下面為大家將介紹下如何用jsp頁面生成隨機(jī)的驗(yàn)證碼,感興趣的朋友可以參考下
2013-09-09 
