JSP第八次作业

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

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

 

 

package stu;


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

public class BD {
    // 连接数据库
    public Connection getConnection() {
        Connection con = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            // 建立连接
            con = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/test", "root", "root");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return con;
    }

    // 关闭数据库
    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 (Exception e) {
            e.printStackTrace();
        }
    }

}
package stu;

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


public class SD extends BD {

    public boolean DengLu(String stuname, String stupwd) {
        boolean f = false;
        Connection con = getConnection();
        String sql = "select * from book where stuname=? and stupwd=?";
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = con.prepareStatement(sql);
            ps.setString(1, stuname);
            ps.setString(2, stupwd);
            rs = ps.executeQuery();//查询
            while (rs.next()) {
                f = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            closeAll(con, ps, rs);
        }
        return f;
    }
    
    public void ZhuCe(int stuid,String stuname,int stupwd,int stuage) {
        Connection con=getConnection();
        PreparedStatement ps=null;
        try {
            String sql="insert into book(stuid,stuname,stupwd,stuage) values(?,?,?,?)";
            ps=con.prepareStatement(sql);
            ps.setInt(1, stuid);
            ps.setString(2, stuname);
            ps.setInt(3, stupwd);
            ps.setInt(4, stuage);
            ps.executeUpdate();//增删改
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            closeAll(con, ps, null);
        }
    }
    

}
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title></title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  
  <body>
     <form action="index.jsp" method="post" name="one">
        请输入用户名:<input name="name" type="text"><br>
        请输入密码:&nbsp;&nbsp;&nbsp;&nbsp;<input name="password" type="password"><br>
        <input type="button" value="登录" onclick="denglu()"> 
        <a href="index2.jsp">注册新用户</a>
        <script type="text/javascript">
            function denglu() {
                if (one.name.value == "") {
                    alert("用户名不能为空!");
                    return;
                }
                if (one.password.value == "") {
                    alert("密码不能为空!");
                    return;
                }
                one.submit();
            }
        </script>
    </form>
  </body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="stu.SD"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title></title>
    
  </head>
  
  <body>
    <%
        request.setCharacterEncoding("utf-8");
        String uname = request.getParameter("name");
        String upassword = request.getParameter("password");
        SD stu = new SD();
        if (stu.DengLu(uname, upassword)) {
            session.setAttribute("name", uname);
            request.getRequestDispatcher("index4.jsp").forward(request,response);
        } else {
            out.print("登录失败,5秒后返回登录页!");
            response.setHeader("refresh", "5;url=inde1.jsp");
        }
    %>
  </body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title></title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  
  <body>
    <form action="index3.jsp" method="post" name="two">
        请输入id:&nbsp;&nbsp;&nbsp;&nbsp;<input type="number" name="id"><br>
        请输入用户名:<input name="name" type="text"><br>
        请输入密码:&nbsp;&nbsp;&nbsp;&nbsp;<input name="password1" type="password"><br>
        再次输入密码:<input name="password2" type="password"><br>
        请输入年龄: <input name="age" type="number"><br> <input
            type="button" value="注册" onclick="zhuce()"> 
            <a href="inde1.jsp">返回登录</a>
        <script type="text/javascript">
        function zhuce() {
                if (two.id.value == "") {
                    alert("id不能为空!");
                    return;
                }
                if (two.name.value == "") {
                    alert("用户名不能为空!");
                    return;
                }
                if (two.password1.value == "") {
                    alert("密码不能为空!");
                    return;
                }
                if (two.password2.value == "") {
                    alert("再次输入密码不能为空!");
                    return;
                }
                if (two.age.value == "") {
                    alert("年龄不能为空!");
                    return;
                }
                if (two.password1.value != two.password2.value) {
                    alert("密码不一致,请重新输入!");
                    return;
                }
                two.submit();
            }
        </script>
    </form>
  </body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="stu.SD"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title></title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  
  <body>
        <%
        request.setCharacterEncoding("utf-8");
        String id = request.getParameter("id");
        int uid = Integer.parseInt(id);

        String uname = request.getParameter("name");
        String upassword1 = request.getParameter("password1");
        String upassword2 = request.getParameter("password2");
        int pass = Integer.parseInt(upassword1);

        String age = request.getParameter("age");
        int uage = Integer.parseInt(age);
        if (upassword1.equals(upassword2)) {
            SD stu = new SD();
            stu.ZhuCe(uid, uname, pass, uage);
            out.print("注册成功,5秒后返回登录页!");
            response.setHeader("refresh", "5;url=inde1.jsp");
        } else {
            out.print("两次密码不一致,5秒后返回注册页!");
            response.setHeader("refresh", "5;url=index2.jsp");
        }
    %>
  </body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title></title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  
  <body>
     <%
        request.setCharacterEncoding("utf-8");
        String name = request.getParameter("name");
    %>
    <h4>登录成功!</h4>
    欢迎登录:<%=name%>
  </body>
</html>

 

 

 

 

posted @ 2022-05-08 13:10  计算机1901万家乐  阅读(25)  评论(0编辑  收藏  举报