JSP ——第九次作业
// 注册功能 public boolean register(Users user) { Connection con = null; PreparedStatement ps = null; try { con = getConnection(); String sql = "insert into users(username,password,email) values(?,?,?)"; ps = con.prepareStatement(sql); ps.setString(1, user.getUsername());// 给sql语句的问号赋值 ps.setString(2, user.getPassword()); ps.setString(3, "aaa@qq.com"); int row = ps.executeUpdate(); if (row > 0) { return true; } } catch (SQLException ex) { ex.printStackTrace(); } finally { closeAll(con, ps, null); } return false; }
<body> <form action="doregister.jsp" method="post"> 用户名:<input type="text" name="uname" /><Br> 密码 :<input type="password" name="upwd" /><br> <input type="submit" value="注册"> </form> </body>
<body> <% request.setCharacterEncoding("utf-8"); String uname = request.getParameter("uname"); String upwd = request.getParameter("upwd"); Users users = new Users(); users.setUsername(uname); users.setPassword(upwd); UsersDao ud = new UsersDao(); if (ud.register(users)) { request.getRequestDispatcher("index.jsp").forward(request, response); } else response.sendRedirect("register.jsp"); %> </body> doregister.jsp
<body> <form action="dologin.jsp" method="post"> 用户名:<input type="text" name="uname" value="小白" /><Br> 密码 :<input type="password" name="upwd" value="88888" /><br> 验证码:<input type="text" name="validationCode" onKeyDown="if(event.keyCode==13){form1.submit.focus();}" size="6"> <% int intmethod1 = (int) ((((Math.random()) * 5)) + 1); int intmethod2 = (int) ((((Math.random()) * 5)) + 1); int intmethod3 = (int) ((((Math.random()) * 5)) + 1); int intmethod4 = (int) ((((Math.random()) * 5)) + 1); String intsum = intmethod1 + "" + intmethod2 + intmethod3 + intmethod4; %> <input type="hidden" name="validationCode1" value="<%=intsum%>"> <img style="height:20px;weight:20px" src="images/<%=intmethod1%>.png"> <img style="height:20px;weight:20px" src="images/<%=intmethod2%>.png"> <img style="height:20px;weight:20px" src="images/<%=intmethod3%>.png"> <img style="height:20px;weight:20px" src="images/<%=intmethod4%>.png"> <br> <input type="submit" value="登录"> </form> </body> index.jsp
<% request.setCharacterEncoding("utf-8"); String uname = request.getParameter("uname"); String upwd = request.getParameter("upwd"); UsersDao ud = new UsersDao(); if (ud.login(uname, upwd) && request.getParameter("validationCode1").equals(request.getParameter("validationCode"))) { //登录成功,创建User对象,并放入session Users u = new Users(); u.setUsername(uname); u.setPassword(upwd); session.setAttribute("user", u); request.getRequestDispatcher("main.jsp").forward(request, response); } else response.sendRedirect("index.jsp"); %> dologin.jsp