<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    我的漫漫程序之旅

    專注于JavaWeb開發
    隨筆 - 39, 文章 - 310, 評論 - 411, 引用 - 0
    數據加載中……

    [原創]J2ME/J2EE實現用戶登錄交互

    實現功能:
    用手機客戶端進行登錄服務器,然后返回消息進行交互.

    服務器代碼:
    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(0this.username.getString().length());
                        
    this.password.delete(0this.password.getString().length());
                    }

                }

            }
     catch (IOException e)
            
    {
                e.printStackTrace();
            }

        }


        
    protected void destroyApp(boolean arg0) throws MIDletStateChangeException
        
    {

        }


        
    protected void pauseApp()
        
    {

        }


    }


    源碼下載:點此下載

    注意此工程為MyEclipse工程,您需要安裝wtk和tomcat才能正常運行此程序.
    登錄圖:

    posted on 2008-06-23 17:41 々上善若水々 閱讀(3421) 評論(9)  編輯  收藏

    評論

    # re: [原創]J2ME/J2EE實現用戶登錄交互[未登錄]  回復  更多評論   

    麻雀雖小,五臟俱全.

    收藏.
    2008-06-23 17:57 |

    # re: [原創]J2ME/J2EE實現用戶登錄交互  回復  更多評論   

    用手機客戶端進行登錄服務器,這個功能真是我工作中要學習的,謝謝。
    2008-06-23 21:58 | DVD比價

    # re: [原創]J2ME/J2EE實現用戶登錄交互  回復  更多評論   

    謝謝不錯的案例
    2008-06-24 07:40 | Java_do

    # re: [原創]J2ME/J2EE實現用戶登錄交互[未登錄]  回復  更多評論   

    在某些情況下username和password可能還需要encoding一下才行
    2008-06-24 09:46 | Gary

    # re: [原創]J2ME/J2EE實現用戶登錄交互[未登錄]  回復  更多評論   

    學習,還有些細節方面的沒看懂!
    2008-06-25 18:54 | 石頭

    # re: [原創]J2ME/J2EE實現用戶登錄交互  回復  更多評論   

    不錯,學習了.
    線程創建那裡,不錯.
    2008-08-14 13:53 | mooring

    # re: [原創]J2ME/J2EE實現用戶登錄交互  回復  更多評論   

    網上關于這方面的內容還真少。謝。
    2008-11-03 17:55 | log

    # re: [原創]J2ME/J2EE實現用戶登錄交互  回復  更多評論   

    看了這個 想在畢業設計中加入 手機客戶端 挺酷的 能不能給一些這方面的資料呀 我的郵箱522315678@qq.com
    2009-03-24 13:29 | 爽哦

    # re: [原創]J2ME/J2EE實現用戶登錄交互  回復  更多評論   

    不錯!感謝樓主你的分享!
    2011-11-15 19:21 | tianyake

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 亚洲国产日韩在线观频| 免费AA片少妇人AA片直播| 日日操夜夜操免费视频| 亚洲粉嫩美白在线| **一级毛片免费完整视| 久久青青成人亚洲精品| a级毛片黄免费a级毛片| 久久国产成人精品国产成人亚洲| 国产亚洲午夜精品| 亚洲 自拍 另类小说综合图区| 亚洲AV无码AV吞精久久| 国产a级特黄的片子视频免费| 国产精品亚洲精品久久精品| 青青青国产免费一夜七次郎| 亚洲AV无码片一区二区三区| 国产精品嫩草影院免费| 三级片免费观看久久| 亚洲色欲一区二区三区在线观看 | 亚洲人成日本在线观看| 免费精品国产自产拍在| 亚洲日韩精品无码专区加勒比 | 337p日本欧洲亚洲大胆艺术| 精品熟女少妇a∨免费久久| 亚洲人成免费电影| 曰皮全部过程视频免费国产30分钟| 国产成人+综合亚洲+天堂| 久久伊人亚洲AV无码网站| 久久久久国产精品免费看| 亚洲性69影院在线观看| 日本高清免费不卡视频| 国内永久免费crm系统z在线 | 亚洲精品高清久久| 噼里啪啦电影在线观看免费高清| 精品无码专区亚洲| 亚洲大尺度无码专区尤物| 日韩亚洲国产高清免费视频| 日本亚洲高清乱码中文在线观看| 亚洲精品国产精品乱码在线观看| 中文字幕无码不卡免费视频| 黄色免费网址大全| 337p日本欧洲亚洲大胆精品555588|