JSP第七周作业

1.用户表:user_id (主键,自动增长)  user_name, user_email,  user_phone,user_password,user_zhishang  表结构如下

 

 

 2.使用分层实现注册。(必做)

UserBean:

package com.example.bean;

public class User {
    private int UserID;
    private String UserName;
    private String UserEmail;
    private String Userphone;
    private String UserZhishang;
    private String Userpassword;

    public User(int userID, String userName, String userEmail, String userphone, String userZhishang, String userpassword) {
        UserID = userID;
        UserName = userName;
        UserEmail = userEmail;
        Userphone = userphone;
        UserZhishang = userZhishang;
        Userpassword = userpassword;
    }
    public User(String userName, String userEmail, String userphone, String userZhishang, String userpassword) {
        UserName = userName;
        UserEmail = userEmail;
        Userphone = userphone;
        UserZhishang = userZhishang;
        Userpassword = userpassword;
    }

    public User() {
    }

    public String getUserpassword() {
        return Userpassword;
    }

    public void setUserpassword(String userpassword) {
        Userpassword = userpassword;
    }

    public int getUserID() {
        return UserID;
    }

    public void setUserID(int userID) {
        UserID = userID;
    }

    public String getUserName() {
        return UserName;
    }

    public void setUserName(String userName) {
        UserName = userName;
    }

    public String getUserEmail() {
        return UserEmail;
    }

    public void setUserEmail(String userEmail) {
        UserEmail = userEmail;
    }

    public String getUserphone() {
        return Userphone;
    }

    public void setUserphone(String userphone) {
        Userphone = userphone;
    }

    public String getUserZhishang() {
        return UserZhishang;
    }

    public void setUserZhishang(String userZhishang) {
        UserZhishang = userZhishang;
    }

    @Override
    public String toString() {
        return "User{" +
                "UserID=" + UserID +
                ", UserName='" + UserName + '\'' +
                ", UserEmail='" + UserEmail + '\'' +
                ", Userphone=" + Userphone +
                ", UserZhishang='" + UserZhishang + '\'' +
                ", Userpassword='" + Userpassword + '\'' +
                '}';
    }
}

UserDao层:

package com.example.Dao;

import com.example.bean.User;

import java.sql.*;
import java.util.ArrayList;

public class UserDao {
    String url="jdbc:mysql://localhost:3306/axing?characterEncoding=utf8";
    String username="root";
    String password="";

    public ArrayList<User> getlist(){
        ArrayList<User> list =new ArrayList<>();
        Connection con=null;
        PreparedStatement pre=null;
        ResultSet rs=null;
        try {
            con = DriverManager.getConnection(url, username, password);
            String sql="SELECT user_id,user_name ,user_email ,user_phone,user_password,user_zhishang FROM USER";
            pre= con.prepareStatement(sql);
            rs = pre.executeQuery();
            while (rs.next()){
                User u =new User();
                u.setUserID(rs.getInt(1));
                u.setUserName(rs.getString(2));
                u.setUserEmail(rs.getString(3));
                u.setUserphone(rs.getString(4));
                u.setUserpassword(rs.getString(5));
                u.setUserZhishang(rs.getString(6));
                list.add(u);
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            try {
                if (rs!=null)
                    rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            try {
                if (pre!=null)
                    pre.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            try {
                if (con!=null)
                    con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return  list;
    }
    public void addUser(User u){
        Connection con=null;
        PreparedStatement pre=null;
        try {
            con= DriverManager.getConnection(url, username, password);
            String sql="INSERT INTO USER (user_name,user_email,user_phone,user_password,user_zhishang)VALUES(?,?,?,?,?)";
            pre = con.prepareStatement(sql);
            pre.setString(1,u.getUserName());
            pre.setString(2,u.getUserEmail());
            pre.setString(3,u.getUserphone());
            pre.setString(4,u.getUserpassword());
            pre.setString(5,u.getUserZhishang());
            int m = pre.executeUpdate();
            if (m!=0){
                //跳转成功
                System.out.println("添加成功");
            }else {
                //跳转失败
                System.out.println("添加失败");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            try {
                if (con!=null)
                con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            try {
                if (pre!=null)
                pre.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }


    public boolean QueryUser(String u,String p){
        Connection con=null;
        PreparedStatement pre=null;
        try {
            con= DriverManager.getConnection(url, username, password);
            String sql="SELECT * FROM USER WHERE user_name=? AND user_password=?";
            pre = con.prepareStatement(sql);
            pre.setString(1,u);
            pre.setString(2,p);
            pre.executeQuery();
            return true;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            try {
                if (con!=null)
                    con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            try {
                if (pre!=null)
                    pre.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return  false;
    }
}

service层:

package com.example.servlet;

import com.example.Dao.UserDao;
import com.example.bean.StudentInfo;
import com.example.bean.User;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.*;

public class Register extends HttpServlet {
    @Override
    public void init() throws ServletException {
        super.init();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8");
        StudentInfo userBean=new StudentInfo();
        req.setAttribute("userBean",userBean);
        String register_username = req.getParameter("username").trim();
        String password1=req.getParameter("password1").trim();
        String password2=req.getParameter("password2").trim();
        String email = req.getParameter("email").trim();
        String phone = req.getParameter("phone").trim();
        String zhishang=new String(req.getParameter("select").getBytes("iso-8859-1"), "utf-8").trim();
        boolean boo=register_username.length()>0&&password1.length()>0;
        if (boo){
            User u=new User(register_username,email,phone,zhishang,password1);
            UserDao userDao=new UserDao();
            userDao.addUser(u);
            resp.getWriter().println("<h1>注册成功<h1>");
            resp.getWriter().println("<h1>5s后页面跳转至登陆界面....</h1>");
            resp.getWriter().println("<a href=\"login.jsp\">立即去登陆界面</a>");
            resp.setHeader("Refresh", "5;url=login.jsp");
        }else {
            resp.getWriter().println("<h1>注册失败<h1>");
            resp.getWriter().println("<h1>5s后页面跳转至注册界面....</h1>");
            resp.getWriter().println("<a href=\"register.jsp\">立即去注册界面</a>");
            resp.setHeader("Refresh", "5;url=register.jsp");
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}

添加一个用户名为:sunhehe 密码为:sunhehe 邮箱为sunhehe@qq.com 号码为:17741369486 智商:低能

 

注册成功 --------------------------------

3.使用分层实现登录。(选做)

 JavaBean:

package com.example.bean;

public class User {
    private int UserID;
    private String UserName;
    private String UserEmail;
    private String Userphone;
    private String UserZhishang;
    private String Userpassword;

    public User(int userID, String userName, String userEmail, String userphone, String userZhishang, String userpassword) {
        UserID = userID;
        UserName = userName;
        UserEmail = userEmail;
        Userphone = userphone;
        UserZhishang = userZhishang;
        Userpassword = userpassword;
    }
    public User(String userName, String userEmail, String userphone, String userZhishang, String userpassword) {
        UserName = userName;
        UserEmail = userEmail;
        Userphone = userphone;
        UserZhishang = userZhishang;
        Userpassword = userpassword;
    }

    public User() {
    }

    public String getUserpassword() {
        return Userpassword;
    }

    public void setUserpassword(String userpassword) {
        Userpassword = userpassword;
    }

    public int getUserID() {
        return UserID;
    }

    public void setUserID(int userID) {
        UserID = userID;
    }

    public String getUserName() {
        return UserName;
    }

    public void setUserName(String userName) {
        UserName = userName;
    }

    public String getUserEmail() {
        return UserEmail;
    }

    public void setUserEmail(String userEmail) {
        UserEmail = userEmail;
    }

    public String getUserphone() {
        return Userphone;
    }

    public void setUserphone(String userphone) {
        Userphone = userphone;
    }

    public String getUserZhishang() {
        return UserZhishang;
    }

    public void setUserZhishang(String userZhishang) {
        UserZhishang = userZhishang;
    }

    @Override
    public String toString() {
        return "User{" +
                "UserID=" + UserID +
                ", UserName='" + UserName + '\'' +
                ", UserEmail='" + UserEmail + '\'' +
                ", Userphone=" + Userphone +
                ", UserZhishang='" + UserZhishang + '\'' +
                ", Userpassword='" + Userpassword + '\'' +
                '}';
    }
}

UserDao:

public boolean QueryUser(String u,String p){
        Connection con=null;
        PreparedStatement pre=null;
        try {
            con= DriverManager.getConnection(url, username, password);
            String sql="SELECT * FROM USER WHERE user_name=? AND user_password=?";
            pre = con.prepareStatement(sql);
            pre.setString(1,u);
            pre.setString(2,p);
            ResultSet rs = pre.executeQuery();
            boolean b = rs.next();
            return b;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            try {
                if (con!=null)
                    con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            try {
                if (pre!=null)
                    pre.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return  false;
    }

 

Service:

package com.example.servlet;

import com.example.Dao.ConnectionPool;
import com.example.Dao.UserDao;
import com.example.bean.User;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.*;

public class Login extends HttpServlet {
    @Override
    public void init() throws ServletException {
        super.init();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=UTF-8");
        String uusername = req.getParameter("username").trim();
        String ppassword = req.getParameter("password").trim();
        int code1Int= 0;
        int code2Int= 0;
        int yzmInt= 0;
        if (code1Int+code2Int==yzmInt) {
            try {
                String yzm = req.getParameter("verify").trim();
                String code1 = req.getParameter("code1").trim();
                String code2 = req.getParameter("code2").trim();
                code1Int = Integer.parseInt(code1);
                code2Int = Integer.parseInt(code2);
                yzmInt = Integer.parseInt(yzm);
                UserDao userDao = new UserDao();
                boolean b = userDao.QueryUser(uusername, ppassword);
                if (code1Int + code2Int == yzmInt) {
                    if (b == true) {
                        HttpSession session = req.getSession();
                        session.setAttribute("uusername", uusername);
                        req.getRequestDispatcher("index.jsp").forward(req, resp);
                    } else {
                        resp.getWriter().println("<h1>登陆失败<h1>");
                        resp.getWriter().println("<h1>用户名或密码输入错误<h1>");
                        resp.getWriter().println("<h1>5s后页面跳转....</h1>");
                        resp.getWriter().println("<a href=\"login.jsp\">返回至登陆界面</a>");
                        resp.setHeader("Refresh", "5;url=login.jsp");
                    }
                } else {
                    resp.getWriter().println("<h1>验证码输入错误<h1>");
                    resp.getWriter().println("<h1>3s后页面跳转....</h1>");
                    resp.getWriter().println("<a href=\"login.jsp\">返回至登陆界面</a>");
                    resp.setHeader("Refresh", "3;url=login.jsp");
                }
            } catch (NumberFormatException e) {
                resp.getWriter().println("<h1>验证码为空,千万不可以为空哦<h1>");
                resp.getWriter().println("<h1>3s后页面跳转....</h1>");
                resp.getWriter().println("<a href=\"login.jsp\">返回至登陆界面</a>");
                resp.setHeader("Refresh","3;url=login.jsp");
            }
        }
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

    public void success(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }
}

用户名为sunhehe 密码为sunhehe 登陆------------------------

 

 

登陆成功的话去主页---------------

 

用户登录失败的话————会重新跳转登陆页面

 

 

 

 在重新查看一下 数据库表 查看是否有sunhehe

 

posted @ 2021-04-14 12:46  快乐阿星  阅读(163)  评论(0编辑  收藏  举报