jsp第十周作业

------------恢复内容开始------------

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

1.设计一个注册页面,实现用户注册功能

2.设计一个登陆页面,实现用户名密码登陆

3.两个页面可以互相超链接

 
package me;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class A {

    protected Connection getConnection(){
        Connection conn=null;
            try {
                Class.forName("com.mysql.jdbc.Driver");
             
                conn = DriverManager.getConnection(
                        "jdbc:mysql://localhost:3306/mysql", "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 me;


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


public class B extends A {
   
    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(?,?,?)"; 
            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>index</title>
</head>
<body>
    <form name="regForm" action="G.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="G.jsp">已有账号?前往登录</a>
    </form>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
<head>
<title>G</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="H.jsp" method="post">
        用户:<input type="text" name="uname"><br> 
        密码:<input type="password" name="pwd"><br> 
        <input type="button" value="登录" onClick="check()"> 
        <a href="index.jsp">立即注册账户</a>
    </form>
</body>
</html>
<%@page import="me.B"%>
<%@ 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>H</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);
        B sd = new B();
        sd.reg(uname, pwd, age);
%>
<script type="text/javascript">alert("注册成功!");</script>
<%
        request.getRequestDispatcher("J.jsp").forward(request,response);
    } else {
%>
<script type="text/javascript">alert("注册失败,请重试!");</script>
<%
        request.getRequestDispatcher("G.jsp").forward(request,response);
    }
%>
    
</body>
</html>

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

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

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

 

 

 

posted @ 2022-05-08 16:51  DEae86  阅读(5)  评论(0编辑  收藏  举报