美文网首页
案例:用户登录

案例:用户登录

作者: 开心的小哈 | 来源:发表于2022-03-05 13:58 被阅读0次

    案例:用户登录

    • 用户登录案例需求:

      1. 编写login.html登录页,username&password两个输入框
      2. 使用Druid数据库连接池技术,操作mysql,day14数据库中user表
      3. 使用JDBCTemplate技术方正JDBC
      4. 登录成功后跳转到SuccessServlet展示:登录成功!用户名,欢迎您
      5. 登录失败后跳转到FailServlet展示:登录失败,用户名或密码错误
    • 实现练习代码:
      1.将第三方jar包进行依赖,因为我使用的开源版IDEA所以不会创建javaee,web项目,然后我手动按照javaee动态项目结构一个文件夹一个文件夹的创建出来了;

    但在依赖过程中遇到的一个问题就是:import jakarta.servlet.http.HttpServlet;的HttpServlet类需要导报导入tomcat的包,我看网上说都是将HttpServlet单独的jar包导入,但是我这里是直接将tomcat的lib目录下的jar全部导入进来;
    导入后编写代码:
    login.html

    
    <!DOCTYPE html>  
    <html lang="zh-CN">  
    <head>  
     <meta charset="UTF-8">  
     <title>Title</title>  
    </head>  
    <body>  
    <form action="/loginDemo/loginServlet" method="post">  
     <input name="username">  
     <input name="password">  
     <input type="submit" value="提交">  
    </form>  
    </body>  
    </html>
    
    
    
    

    user.java

    package cn.itcast.domain;  
      
    public class User {  
        private int id;  
     private String username;  
     private String password;  
      
     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;  
     }  
      
        @Override  
     public String toString() {  
            return "User{" +  
                    "id=" + id +  
                    ", username='" + username + '\'' +  
                    ", password='" + password + '\'' +  
                    '}';  
     }  
    }
    

    userDao.java

    package cn.itcast.dao;  
      
    import cn.itcast.domain.User;  
    import cn.itcast.util.JDBCUtils;  
    import org.springframework.dao.DataAccessException;  
    import org.springframework.jdbc.core.BeanPropertyRowMapper;  
    import org.springframework.jdbc.core.JdbcTemplate;  
      
    /**  
     * 操作数据库中User表的类  
     */  
    public class UserDao {  
        //声明JDBCtemplate对象公用  
     private JdbcTemplate template=new JdbcTemplate(JDBCUtils.getDataSource());  
      
     /**  
     * 登录方法  
     * @param loginUser,只有用户名和密  
     * @return user包含用户全部数据  
     */  
     public User login(User loginUser){  
            try {  
                String sql = "select * from accoun where username= ? and password =  ?";  
     User user = template.queryForObject(sql,  
     new BeanPropertyRowMapper<User>(User.class),  
     loginUser.getUsername(), loginUser.getPassword());  
     return user;  
     }catch(DataAccessException e){  
                e.printStackTrace();  
     return null; }  
        }  
    }
    

    JDBCUtils.java

    package cn.itcast.util;  
      
    import com.alibaba.druid.pool.DruidDataSourceFactory;  
      
    import javax.sql.DataSource;  
    import java.io.IOException;  
    import java.io.InputStream;  
    import java.sql.Connection;  
    import java.sql.SQLException;  
    import java.util.Properties;  
      
    /**  
     * JDBC工具类,使用Durid连接池  
     */  
    public class JDBCUtils {  
        private static DataSource ds;  
      
     static{  
            //1.加载配置文件  
     Properties pro=new Properties();  
     InputStream resourceAsStream = JDBCUtils.class.getClassLoader().getResourceAsStream("dd.properties");  
     try {  
                pro.load(resourceAsStream);  
     ds = DruidDataSourceFactory.createDataSource(pro);  
     } catch (IOException e) {  
                e.printStackTrace();  
     } catch (Exception e) {  
                e.printStackTrace();  
     }  
            //初始化连接池对象  
     }  
      
        public static DataSource getDataSource(){  
            return ds;  
     }  
      
        public static Connection getConnection() throws SQLException{  
            return ds.getConnection();  
     }  
    }
    

    FailServlet.java

    
    package cn.itcast.web.servlet;  
      
    import jakarta.servlet.ServletException;  
    import jakarta.servlet.annotation.WebServlet;  
    import jakarta.servlet.http.HttpServlet;  
    import jakarta.servlet.http.HttpServletRequest;  
    import jakarta.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.setHeader("Content-type", "text/html;charset=UTF-8");  
     resp.setCharacterEncoding("UTF-8");  
     resp.getWriter().write("用户登录失败");  
     }  
        @Override  
     protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {  
            this.doGet(req,resp);  
     }  
    }
    

    SuccessServlet.java

    
    package cn.itcast.web.servlet;  
      
    import cn.itcast.domain.User;  
    import jakarta.servlet.ServletException;  
    import jakarta.servlet.annotation.WebServlet;  
    import jakarta.servlet.http.HttpServlet;  
    import jakarta.servlet.http.HttpServletRequest;  
    import jakarta.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");  
     resp.setHeader("Content-type", "text/html;charset=UTF-8");  
     resp.setCharacterEncoding("UTF-8");  
     resp.getWriter().write("登录成功欢迎您"+user.getUsername());  
     }  
        @Override  
     protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {  
            this.doGet(req,resp);  
     }  
    }
    

    loginServlet.java

    package cn.itcast.web.servlet;  
      
    import cn.itcast.dao.UserDao;  
    import cn.itcast.domain.User;  
    import jakarta.servlet.ServletException;  
      
    import jakarta.servlet.annotation.WebServlet;  
    import jakarta.servlet.http.HttpServlet;  
    import jakarta.servlet.http.HttpServletRequest;  
    import jakarta.servlet.http.HttpServletResponse;  
      
      
    import java.io.IOException;  
    @WebServlet("/loginServlet")  
    public class LoginServlet extends HttpServlet {  
        @Override  
     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {  
        //1.设置编码  
     req.setCharacterEncoding("utf-8");  
     String username = req.getParameter("username");  
     String password = req.getParameter("password");  
     User user = new User();  
     user.setUsername(username);  
     user.setPassword(password);  
     UserDao userDao=new UserDao();  
     User login = userDao.login(user);  
     if(login==null){  
            req.getRequestDispatcher("/failServlet").forward(req,resp);  
     }else{  
                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);  
     }  
    }
    

    dd.properties

    driverClassName=com.mysql.cj.jdbc.Driver  
    url=jdbc:mysql://localhost:3306/db1  
    username=root  
    password=502900  
    initalSize=2  
    maxActive=2  
    maxWait=3000
    
    

    BeanUtils工具类,简化封装
    用于封装javaBean的
    1.标准的java类
    1. 类必须被public修饰
    2. 必须是提供供餐的构造器
    3. 成员变量必须使用private修饰
    4. 提供公共setter和getter方法

    1. 功能:封装数据
    2. 概念:
      1. 成员变量:
      2. 属性:setter和getter方法截取后的产物
        1. 例如:getUsername方法()--> Username --> username成员变量
    3. 方法
      1. setProperty()设置属性值
      2. getProperty ()获取属性值
      3. populate(Object obj,map map)将map集合的键值对信息,封装到对应的bean类中

    相关文章

      网友评论

          本文标题:案例:用户登录

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