RegisterServlet代码:
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
//获取注册信息
UserDao userdao=new UserDao();
String userID=request.getParameter("phone");
String username = request.getParameter("uname");
String password = request.getParameter("pwd");
String email = request.getParameter("mail");
String sex = request.getParameter("sex");
String birth=request.getParameter("birth");
String address=request.getParameter("address");
//可以传值
/*out.println(userID);
out.println(username);
out.println(password);
out.println(email);
out.println(sex);
out.println(address);
out.println(birth);*/
//将注册信息存入数据库,再返回登录
userdao.Register(userID,username,password,birth,email,sex,address);
response.sendRedirect("Login.jsp");
}
UserDao的register代码段
public void Register(String userID, String userPsw, String userName, String email,String birth,String sex,String address) {
// TODO Auto-generated method stub
//连接数据库
Connection conn = DBConnection.getConnection();
//sql语句
//String sql = "insert into user(flag,userID,userPsw,userName,birth,E-mail,sex,address) values(?,?,?,?,?,?,?,?)";
String sql="insert into user(userID,userName,userPsw,bitrh,E-mail,sex,address,flag) values(?,?,?,?,?,?,?,?)";
int rs = 0;
PreparedStatement stm = null;
try {
//预编译SQL,减少sql执行
stm = conn.prepareStatement(sql);
//传参
stm.setInt(8, 1);//状态->用户注册所以全为1
stm.setString(1, userID);//手机号
stm.setString(3, userPsw);//密码
stm.setString(2, userName);//用户名
stm.setString(5, email);//电子邮箱
stm.setString(4, birth);//出生日期
stm.setString(6, sex);//性别
stm.setString(7, address);//收货地址
//执行更新
rs = stm.executeUpdate();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
stm.close();
conn.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
- 点击注册后user表没有新记录,但会跳转到Login.jsp
- register页面传过来的值也能接收到
- 数据库已经连接好了,因为登录页面是没问题的,可以找到用户。
- 点了注册提交之后,数据库没有新记录产生。