MVC模型和分层模式完成登录和注册,书写博客;

  1. 对浏览器的前端进行设计,完成登录和注册的操作页面

登录的页面前端设计代码:
注册的页面设计代码:
  1. 用mvc模式进行分层操作

1.View(视图):用户的操作页面,如html、jsp。
2.Model(模型):具体的业务模型与数据模型。如:service、dao、pojo
3.controller(控制):处理从视图层面发送的请求,并选择模型层的业务模型完成响应的业务实现,并产生响应。如:Servlet

3.编写模型层面的代码

bean.User里面的代码
package com.wang.bean;

import com.wang.dao.UserDao;

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

    //get和set方法

    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 getAddress() {
        return address;
    }

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

    public String getPhone() {
        return phone;
    }

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

    @Override
    public String toString() {
        return "User{" +
                "uid=" + uid +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", address='" + address + '\'' +
                '}';
    }
}
Util.JDBCUtil里面的代码
package com.wang.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://localhost:3306/mydb";
    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();
        }
    }
}
dao.Select里面的代码(jdbc查询)
package com.wang.dao.impl;
import com.wang.bean.User;
import com.wang.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;
    }
}

dao.Insert里面的代码(Jdbc注册)

package com.wang.dao.impl;
import com.wang.bean.User;
import com.wang.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;
    }
}

4.控制器层面的代码

servlet.Login(登录的响应操作)
package com.wang.servlet;
import com.wang.bean.User;
import com.wang.dao.impl.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("/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 (SQLException throwables) {
                throwables.printStackTrace();
            }
            if (user != null) {
                resp.sendRedirect("zhuye.jsp");
            } else {
                resp.sendRedirect("error.jsp");
            }
        }
    }
servlet.Login1(注册的响应操作)
package com.wang.servlet;
import com.wang.bean.User;
import com.wang.dao.impl.Insertdao;
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");
        }
    }
}

根据数据库里面的数据进行登录操作

成功了为:

错误了为:

这侧页面为:

注册成功为:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值