美文网首页
小练习--使用request对象实现登录

小练习--使用request对象实现登录

作者: RicherYY | 来源:发表于2020-08-23 14:44 被阅读0次

    db.properties

    driverClassName = com.mysql.cj.jdbc.Driver
    username = root
    password = 123456
    url = jdbc:mysql://localhost:3306/test?serverTimezone=UTC
    

    JDBCUtils

    package utils;
    
    
    import java.io.InputStream;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;
    import java.util.Properties;
    
    public class JDBCUtils {
    
        private static Properties properties = new Properties();
    
        static {
            try {
                InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("db.properties");
                properties.load(inputStream);
    
                Class.forName(properties.getProperty("driverClassName"));
            } catch (Exception e){
                e.printStackTrace();
            }
        }
    
        public static Connection getConn(){
            try {
               return DriverManager.getConnection(
                       properties.getProperty("url"),
                       properties.getProperty("username"),
                       properties.getProperty("password"));
            } catch (Exception e){
                e.printStackTrace();
            }
            return null;
        }
    
        public static void close(Connection conn, Statement st, ResultSet rs){
            try{
                if (rs != null){
                    rs.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                try {
                    if (st != null){
                        st.close();
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }finally {
                    try {
                        if (conn != null){
                            conn.close();
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    

    domian

    package domain;
    
    import lombok.Data;
    
    @Data
    public class User {
        private int id;
        private String username;
        private String password;
    }
    

    LoginServlet

    package server;
    
    import dao.UserDao;
    import domain.User;
    
    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("/loginServlet")
    public class LoginServlet extends HttpServlet {
    
        /**
         * 编写业务逻辑代码
         * @param req
         * @param resp
         * @throws ServletException
         * @throws IOException
         */
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
            //1.设置编码格式
            req.setCharacterEncoding("utf-8");
    
            //2.获取请求参数
            String username = req.getParameter("username");
            String password = req.getParameter("password");
    
            //3.封装User对象
            User loginUser = new User();
            loginUser.setUsername(username);
            loginUser.setPassword(password);
    
            //4.调用UserDao类中登录方法
            UserDao userDao = new UserDao();
    
    
            User user = userDao.login(loginUser);
    
            //5.进行判断
            if (user == null){
                req.getRequestDispatcher("/failServlet").forward(req,resp);
            }
    
            //存储数据
            req.setAttribute("user",user);
    
            //转发
            req.getRequestDispatcher("successServlet").forward(req,resp);
    
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            this.doGet(req,resp);
        }
    }
    
    

    SuccessServlet

    package server;
    
    import domain.User;
    
    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("/successServlet")
    public class SuccessServlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
           User user = (User) req.getAttribute("user");
    
            if(user != null){
                //给页面写一句话
    
                //设置编码
                resp.setContentType("text/html;charset=utf-8");
                //输出
                resp.getWriter().write("登录成功!"+user.getUsername()+",欢迎您");
            }
    
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            this.doGet(req,resp);
        }
    }
    
    

    FailServlet

    package server;
    
    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("/failServlet")
    public class FailServlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            //给页面写一句话
    
            //设置编码
            resp.setContentType("text/html;charset=utf-8");
            //输出
            resp.getWriter().write("登录失败,用户名或密码错误");
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            this.doGet(req,resp);
        }
    }
    

    相关文章

      网友评论

          本文标题:小练习--使用request对象实现登录

          本文链接:https://www.haomeiwen.com/subject/hjdsjktx.html