JSP第10次作业

1.
<%@ 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="doreg.jsp" method="post">

        用户名:<input type="text" name="uname"><br> 
        密   码: <input type="password" name="password"> <br>
        确认密码: <input type="password" name="password"> <br>
        年   龄:<input type="text" name="age"><br>
        <input type="submit" value="注册">
        <a href="login.jsp">登录</a>
    </form>
    
  </body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="com.gd.entity.User"%>
<%@ page import="com.gd.entity.StuDao"%>
<%
    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);
    StuDao stuDao = new StuDao();
    stuDao.regUser(user);
    response.sendRedirect("login.jsp");
%>
<%@ 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="register.jsp">注册</a>
    </form>

</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="com.gd.entity.User"%>
<%@ page import="com.gd.entity.StuDao"%>
<%
    request.setCharacterEncoding("utf-8");
    response.setCharacterEncoding("utf-8");
    String uname = request.getParameter("uname");
    String password = request.getParameter("password");
    StuDao stuDao = new StuDao();
    User user = stuDao.getUserByName(uname);
    if (password.equals(user.getPassword())) {
        response.sendRedirect("success.jsp");
    } else {
        response.sendRedirect("fail.jsp");
    }
%>
%@ 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 'success.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 'fail.jsp' starting page</title>
</head>

<body>
    <h1>登陆失败</h1>
    <%
        response.setHeader("refresh", "5;url=login.jsp");
    %>

</body>
</html>
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class StuDao extends BaseDao {
    public int Register(String uname, String password, int age) {
        int i = -1;
        Connection con = getConnection();
        String sql = "insert into stu(uname,password,age)values(?,?,?)";
        PreparedStatement pred = null;
        try {
            pred = con.prepareStatement(sql);
            pred.setString(1, uname);
            pred.setString(2, password);
            pred.setInt(3, age);
            i = pred.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll(con, pred, null);
        }
        return i;
    }
    public boolean Login(String uname, String password) {
        boolean f=false;
        Connection con = getConnection();
        String sql = "select * from stu where uname=? and password=?";
        PreparedStatement pred = null;
        ResultSet resultSet = null;
        try {
            pred = con.prepareStatement(sql);
            pred.setString(1, uname);
            pred.setString(2, password);
            resultSet = pred.executeQuery();
            while (resultSet.next()) {
                f=true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll(con, pred, resultSet);
        }
        return f;
    }
}
package com.gd.entity;

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;
    }
    

}

 

 

 

posted @ 2022-05-07 19:27  33。  阅读(21)  评论(0编辑  收藏  举报