基于MVC模型和分层模式完成登录和注册

  • 对页面进行设计

  1. 创建一个denglu.isp作为登录页面,书写form表单指定提交地址和提交方式

  1. 创建一个zhuce.isp作为注册页面,书写form表单指定提交地址和提交方式

  • 编写代码

1.创建一个bean包,负责写User的getset方法和tostring方法

package com.bean;

public class User {
    private Integer uid;
    private  String username;
    private  String password;
    private String phone;
    private String address;

    public Integer getUid() {
        return uid;
    }

    public void setUid(Integer uid) {
        this.uid = uid;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

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

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "User{" +
                "uid=" + uid +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", phone='" + phone + '\'' +
                ", address='" + address + '\'' +
                '}';
    }
}

2.dao包

查询

package com.dao;

import com.bean.User;
import com.util.JDBCUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Selectdao {
    private Connection con = null;
    private PreparedStatement ps=null;
    private ResultSet rs=null;
    public User doselect(String username, String password) throws SQLException {
        User user = null;
        //加载驱动
        con= JDBCUtil.getCon();
        //创建sql语句
        String sql = "select * from t_user where username=? and  password=?";
        //获取sql语句
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, username);
        ps.setObject(2, password);
        //生成一个结果集
        ResultSet rs = ps.executeQuery();
        //进行下一步操作
        while (rs.next()) {
            //根据字段名称获取表中的数据
            int uid = rs.getInt("uid");
            String username1 = rs.getString("username");
            String password1 = rs.getString("password");
            String phone = rs.getString("phone");
            String address = rs.getString("address");
            //把以上数据封装到User对象中
            user = new User();
            //一行数据就封装成了一个Student对象
            user.setUid(uid);
            user.setUsername(username1);
            user.setPassword(password1);
            user.setAddress(address);
            user.setPhone(phone);
        }
        //关闭数据
        JDBCUtil.close(rs,ps,con);
        return user;
    }
}

注册

package com.dao;

import com.bean.User;
import com.util.JDBCUtil;

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

public class Insertdao {
    private Connection con = null;
    private PreparedStatement ps=null;
    private ResultSet rs=null;
    public Boolean doInsert(User user) throws SQLException {
        Boolean bool = null;
        //加载驱动
        con = JDBCUtil.getCon();
        //创建sql语句
        String sql = "insert into t_user(username,password) values(?,?)";
        //获取sql语句
        PreparedStatement ps = con.prepareStatement(sql);
        //传参
        ps.setObject(1, user.getUsername());
        ps.setObject(2, user.getPassword());
        int i = ps.executeUpdate();
        if (i > 0) {
            bool = true;
        } else {
            bool = false;
        }
        //关闭数据
        JDBCUtil.close(ps,con);
        return bool;
    }
}

3.selvlet包

查询的响应

package com.servlet;

import com.bean.User;
import com.dao.Selectdao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebServlet("/login")
public class Login extends HttpServlet{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.设置请求代码的格式
        req.setCharacterEncoding("utf-8");//设置请求代码为中文
        resp.setCharacterEncoding("utf-8");//设置响应的代码为中文
        resp.setContentType("text/html;charset=UTF-8");//以什么样的格式响应(文本/html)

        //2.获取请求的参数
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        System.out.println(username);
        System.out.println(password);

        //3.处理
        User user = null;

        //1.调用jdbc查询方法进行操作
        Selectdao selectdao=new Selectdao();
        try {
            user = selectdao.doselect(username, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (user != null) {
            resp.sendRedirect("zhuye.jsp");
        } else {
            resp.sendRedirect("error.jsp");
        }
    }
}

注册的响应

package com.servlet;

import com.bean.User;
import com.dao.Insertdao;
import com.dao.Selectdao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;


@WebServlet("/login1")
public class Login1 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.设置请求代码的格式
        req.setCharacterEncoding("utf-8");//设置请求代码为中文
        resp.setCharacterEncoding("utf-8");//设置响应的代码为中文
        resp.setContentType("text/html;charset=UTF-8");//以什么样的格式响应(文本/html)

        //2.获取请求的参数
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        System.out.println(username);
        System.out.println(password);
        User user = null;
        user=new User();
        user.setUsername(username);
        user.setPassword(password);
        System.out.println(user);


        //3.处理
        Boolean bool = null;
        //4.调用jdbc新增数据的方法进行操作
        Insertdao insertdao = new Insertdao();
        try {
            bool = insertdao.doInsert(user);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        if (bool != false) {
            resp.sendRedirect("chenggong.jsp");
        } else {
            resp.sendRedirect("shibai.jsp");
        }

    }
}

4.util包

package com.util;

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

public class JDBCUtil {
        private static String driver="com.mysql.jdbc.Driver";
        private static String url="jdbc:mysql://127.0.0.1:3306/mydb?useSSL=false&serverTimezone=UTC";
        private static String user="root";
        private static String password="root";

        private static Connection con=null;

        /**
         * 获取数据库链接对象
         */
        public static Connection getCon() {
            try {
                Class.forName(driver);
                con= DriverManager.getConnection(url,user,password);
            }catch (Exception e){
                e.printStackTrace();
            }
            return con;
        }

        //关闭数据库
        public static void close(PreparedStatement ps, Connection con){
            try{
                if (ps != null) {
                    ps.close();
                }
                if (con != null) {
                    con.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }

        public static void close(ResultSet rs, PreparedStatement ps, Connection con){
            try{
                if (rs != null) {
                    rs.close();
                }
                if (ps != null) {
                    ps.close();
                }
                if (con != null) {
                    con.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
}
  • 运行结果

进行登录

登录成功

登录失败

进行注册

注册成功

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值