欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

com.media.bean UserBean

程序员文章站 2022-06-28 16:56:49
userbeanpackage com.media.bean;public class UserBean { private int id; private String username; private String password; private int gender; private int age; private int status; public int getId() { return id; }...
  1. com.media.bean UserBean
package com.media.bean;
public class UserBean {
    private int id;
    private String username;
    private String password;
    private int gender;
    private int age;
    private int status;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    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 int getGender() {
        return gender;
    }
    public void setGender(int gender) {
        this.gender = gender;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public int getStatus() {
        return status;
    }
    public void setStatus(int status) {
        this.status = status;
    }
}

2.com.media.controller LoginController

package com.media.controller;
import com.media.util.DBUtil;
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 javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * Servlet 注解配置信息
 */
@WebServlet(name = "LoginServlet",value = "/login")
public class LoginController extends HttpServlet {

    /**
     * post请求 会执行doPost方法
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
    /**
     * get请求 会执行doGet方法
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("servlet 请求");
        request.setCharacterEncoding("utf-8");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        HttpSession session = request.getSession();
        if("1".equals(request.getParameter("invalidate")) && session.getAttribute("username") != null){
            session.invalidate();
            //session.removeAttribute("username");
            response.sendRedirect("login.jsp");
        }
        try {
            Connection connection = DBUtil.getConnection();
            //sql 模板
            String sql = "select * from user where username = ? and password = ?";
            //使用PreparedStatement对象执行sql语句
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1,username);
            ps.setString(2,password);
            ResultSet rs = ps.executeQuery();
            //response.setContentType("text/html;charset=UTF-8");
            PrintWriter out = response.getWriter();
            if(rs.next()){
                //表示登录成功
                session.setAttribute("username",username);
                response.sendRedirect("home.jsp");
            }else{
                //表示登录未成功
                out.print("登录失败,3秒之后跳转登录页面...");
                response.setHeader("refresh","3;url=login.jsp");
            }
            //服务端跳转
            //request.getRequestDispatcher("home.jsp").forward(request,response);
            DBUtil.close(connection,ps,rs);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

3.com.media.controller UserController

package com.media.controller;
import com.media.bean.UserBean;
import com.media.service.UserService;
import com.media.service.impl.UserServiceImpl;
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("/user")
public class UserController extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String method = request.getParameter("method");
        UserBean userBean = new UserBean();
        userBean.setUsername(request.getParameter("username"));
        userBean.setPassword(request.getParameter("password"));
        UserService userService = new UserServiceImpl();
        if("addUser".equals(method)){
            try {
                int count = userService.addUser(userBean);
                if(count == 0){
                    response.sendRedirect("");
                }else{
                    response.sendRedirect("home.jsp");
                }
            }catch (SQLException e){
                e.printStackTrace();
            }
        }
        if("editUser".equals(method)){
            userBean.setId(Integer.parseInt(request.getParameter("id")));
            userService.editUser(userBean);
        }
        if("deleteUser".equals(method)){
            userService.deleteUserById(Integer.parseInt(request.getParameter("id")));
        }
    }
}

4.com.media.dao.impl UserDaoImpl

package com.media.dao.impl;
import com.media.bean.UserBean;
import com.media.dao.UserDao;
import com.media.util.DBUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class UserDaoImpl implements UserDao {
    @Override
    public UserBean getUserById(int id) {
        UserBean userBean = new UserBean();
        try{
            Connection connection = DBUtil.getConnection();
            String sql = "select * from user where id = ?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setInt(1,id);
            ResultSet rs = ps.executeQuery();
            if(rs.next()){
                userBean.setId(rs.getInt("id"));
                userBean.setUsername(rs.getString("username"));
                userBean.setPassword(rs.getString("password"));
                userBean.setGender(rs.getInt("gender"));
                userBean.setAge(rs.getInt("age"));
                userBean.setStatus(rs.getInt("status"));
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
        return userBean;
    }
    @Override
    public UserBean getUserByName(String username) {
        UserBean userBean = new UserBean();
        try{
            Connection connection = DBUtil.getConnection();
            String sql = "select * from user where username = ?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1,username);
            ResultSet rs = ps.executeQuery();
            if(rs.next()){
                userBean.setUsername(rs.getString("username"));
                userBean.setPassword(rs.getString("password"));
                userBean.setGender(rs.getInt("gender"));
                userBean.setAge(rs.getInt("age"));
                userBean.setStatus(rs.getInt("status"));
                userBean.setId(rs.getInt("id"));
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
        return userBean;
    }
    @Override
    public List<UserBean> getUserList(UserBean user) {
        List<UserBean> userList = new ArrayList<>();
        UserBean userBean = null;
        try{
            Connection connection = DBUtil.getConnection();
            String sql = "select * from user where username like ?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1,user.getUsername());
            ResultSet rs = ps.executeQuery();
            while (rs.next()){
                userBean = new UserBean();
                userBean.setUsername(rs.getString("username"));
                userBean.setPassword(rs.getString("password"));
                userBean.setGender(rs.getInt("gender"));
                userBean.setAge(rs.getInt("age"));
                userBean.setId(rs.getInt("id"));
                userBean.setStatus(rs.getInt("status"));
                userList.add(userBean);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
        return userList;
    }
    @Override
    public int addUser(UserBean user) throws SQLException {
        Connection connection = DBUtil.getConnection();
        String sql = "inert into user (username,password,age,gender,status) values (?,?,?,?,?)";
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setString(1,user.getUsername());
        ps.setString(2,user.getPassword());
        ps.setInt(3,user.getAge());
        ps.setInt(4,user.getGender());
        ps.setInt(5,user.getStatus());
        int count = ps.executeUpdate();
        DBUtil.close(connection,ps,null);
        return count;
    }
    @Override
    public int editUser(UserBean user) {
        return 0;
    }
    @Override
    public int deleteUserById(int id) {
        return 0;
    }
}

5.com.media.dao UserDao

package com.media.dao;
import com.media.bean.UserBean;
import java.sql.SQLException;
import java.util.List;
public interface UserDao {
    UserBean getUserById(int id);
    UserBean getUserByName(String username);
    List<UserBean> getUserList(UserBean user);
    int addUser(UserBean user) throws SQLException;
    int editUser(UserBean user);
    int deleteUserById(int id);
}

6.com.media.service.impl UserServiceImpl

package com.media.service.impl;
import com.media.bean.UserBean;
import com.media.dao.UserDao;
import com.media.dao.impl.UserDaoImpl;
import com.media.service.UserService;
import java.sql.SQLException;
import java.util.List;
public class UserServiceImpl implements UserService {
    @Override
    public UserBean getUserById(int id) {
        return null;
    }
    @Override
    public UserBean getUserByName(String username) {
        return null;
    }
    @Override
    public List<UserBean> getUserList() {
        return null;
    }
    @Override
    public int addUser(UserBean user) throws SQLException {
        //业务层 负责业务数据处理
        UserDao userDao = new UserDaoImpl();
        return userDao.addUser(user);
    }
    @Override
    public int editUser(UserBean user) {
        return 0;
    }
    @Override
    public int deleteUserById(int id) {
        return 0;
    }
}

7.com.media.service UserService

package com.media.service;
import com.media.bean.UserBean;
import java.sql.SQLException;
import java.util.List;
public interface UserService {
    UserBean getUserById(int id);
    UserBean getUserByName(String username);
    List<UserBean> getUserList();
    int addUser(UserBean user) throws SQLException;
    int editUser(UserBean user);
    int deleteUserById(int id);
}

8.com.media.util DBUti

package com.media.util;
import java.sql.*;
public class DBUtil {
    public static Connection getConnection(){
        Connection connection = null;
        try {
            //加载数据库驱动
            Class.forName("com.mysql.jdbc.Driver");
            //建立数据库连接
            String url = "jdbc:mysql://localhost:3306/book";
            connection = DriverManager.getConnection(url,"root","root");
        }catch (Exception e){
            e.printStackTrace();
        }
        return connection;
    }
    public static void close(Connection connection, PreparedStatement ps, ResultSet rs){
        //关闭数据库连接
        if(rs != null){
            try{
                rs.close();
            }catch (SQLException e){
                e.printStackTrace();
            }
        }
        if(ps != null){
            try {
                ps.close();
            }catch (SQLException e){
                e.printStackTrace();
            }
        }
        if(connection != null){
            try {
                connection.close();
            }catch (SQLException e){
                e.printStackTrace();
            }
        }
    }
    /**
     * MVC Model 模型层  访问数据库  View 视图层 显示页面内容的  Controller 控制器 负责页面请求跳转的
     *
     * dao Data Access Object 数据访问对象
     *
     *
     */
}

9.login.jsp

<html>
<head>
    <title>登录页面</title>
    <%--引入css文件和js文件--%>
    <link rel="stylesheet" href="css/bootstrap.css">
    <script src="js/jquery.min.js"></script>
    <script src="js/bootstrap.js"></script>
    <script src="js/bootstrap.bundle.js"></script>
    <style>
        .container{
            width: 20%;
            height: 20%;
            margin-top: 10%;
        }
    </style>
</head>
<body>
<div class="container">
    <div class="row clearfix">
        <div class="col-md-12 column">
            <form role="form" method="post" action="user?method=addUser">
                <h3 style="text-align: center">登录</h3>
                <div class="form-group">
                    <label for="exampleInputEmail1">用户名:</label><input type="text" name="username" class="form-control" id="exampleInputEmail1" />
                </div>
                <div class="form-group">
                    <label for="exampleInputPassword1">密码:</label><input type="password" name="password" class="form-control" id="exampleInputPassword1" />
                </div>
                <button type="submit" style="width: 100%" class="btn btn-info">提交</button>
            </form>
        </div>
    </div>
</div>
</body>
</html>

本文地址:https://blog.csdn.net/weixin_45820586/article/details/110219691