利用简易Tomcat服务器结合MysqL实现Android手机注册与登录(客户端部分)

一、概述

1、服务器在本地搭建,利用Tomcat运行servlet容器
2、继承于httpServlet类的自定义实现与客户端的交互,结合MysqL,jdbc知识
3、客户端实现简单的注册和登录验证

注意点:
1、web.xml里入口定义要正确,否则登录客户端连接不到
2、当在Tomcat中运行时,需要在WEB_INF文件夹中手动添加jdbc库文件,偶尔会在运行过后消失,要注意。(或者直接脱离Eclipse就不会有这个问题)
3、sql语句要多测试,否则容易写错但执行不会报错。
4、注意事先需启动MysqL。

二、代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package com.example.java4androidmysql;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
public static final String URL = "http://192.168.2.1:8080/JavaRegisterMysql/login";
public static String res="";
public static String name = "";
public static String code = "";
public static String phone =" ";
public static String chose="0";
Button connectButton=null;
Button registerButton=null;
EditText noteUser=null;
EditText notePassword=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
connectButton=(Button)findViewById(R.id.btnConnect);
registerButton=(Button)findViewById(R.id.btnRegister);
noteUser=(EditText)findViewById(R.id.editUserName);
notePassword=(EditText)findViewById(R.id.editPassword);
//////注意类Class DriverManager Statement ResultSet
connectButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(MainActivity.this, "正在登录", Toast.LENGTH_SHORT).show();
name = noteUser.getText().toString().trim();
code = notePassword.getText().toString().trim();
chose="1";
new SubmitAsyncTask().execute(URL);
Toast.makeText(MainActivity.this, "res = "+res, Toast.LENGTH_SHORT).show();
if(res.equals("2")){
Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
//Intent intent=new Intent();
//intent.setClass(MainActivity.this, WriteNote.class);
//MainActivity.this.startActivity(intent);
}else if(res.equals("1")){
Toast.makeText(MainActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
}else if(res.equals("3")){
Toast.makeText(MainActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
}else if(res.equals("4")){
Toast.makeText(MainActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
}else if(res.equals("-1")){
Toast.makeText(MainActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
}
}
});
noteUser.setText("ab");
notePassword.setText("123456");
registerButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent=new Intent();
intent.setClass(MainActivity.this, Register.class);
MainActivity.this.startActivity(intent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}

以上为手机主界面函数调用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package com.example.java4androidmysql;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Register extends Activity{
public static final String URL = "http://192.168.2.1:8080/JavaRegisterMysql/login";
Button backButton=null;
Button registerButton=null;
EditText user=null;
EditText password=null;
EditText phone=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.register);
backButton=(Button)findViewById(R.id.btnBack);
registerButton=(Button)findViewById(R.id.btnRegister);
user=(EditText)findViewById(R.id.editUserName);
password=(EditText)findViewById(R.id.editPassword);
phone=(EditText)findViewById(R.id.editPhone);
//////注意类Class DriverManager Statement ResultSet
registerButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
MainActivity.name = user.getText().toString().trim();
MainActivity.code = password.getText().toString().trim();
MainActivity.phone = phone.getText().toString().trim();
MainActivity.chose="2";
new SubmitAsyncTask().execute(URL);
Toast.makeText(Register.this, "res = "+MainActivity.res, Toast.LENGTH_SHORT).show();
if(MainActivity.res.equals("2")){
Toast.makeText(Register.this, "登录成功", Toast.LENGTH_SHORT).show();
//Intent intent=new Intent();
//intent.setClass(MainActivity.this, WriteNote.class);
//MainActivity.this.startActivity(intent);
}else if(MainActivity.res.equals("1")){
Toast.makeText(Register.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
}else if(MainActivity.res.equals("3")){
Toast.makeText(Register.this, "注册失败", Toast.LENGTH_SHORT).show();
}else if(MainActivity.res.equals("4")){
Toast.makeText(Register.this, "注册成功", Toast.LENGTH_SHORT).show();
}else if(MainActivity.res.equals("-1")){
Toast.makeText(Register.this, "网络异常", Toast.LENGTH_SHORT).show();
}
}
});
user.setText("gjw");
password.setText("123");
phone.setText("13570236302");
}
}

以上为注册界面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package com.example.java4androidmysql;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import android.os.AsyncTask;
public class SubmitAsyncTask extends AsyncTask<String, Integer, String>{
String info = "";
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
String url = params[0];
String reps = "";
reps = doPost(url);
return reps;
}
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
MainActivity.res = result.trim();
super.onPostExecute(result);
}
/**
* 用Post方式跟服务器传递数据
* @param url
* @return
*/
private String doPost(String url){
String responseStr = "";
try {
//发送post类型请求
HttpPost httpRequest = new HttpPost(url);
HttpParams params = new BasicHttpParams();
ConnManagerParams.setTimeout(params, 1000); //从连接池中获取连接的超时时间
HttpConnectionParams.setConnectionTimeout(params, 3000);//通过网络与服务器建立连接的超时时间
HttpConnectionParams.setSoTimeout(params, 5000);//读响应数据的超时时间
httpRequest.setParams(params);
//下面开始跟服务器传递数据,使用BasicNameValuePair
List<BasicNameValuePair> paramsList = new ArrayList<BasicNameValuePair>();
paramsList.add(new BasicNameValuePair("NAME", MainActivity.name));
paramsList.add(new BasicNameValuePair("CODE", MainActivity.code));
paramsList.add(new BasicNameValuePair("PHONE", MainActivity.phone));
paramsList.add(new BasicNameValuePair("CHOSE", MainActivity.chose));
UrlEncodedFormEntity mUrlEncodeFormEntity = new UrlEncodedFormEntity(paramsList, HTTP.UTF_8);
httpRequest.setEntity(mUrlEncodeFormEntity);
////////////////////////////////////////////////
HttpClient httpClient = new DefaultHttpClient();
HttpResponse httpResponse = httpClient.execute(httpRequest);
final int ret = httpResponse.getStatusLine().getStatusCode();
if(ret == HttpStatus.SC_OK){
responseStr = EntityUtils.toString(httpResponse.getEntity(), HTTP.UTF_8);
}else{
responseStr = "-1";
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return responseStr;
}
}

以上为接口类,但写得可能有些不对(如返回变量那里是上次调用的),略忙,等再改一下。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/ab" >
<TextView
android:id="@+id/FRppt01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/Title"
android:textSize="15pt"
android:textColor="#FFFFFF"
android:textStyle="bold"
android:layout_marginTop="40dp"
android:gravity="center_horizontal"
android:padding="3dip"/>
<EditText
android:id="@+id/editUserName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="输入用户名"/>
<EditText
android:id="@+id/editPassword"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="输入密码"/>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow>
<Button
android:id="@+id/btnConnect"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="登录"
android:layout_marginTop="260dp"
android:layout_weight="1" />
<Button
android:id="@+id/btnRegister"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="注册"
android:layout_marginTop="260dp"
android:layout_weight="1"/>"
</TableRow>
</TableLayout>
</LinearLayout>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/ab" >
<TextView
android:id="@+id/FRppt01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/RegisterPage"
android:textSize="15pt"
android:textColor="#FFFFFF"
android:textStyle="bold"
android:layout_marginTop="40dp"
android:gravity="center_horizontal"
android:padding="3dip"/>
<TableLayout
android:layout_below="@id/FRppt01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="1"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dip">
<TextView
android:id="@+id/textUserName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12pt"
android:layout_weight="1"
android:layout_below="@id/FRppt01"
android:text="@string/userName"/>"
<EditText
android:id="@+id/editUserName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dip">
<TextView
android:id="@+id/textPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="12pt"
android:text="@string/passWord"/>
<EditText
android:id="@+id/editPassword"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dip">
<TextView
android:id="@+id/textPhone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="12pt"
android:text="@string/phone"/>
<EditText
android:id="@+id/editPhone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
</TableRow>
</TableLayout>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TableRow>
<Button
android:id="@+id/btnBack"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="260dp"
android:layout_weight="1"
android:text="@string/Back" />
<Button
android:id="@+id/btnRegister"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="260dp"
android:layout_weight="1"
android:text="@string/Register" />
</TableRow>
</TableLayout>
</RelativeLayout>

以上两段代码为布局文件代码。

Comments