jsp第八次作业

数据库test 中建个表 stu(stuid 主键 自动增长 ,用户名,密码,年龄)

1.设计一个注册页面,实现用户注册功能
2.设计一个登陆页面,实现用户名密码登陆
3.两个页面可以互相超链接

package wjd;

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");
                // 2.建立连接
                conn = DriverManager.getConnection(
                        "jdbc:mysql://localhost:3306/mysql", "root", "001010Chen");
            } 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();
        }
    }
}

package wjd;



import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


public class Stu extends BaseDao {
    //用户登录验证
    public boolean login(String uname, String pwd) {
        boolean f = false;
        Connection conn = getConnection();
        String sql = "select * from stu where uname=? and pwd=?";
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, uname);
            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 pwd, int age) {
        Connection conn = getConnection();
        PreparedStatement ps = null;
        try {
            String sql = "insert into stu(uname,pwd,age) values(?,?,?)"; // 3个占位符
            // 4.执行SQL语句
            ps = conn.prepareStatement(sql);
            ps.setString(1, uname);
            ps.setString(2, pwd);
            ps.setInt(3, age);
            ps.executeUpdate();// 增删改都用这个
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            closeAll(conn, ps, null);
        }

    }

}
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
<head>
<title>注册页面</title>
</head>
<body>
    <form name="regForm" action="index1.jsp" method="post">
        用户:<input type="text" name="uname"><br> 
        密码:<input type="password" name="pwd" id="psw"><br> 
        确认密码:<input type="password" name="pwd1" id="psw1 "><br> 
        年龄:<input type="text" name="age"><br> 
        <input type="submit" value="注册"> 
        <a href="index1.jsp">已有账号?前往登录</a>
    </form>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
<head>
<title>登录页面</title>
<script type="text/javascript">
    function check() {
        if (loginForm.uname.value == "") {
            alert("请输入账号!");
            return;
        } else if (loginForm.pwd.value == "") {
            alert("请输入密码!");
            return;
        }
        loginForm.submit();
    }
</script>
</head>
<body>
    <form name="loginForm" action="index2.jsp" method="post">
        用户:<input type="text" name="uname"><br> 
        密码:<input type="password" name="pwd"><br> 
        <input type="button" value="登录" onClick="check()"> 
        <a href="e.jsp">立即注册账户</a>
    </form>
</body>
</html>
<%@page import="wjd.Stu"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>获取</title>
</head>
<body>
<%
    String uname=request.getParameter("uname");
    String pwd=request.getParameter("pwd");
    String pwd1=request.getParameter("pwd1");
    String ages=request.getParameter("age");

    if (pwd.equals(pwd1)) {
        int age=Integer.parseInt(ages);
        Stu sd = new Stu();
        sd.reg(uname, pwd, age);
%>
<script type="text/javascript">alert("注册成功!");</script>
<%
        request.getRequestDispatcher("index3.jsp").forward(request,response);
    } else {
%>
<script type="text/javascript">alert("注册失败,请重试!");</script>
<%
        request.getRequestDispatcher("index.jsp").forward(request,response);
    }
</body>
</html>


<%@page import="wjd.Stu"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>

<html>
<head>
<title>判断</title>
</head>
<body>
    <%
        String uname = request.getParameter("uname");
            String pwd = request.getParameter("pwd");

            wjd.Stu sd = new wjd.Stu();
            if (sd.login(uname, pwd)) {
    %>
    <h1>登录成功</h1>
    <%
        } else {
    %>
    <h1>登录失败,3秒后返回登录界面</h1>
    <%
        response.setHeader("refresh", "3;url=login.jsp");
        }
    %>
</body>
</html>

 

posted @ 2022-05-08 20:37  赵云鹏  阅读(14)  评论(0编辑  收藏  举报