第十周作业
数据库test 中建个表 stu(stuid 主键 自动增长 ,用户名,密码,年龄)
1.设计一个注册页面,实现用户注册功能
2.设计一个登陆页面,实现用户名密码登陆
3.两个页面可以互相超链接
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'login.jsp' starting page</title> </head> <body> <script type="text/javascript"> function validate(){ if(loginForm.stuname.value==""){ alert("账号不能为空!"); return; } if(loginForm.stupwd.value==""){ alert("密码不能为空!"); return; } loginForm.submit(); } </script> <form name="loginForm" action="dologin.jsp" method="post"> 用户名:<input type="text" name="stuname" value=""><br> 密码: <input type="password" name="stupwd" value=""> <input type="button" value="登录" onClick="validate()"> <a href="register.jsp">注册</a> </form> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'dologin.jsp' starting page</title> </head> <body> <% StuDao sd=new StuDao(); request.setCharacterEncoding("utf-8"); String stuname = request.getParameter("stuname"); String stupwd = request.getParameter("stupwd"); if (sd.login(stuname, stupwd)){ session.setAttribute("stuname", stuname); request.getRequestDispatcher("main.jsp").forward(request, response); }else{ out.print("登陆失败,即将跳回登陆页....."); response.setHeader("refresh", "2;url=login.jsp"); } %> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'register.jsp' starting page</title> </head> <body> <script type="text/javascript"> function validate(){ if(loginForm.stuname.value==""){ alert("账号不能为空!"); return; } if(loginForm.stupwd.value==""){ alert("密码不能为空!"); return; } if(loginForm.stupwd1.value==""){ alert("确认密码不能为空!"); return; } if(loginForm.stuage.value==""){ alert("年龄不能为空!"); return; } loginForm.submit(); } </script> <form name="loginForm" action="jump.jsp" method="post"> 用户名:<input type="text" name="stuname" value=""><br> 密码: <input type="password" name="stupwd" value=""><br> 确认密码: <input type="password" name="stupwd1" value=""><br> 年龄:<input type="text" name="stuage" value=""><br> <a href="login.jsp">返回登录</a> <input type="button" value="注册" onClick="validate()"> </form> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'jump.jsp' starting page</title> </head> <body> <% StuDao sd=new StuDao(); request.setCharacterEncoding("utf-8"); String stuname = request.getParameter("stuname"); String stupwd = request.getParameter("stupwd"); String stuages = request.getParameter("stuage"); int stuage = Integer.parseInt(stuages); if(sd.reg(stuname, stupwd, stuage)){ request.getRequestDispatcher("login.jsp").forward(request, response); }else{ out.print("注册失败"); } %> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'main.jsp' starting page</title>
</head>
<body>
登陆成功! <br>
</body>
</html>
import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class StuDao extends BaseDao { public boolean login(String name, String pwd) { boolean f = false; Connection conn = getConnection(); String sql = "select * from stu where uname=? and upwd=?"; PreparedStatement ps = null; ResultSet rs = null; try { ps = conn.prepareStatement(sql); ps.setString(1, name); ps.setString(2, pwd); rs = ps.executeQuery(); if (rs.next()) f = true; } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { closeAll(conn, ps, rs); } return f; } public void reg(String uname, String upwd,int uage) { Connection conn = getConnection(); PreparedStatement ps = null; try { String sql = "insert into stu(uname,upwd,uage) values(?,?,?)"; ps = conn.prepareStatement(sql); ps.setString(1, uname); ps.setString(2, upwd); ps.setInt(3, uage); ps.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { closeAll(conn, ps, null); } } }
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class BaseDao { protected Connection getConnection(){ Connection conn=null; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/test", "root", "123456"); } catch (Exception e) { e.printStackTrace(); } return conn; } protected void closeAll(Connection con,PreparedStatement ps,ResultSet rs){ try { if(rs != null) rs.close(); if(ps != null) ps.close(); if(con != null) con.close(); } catch (SQLException e) { e.printStackTrace(); } } }