第十周作业

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

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

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head>

<body>
    <script type="text/javascript">
        function validate() {
            if (loginForm.uname.value == "") {
                alert("账号不能为空!");
                return;
            }
            if (loginForm.upwd.value == "") {
                alert("密码不能为空!");
                return;
            }
            loginForm.submit();
        }
    </script>


    <form name="loginForm" action="dologin.jsp" method="post">

        用户名:<input type="text" name="uname"><br> 
        密码: <input type="password" name="upwd"><br> 
             <input type="button"value="登录" onClick="validate()"> 
             <a href="zhuce.jsp">注册</a>

    </form>

</body>
</html>
<%@page import="com.sf.StuDao"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head>

<body>
<%
    StuDao sd=new StuDao();
    request.setCharacterEncoding("utf-8");
    String uname = request.getParameter("uname");
    String upwd = request.getParameter("upwd");
    if (sd.login(uname, upwd)){
        session.setAttribute("uname", uname);
        request.getRequestDispatcher("main.jsp").forward(request, response);
    }else{
        out.print("登陆失败,跳回登陆页.....");
        response.setHeader("refresh", "3;url=login.jsp");
    }


 %>



</body>
</html>
<%@page import="com.sf.StuDao"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head>

<body>
<%
    
    request.setCharacterEncoding("utf-8");
    
    String id =request.getParameter("stuid");
    Integer stuid =Integer.parseInt(id);
    
    String uname = request.getParameter("uname");
    
    String upwd = request.getParameter("upwd");
    
    String upwd1 = request.getParameter("upwd1");
    
    String age =request.getParameter("uage");
    Integer uage =Integer.parseInt(age);
    
        if((upwd.equals(upwd1))){
            StuDao sd=new StuDao();
            sd.reg(stuid, uname, upwd, uage);
            out.print("注册成功,跳回登录页.....");
            response.setHeader("refresh", "3;url=login.jsp");
    }
    else{
        out.print("两次密码不一致,跳回注册页.....");
            response.setHeader("refresh", "3;url=zhuce.jsp");
    }
        
    
    
 %>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head>

<body>
<script type="text/javascript">
        function validate(){
            if(loginForm.stuid.value==""){
                alert("id不能为空!");
                return;
            }
            if(loginForm.uname.value==""){
                alert("用户名不能为空!");
                return;
            }
            if(loginForm.upwd.value==""){
                alert("密码不能为空!");
                return;
            }
            if(loginForm.upwd1.value==""){
                alert("确认密码不能为空!");
                return;
            }
            if(loginForm.uage.value==""){
                alert("年龄不能为空!");
                return;
            }
            loginForm.submit();
        }
    </script>


    <form name="loginForm" action="dozhuce.jsp" method="post">
    学生id:<input type="number" name="stuid" ><br> 
    用户名:<input type="text" name="uname" ><br> 
    密码: <input  type="password" name="upwd"  ><br> 
    确认密码: <input  type="password" name="upwd1" ><br> 
    年龄:<input type="number" name="uage" ><br> 
    <a href="login.jsp">返回登录</a>
        <input type="button" value="注册" onClick="validate()">    
    </form>




</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>
登录成功
</body>
</html>
package com.sf;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class BaseDao {


    //获取连接
    protected Connection getConnection(){
        Connection conn=null;
            try {
                Class.forName("com.mysql.jdbc.Driver");
                // 2.建立连接
                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();
        }
    }
    
}
package com.sf;

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

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(Integer stuid,String uname, String upwd,Integer uage) {
        Connection conn = getConnection();
        PreparedStatement ps = null;
        try {
            String sql = "insert into stu(stuid,uname,upwd,uage) values(?,?,?,?)"; // 2个占位符
            // 4.执行SQL语句
            ps = conn.prepareStatement(sql);
            
            ps.setInt(1, stuid);
            ps.setString(2, uname);
            ps.setString(3, upwd);
            ps.setInt(4, uage);
            ps.executeUpdate();// 增删改都用这个
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            closeAll(conn, ps, null);
        }

    }
        
}

 

 

 

posted @ 2022-05-07 13:01  ♡十梦九你♡  阅读(17)  评论(0编辑  收藏  举报