1 packagecom.martin;2
3 importjava.io.IOException;4 importjava.io.PrintWriter;5 importjava.sql.Connection;6 importjava.sql.PreparedStatement;7 importjava.sql.ResultSet;8 importjava.sql.SQLException;9 importjava.sql.Statement;10 importjava.util.ArrayList;11 importjava.util.List;12
13 importjavax.servlet.ServletException;14 importjavax.servlet.annotation.WebServlet;15 importjavax.servlet.http.HttpServlet;16 importjavax.servlet.http.HttpServletRequest;17 importjavax.servlet.http.HttpServletResponse;18
19 importcom.utils.GetConnection;20
21 @WebServlet("/RegistServlet")22 public class RegistServlet extendsHttpServlet {23
24 protected voiddoPost(HttpServletRequest request, HttpServletResponse response)25 throwsServletException, IOException {26
27 //设置请求编码、响应方式和编码方式
28 request.setCharacterEncoding("UTF-8");29 response.setCharacterEncoding("UTF-8");30 response.setContentType("text/html");31
32 PrintWriter out =response.getWriter();33
34 Statement st = null;35 ResultSet rs = null;36
37 PreparedStatement ptst = null;38 //创建用户名集合
39 List usernameList = new ArrayList();40
41 //获取注册用户名
42 String registName = request.getParameter("username");43 //获取注册用户密码
44 String registPassword = request.getParameter("password");45 //获取注册用户二次密码
46 String registRepeatpsd = request.getParameter("repeatPsd");47 //获取注册用户真实姓名
48 String userTrueName = request.getParameter("truename");49 //获取用户性别
50 String gender = request.getParameter("gender");51 //获取注册用户手机号码
52 String phoneNumber = request.getParameter("phone");53 //获取用户身份证号码
54 String identityCode = request.getParameter("indetity");55
56 //获取与MySQL链接的Connection对象
57 Connection conn = null;58 try{59 conn = newGetConnection().getConnection();60 /**
61 * 判断两次密码是否一致:62 * 是:继续注册;63 * 否:返回错误;64 */
65 if(registPassword.equals(registRepeatpsd)) {66 try{67 /**
68 * 判断用户表中是否已经存在该用户69 * 1.遍历tb_user表中所用的username字段70 * 2.将username字段中的所有数据存入集合中;71 * 3.判断集合中和否含有注册的用户名72 * 3.1:如果有,返回到error页面73 * 3.2:如果没有,进行注册操作74 */
75
76 //遍历tb_user表中username字段
77 String select = "select username from tb_user";78 st =conn.createStatement();79 rs =st.executeQuery(select);80 //将username字段的所有数据存入集合中
81
82 while(rs.next()) {83 usernameList.add(rs.getString(1));84 }85 //关闭ResultSet和Statement链接
86 rs.close();87 st.close();88
89 } catch(SQLException e) {90 //TODO Auto-generated catch block
91 e.printStackTrace();92 }93
94 if(usernameList.contains(registName)) {95 out.println("用户名已注册,请重新尝试。");96 } else{97 String insert = "insert into tb_user(username,password,name,gender,phonenumber,identitycode) values(?,?,?,?,?,?)";98 try{99 ptst =conn.prepareStatement(insert);100 //设置ptst参数
101 ptst.setString(1, registName);102 ptst.setString(2,registPassword);103 ptst.setString(3, userTrueName);104 ptst.setString(4, gender);105 ptst.setString(5,phoneNumber);106 ptst.setString(6, identityCode);107 //执行sql语句
108 ptst.execute();109 out.println("欢迎注册。");110 //关闭关闭ResultSet和Statement链接
111 ptst.close();112
113 } catch(SQLException e) {114 //TODO Auto-generated catch block
115 e.printStackTrace();116 }117
118 }119 } else{120 out.println("两次密码输入不一致,请重新尝试。");121 }122 } catch(ClassNotFoundException e1) {123 //TODO Auto-generated catch block
124 e1.printStackTrace();125 } finally{126 try{127 //关闭Connection链接
128 if (conn != null) {129 conn.close();130 }131 } catch(SQLException e) {132 //TODO Auto-generated catch block
133 e.printStackTrace();134 }135 }136
137 out.flush();138 out.close();139
140 }141
142 }