JSP第八次作业(第十周)

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

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

<%@ 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>
    <title>My JSP 'register.jsp' starting page</title>
    
  </head>
  
  <body>

    <form  action="doregister.jsp" method="post">

        用户名:<input type="text" name="uname"><br> 
        密   码: <input type="password" name="password"> <br>
        年   龄:<input type="text" name="age"><br>
        <input type="submit" value="注册"><hr>
    </form><hr>
    <a href="login.jsp">返回登录</a>
</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>
    <title>My JSP 'login.jsp' starting page</title>
    
  </head>
  <body>
  <script type="text/javascript">
        function validate() {
            if (loginForm.uname.value == "") {
                alert("账号不能为空!");
                return;
            }
            if (loginForm.password.value == "") {
                alert("密码不能为空!");
                return;
            }
            loginForm.submit();
        }
    </script>
    <form  name="loginForm" action="dologin.jsp" method="post">
        用户名:<input type="text" name="uname"><br> 
        密   码: <input type="password" name="password"> <br>
        <input type="button" value="登录" onclick="validate()"><hr>
    </form>
    <hr>
    <a href="register.jsp">返回注册</a>
</body>
 
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="homework.User" %>
<%@ page import="homework.Stu" %>
<%
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>
    <title>My JSP 'dologin.jsp' starting page</title>
    
  </head>
  
  <body >
    <%
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        String uname = request.getParameter("uname");
        String password = request.getParameter("password");
        Stu stu = new Stu();
        User user = stu.getUserByName(uname);
       if (password.equals(user.getPassword())) {
         response.sendRedirect("ok.jsp");
       } else {
        response.sendRedirect("no.jsp");
        }
    %>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="homework.User" %>
<%@ page import="homework.Stu" %>
<%
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>
    <title>My JSP 'doregister.jsp' starting page</title>
    
  </head>
  
  <body>
    <%
    request.setCharacterEncoding("utf-8");
    response.setCharacterEncoding("utf-8");
    String uname=request.getParameter("uname");
    String password=request.getParameter("password");
    Integer age=Integer.parseInt(request.getParameter("age"));
    User user=new User(uname,password,age);
    Stu stuDao=new Stu();
    stuDao.regUser(user);
    response.sendRedirect("login.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>
    <title>My JSP 'ok.jsp' starting page</title>
    
  </head>
  
  <body>
    <h1>登录成功!!!</h1>
  </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>
    <title>My JSP 'no.jsp' starting page</title>
    
  </head>
  
  <body>
       <h1>登录失败!3秒后将返回登录界面</h1>
<%
    response.setHeader("refresh", "3;url=login.jsp");
%>

  </body>
</html>
package homework;

public class User {
    private Integer stuId;
    private String uname;
    private String password;
    private Integer age;

    public User(String uname, String password, Integer age) {
        super();
        this.uname = uname;
        this.password = password;
        this.age = age;
    }

    public User() {
        super();
    }

    public String getUname() {
        return uname;
    }

    public void setUname(String uname) {
        this.uname = uname;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Integer getStuId() {
        return stuId;
    }

    public void setStuId(Integer stuId) {
        this.stuId = stuId;
    }

}
package homework;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class Dao {
    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static Connection getCon() {
        Connection cn = null;
        try {
            cn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return cn;
    }

}
package homework;

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

public class Stu {
    public void regUser(User user) {
        String sql = "insert into stu(uname,password,age) value(?,?,?)";
        Connection cn = null;
        PreparedStatement ps = null;
        // 创建连接
        cn = Dao.getCon();
        try {
            // 预编译
            ps = cn.prepareStatement(sql);
            // 替换占位符‘?’
            ps.setString(1, user.getUname());
            ps.setString(2, user.getPassword());
            ps.setInt(3, user.getAge());
            // 编译执行sql语句
            ps.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                ps.close();
                cn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    public User getUserByName(String uname) {
        String sql = "select * from stu where uname=?";
        Connection cn = null;
        PreparedStatement ps = null;
        ResultSet res = null;
        User user = null;
        cn = Dao.getCon();
        try {
            ps = cn.prepareStatement(sql);
            ps.setString(1, uname);
            res = ps.executeQuery();
            if (res.next()) {
                user = new User(res.getString("uname"), res.getString("password"), res.getInt("age"));
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                ps.close();
                cn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return user;
    }

}

 

 

 

 

 

 

 

 

 

posted @ 2022-05-07 13:54  小Q爱吃巧克力  阅读(23)  评论(0编辑  收藏  举报