利用简易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
package com;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
<!--more-->
public class JavaRegisterMysql extends HttpServlet{
private static final long serialVersionUID = 1L;
private static final int NAME_CODE_RIGHT = 0; //
private static final int CODE_WRONG = 1; //
private static final int NAME_WRONG = 2; //
String notename = null;
String notepassword = null;
String notephone = null;
public JavaRegisterMysql(){
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
if(req == null){
return;
}
resp.setContentType("text/html;charset=utf-8");
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
PrintWriter out = resp.getWriter();
String name = req.getParameter("NAME");
String code = req.getParameter("CODE");
String phone = req.getParameter("PHONE");
String chose=req.getParameter("CHOSE");
//手机客户端访问
int ret=-1;
try {
ret = checkSubmit(name, code, phone, chose);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
out.print(ret);
out.flush();
out.close();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
if(req == null){
return;
}
resp.setContentType("text/html;charset=utf-8");
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
PrintWriter out = resp.getWriter();
String name = req.getParameter("NAME");
String code = req.getParameter("CODE");
String phone = req.getParameter("PHONE");
String chose=req.getParameter("CHOSE");
int ret=-2;
try {
ret = checkSubmit(name, code, phone,chose);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
out.print(ret);
out.flush();
out.close();
}
/**
* 判断登录名和密码
* @param name
* @param code
* @return
* @throws Exception
*/
private int checkSubmit(String name, String code, String phone,String chose) throws Exception{
int ret = -1;
//UserDAOProxy ab=new UserDAOProxy();
User user=new User(name,code,phone);
ret=new UserDAOProxy().findLogin(user, chose);
return ret;
}
}

主接收类

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
package com;
import java.sql.*;
public class DatabaseConnection {
public static final String DRIVER = "com.mysql.jdbc.Driver";
public static final String URL = "jdbc:mysql://localhost:3306/scut";
public static final String USER = "root";
public static final String PASS = "123456";
private Connection con;
public DatabaseConnection(){
try{
Class.forName(DRIVER);
con = DriverManager.getConnection(URL,USER,PASS);
}
catch(Exception e){}
}
public Connection getConnection(){
return con;
}
public void close(){
try{
if(con!=null){
con.close();
}
}
catch(Exception e){}
}
}

JDBC接口类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com;
public class UserDAOProxy {
private DatabaseConnection dbc;
private UserDAOImpl idao;
public UserDAOProxy(){
dbc = new DatabaseConnection();
idao = new UserDAOImpl(dbc.getConnection());
}
public int findLogin(User user,String chose)throws Exception{
int flag = idao.findLogin(user,chose);
dbc.close();
return flag;
}
}
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
package com;
import java.sql.*;
public class UserDAOImpl {
private Connection con;
private Statement stat;
public UserDAOImpl(Connection con){
this.con = con;
}
public int findLogin(User user,String chose)throws Exception{
int flag = 0;
//String sql = "SELECT * FROM noteinfo WHERE name="+"'"+user.getName()+"'";
//String sql = "SELECT * FROM noteinfo WHERE id=1";
//String cd=user.getName();
//String sql = "SELECT * FROM noteinfo WHERE name="+ "'"+user.getName()+"'" +
// " and password="+ "'"+user.getPassword()+"'";
try {
this.stat = con.createStatement();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 结果集
if(chose.equals("1"))
{
flag=1;
String sql = "SELECT * FROM noteinfo WHERE name="+ "'"+user.getName()+"'" +
" and password="+ "'"+user.getPassword()+"'";
//String sql2="insert into noteinfo values(null,'ggg','111','13412323234')";
ResultSet rs = stat.executeQuery(sql);
if(rs.next()){
user.setName(rs.getString("name"));
user.setPassword(rs.getString("password"));
flag = 2;
}
//stat.execute(sql2);
}
if(chose.equals("2"))
{
flag=3;
String sql="insert into noteinfo values(null,"+ "'"+user.getName()+ "'"+","+ "'"+user.getPassword()+ "'"+","
+ "'"+user.getPhone()+ "')";
stat.execute(sql);
flag = 4;
}
return flag;
}
}
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
package com;
public class User {
public User(){
this.name=" ";
this.password=" ";
this.phone=" ";
}
public User(String name,String code,String phone){
this.name=name;
this.password=code;
this.phone=phone;
}
private String phone;
private String name;
private String password;
public String getPhone(){
return phone;
}
public void setPhone(String phone){
this.phone = phone;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public void setPassword(String password){
this.password = password;
}
public String getPassword(){
return password;
}
}

以上为相应调用类

1
2
3
4
5
6
7
8
<servlet>
<servlet-name>gjwServlet</servlet-name>
<servlet-class>com.JavaRegisterMysql</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>gjwServlet</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>

web.xml需添加以上部分代码

Comments