Android client to server-side verification login method implementation 2

Source: Internet
Author: User
Tags http post

First, in the previous article, I just mentioned one of the ways to achieve landing

Everyone can see:

http://www.apkbus.com/android-45004-1-1.html Android get Web server side session and verify login

http://blog.csdn.net/cainiao123hack/article/details/8255848 server-to-Android client value-Login implementation

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

How to write a Java server-side servlet, and then can when the client makes an HTTP request, can accept the request, read the database

Second, some code to explain:

What is a servlet: a small, platform-agnostic Java program that does not have a graphical user interface that can extend the functionality of Web services in many ways.

HttpServlet class:

Reference: http://blog.csdn.net/jay198746/article/details/4489571

http://www.cnblogs.com/amboyna/archive/2007/09/18/897101.html Java Learning Servlet-doget () and Dopost ()

Provides a framework for handling HTTP protocols.
The service method in this class supports standard HTTP methods such as GET, POST, and so on. This support
The process is achieved by assigning them to appropriate methods (such as Doget, DoPost).

Doget method: Called by the service method of this class to handle an HTTP GET operation. This operation allows the client to simply obtain resources from an HTTP server. Overloading of this method will automatically support the head method. Secure approach.

Dopost method: Called by the Service method to handle an HTTP POST operation. This operation contains the requested data body, which the servlet should follow. This operation may be a negative operation.

Third, client and server-side data transfer

Both the post and get methods of the client are data-passing, in the request in response. We use the Serviceinputstream and Serviceoutputstream classes to achieve data sharing.

Reference: http://bbs.csdn.net/topics/370118463 servlet server-side how to return data to clients

1. The client makes an HTTP request, the server accepts the request, responds, and then reads the database.

2, in the client: the use of built-in object requst to obtain data, and transmitted to the server side.

Put and GetParameter are a pair; Setatribute and Getattibute are a pair.

Server side: We can also return data with request, in addition to returning data with response. Put the encapsulated objects in the requets.session, directly with their setatributer ("Key", "value"), and then, on the client side: We use Request.getattibute to get the data.

int i=10;
Put the data into the properties of the servlet request
Request.setattribute ("I", I);
Forward to Client
Request.getrequestdispatcher ("url"). Forward (Request,response);
On the forwarded page, use Request.getattribute (i) to remove it. Remember type conversion

On the server side: Use the built-in object response to get the data and pass it to the client. Response in the corresponding method.

Eg:response.getOutStream (); Printwrite pw = Response.getprintwrite ();

Pw.out ("You want to upload data to the client");

                                                  response This object is to respond!
                                                  Response.getoutputsteam (). Write ("Write data") ;
                                                  Response.flushbuffer () so you can send data to the client!

Of course, if we just want to know the identity of the return and don't want the data, then we'll use the identifier to determine

Server-side code:

if (userlist! = null) {
Resp.setstatus (200);
} else {
Resp.senderror (345, "username password is incorrect!) ");

Written in the midst of resp.

In the client code:

 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 (); }

Based on the value of the identifier we passed in (StatusCode), we know whether to enter the Onsuccess or OnFailure method. Then, in these methods, select the action that we want to take.

Iv. the role of Resultmap in MyBatis:

Reference: 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> Map Labels

<resultmap id = "ID name associated with class name" type = "class name"/> ID corresponds to resultmap one by one in the following select, and type is followed by a class name. This class is found by ID.

Here are all the property names for this class with <result property= "" column= ""/> preferably one by one corresponding

Where: property is the attribute of the class you set up, and column is the name of the field in the database you created yourself.

<select id = "function name" xxxxxxxx \>

Paremetertype This is the parameter type for the Selectuserlist function, and if there is only one argument, we can write a string or an int. But if there are multiple parameters, it needs to be encapsulated in the map.

<!--query, 1. Enter multiple parameters with map 2.<where  > statement, Smart Add where and and keyword 3. Output direct Mapping Object--<select id= "findAll"  resultmap= "navigations"  Parametertype= "map"  > select * from s_navigation <where> <if  test= "Portletid! = null"  > Portletid=#{portletid} </if  > <if  test=< span class= "str" > "UUID! = null"  > Uuid=#{uuid} </if             ;  <if  test= "UUID! = null and Portletid! = null"  > and Isshow=1 </if  > </where  > </select& gt; 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.