android用戶端向伺服器端驗證登陸方法的實現2

來源:互聯網
上載者:User

標籤:android   style   c   class   blog   code   

一、在上一篇文章中,我只是提到了其中一種方法來實現登陸

大家可以參見:

http://www.apkbus.com/android-45004-1-1.html      android擷取web伺服器端session並驗證登陸

http://blog.csdn.net/cainiao123hack/article/details/8255848   伺服器端向Android用戶端傳值——登入實現

http://zhidao.baidu.com/link?url=8g9EWhyUkUgUr1dh3_tEmmJgBcVLVVVJLtDrk_hJBm_-3n-SSBSItPdo1kjlC8MbeWyaK_fhgISCJbwGF4wQIa

請問如何寫一個java 伺服器端的servlet,然後可以當用戶端發出http請求的時候,可以接受請求,讀取資料庫

二、一些代碼的講解:

    什麼是servlet:一個小的,具有平台無關性,沒有圖形化使用者介面的java程式,他可以在很多方面擴充web服務的功能。

HttpServlet 類  :

參考文獻:http://blog.csdn.net/jay198746/article/details/4489571   

http://www.cnblogs.com/amboyna/archive/2007/09/18/897101.html    Java學習之Servlet-doGet()與doPost()

提供了一個處理 HTTP 協議的架構。
在這個類中的 service 方法支援例如 GET、POST 這樣的標準的 HTTP 方法。這一支援
過程是通過分配他們到適當的方法(例如 doGet、doPost)來實現的。

doGet方法:被這個類的service方法調用,用來處理一個HTTP GET操作。這個操作允許用戶端簡單的從一個HTTP伺服器獲得資源。對這個方法的重載將自動支援HEAD方法。安全的方法。

doPost方法:被service方法調用,用來處理一個HTTP POST操作。這個操作包含請求的資料體,servlet應該按照此行事。此操作可能是負面的操作。

三、用戶端與伺服器端的資料傳遞

相對於用戶端的post與get方法,其都有資料傳遞,都在request於response當中。我們通過ServiceInputStream與ServiceOutputStream類來實現資料的共用。

參考文獻:    http://bbs.csdn.net/topics/370118463    servlet伺服器端如何向用戶端返回資料

                   1、用戶端發出http請求,伺服器端接受請求,進行響應,然後讀取資料庫。

                   2、在用戶端:利用內建對象requst擷取資料,並傳至伺服器端。

                    put 與 getparameter是一對  ; setAtribute與getAttibute是一對。

                   伺服器端:我們除了用response返回資料外,也可以用request返回資料。將封裝好的對象放到requets.session中,直接用它們的setAtributer  (“key”,"value");然後,在用戶端:我們用request.getAttibute來擷取資料。  

int i=10;
把資料放到Servlet請求的屬性中
request.setAttribute("i",i);
轉寄到用戶端
request.getRequestdispatcher("url").forward(request,response);
在轉寄到的頁面用request.getAttribute(i)取出來..記得類型轉換

                        在伺服器端:利用內建對象response擷取資料,並傳至用戶端。與之對應方法中的response。

                   eg: response.getOutStream();  PrintWrite pw = response.getPrintWrite();

                           pw.out(“你想要傳到用戶端的資料”);      

                                                response這個對象就是用來響應的呀!
                                                response.getOutputSteam().write("寫資料");
                                                response.flushBuffer()這樣就可以發往資料到用戶端了呀!

當然,假如我們只想知道返回的標識,而不想要資料,那麼我們通過標識碼來判斷

伺服器端代碼:

if (userlist != null) {
            resp.setStatus(200);
        } else {
            resp.sendError(345, "使用者名稱密碼不正確!");

寫在了resp當中。

在用戶端代碼:

    httpClient.post("http://192.168.1.106:8080/login", param,                new BaseJsonHttpResponseHandler<Object>() {                    @Override                    public void onSuccess(int statusCode, Header[] headers,                            String rawJsonResponse, Object response) {                        Log.d(TAG, rawJsonResponse);                        Intent intent = new Intent(LoginActivity.this,                                BMapApiDemoMain.class);                        LoginActivity.this.startActivity(intent);                        finish();                    }

      根據我們傳遞過來的標識碼的值(statusCode),就知道到底是進入到onSuccess還是onFailure方法中。然後,在這些方法中選擇我們所要進行的操作。

      

四、mybatis當中resultMap等的作用:

參考文獻:http://zhuyuehua.iteye.com/blog/1721715   

 <mapper  namespace="zhuxuekui.data.UserlistMapper">                <resultMap id="userResultMap"  type="Userlist">            <result property="username" column="username"></result>            <result property="passward" column="passward"></result>        </resultMap>                        <select id="selectUserlist"  parameterType="Map"            resultMap="userResultMap">            select  * from userlist                 where username = #{username}  and passward = #{passward}        </select>          </mapper>

<mapper></mapper>映射標籤

<resultMap id =”與類名相關的id名” type =”類名”/>  id 與下面的select中的resultMap一一對應,type後面是一個類名。通過id找到此類。

下面全是 此類的一些屬性名稱用<result property=””  column=””/>   最好是一一對應

其中:property為你自己建立的類當中的屬性,column是你自己建立的資料庫當中的欄位名。

<select   id = “函數名”   xxxxxxxx    \>

paremeterType這個為selectUserlist函數的參數類型,假如只有一個參數,我們可以寫string或int等。但假如有多個參數,需要封裝在Map裡面。

<!-- 查詢, 1.輸入用map傳入多個參數 2.<where>語句, 智能添加where和and關鍵字 3.輸出直接映射對象 -->    <select id="findAll" resultMap="navigations" parameterType="map" >        select * from s_navigation        <where>            <if test="portletid != null">                portletid=#{portletid}            </if>            <if test="uuid != null">                and uuid=#{uuid}            </if>            <if test="uuid != null and portletid != null">                and isshow=1            </if>        </where>    </select>

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.