Java实战之实现用户登录

论坛 期权论坛 脚本     
niminba   2021-5-16 23:32   12   0

一、前言

在这里插入图片描述
在这里插入图片描述

二、案例需求

1.编写login.html登录页面,username&password两个输入框

2.使用Druid数据库连接池技术,操作mysql,day14数据库中的user表

3.使用jdbcTemplate技术封装JDBC

4.登录成功跳转到SuccessServlet展示:登录成功!用户名,欢迎你

5.登录失败跳转到FailServlet展示:登录失败密码错误

三、开始第一步

首先创建项目,写html页面,配置文件,jar包

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="/day14/loginServlet" method="post">
    用户名:<input type="text" name="username"> <br>
    密码:<input type="password" name="password"> <br>
    <input type="submit" value="登录">
</form>
</body>
</html>

在这里插入图片描述

druid.properties配置文件:

driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql:///day14
username=root
password=root
#初始化连接数量
initialSize=5
#最大连接数
maxActive=10
#最大等待时间
maxWait=3000
filters=stat
timeBetweenEvictionRunsMillis=60000
minEvictableIdleTimeMillis=300000
validationQuery=SELECT 1
testWhileIdle=true
testOnBorrow=false
testOnReturn=false
poolPreparedStatements=false
maxPoolPreparedStatementPerConnectionSize=200

导入jar包:

在这里插入图片描述

四、第二步

在这里插入图片描述

创建user类:

package com.wzc.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 + '\'' +
                '}';
    }
}

五、第三步

创建jdbcUtils工具类

package com.wzc.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工具类 使用Druid连接池
public class JDBCUtils {
    private static DataSource ds ;
    static {
        try {
//            加载配置文件
            Properties pro = new Properties();
//        使用ClassLoader加载配置文件,获取字节输入流
            InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties");
            pro.load(is);
//            初始化连接池对象
             ds = DruidDataSourceFactory.createDataSource(pro);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
//        初始化连接池
    }
    /*获取连接池对象*/
    public static DataSource getDataSource(){
        return ds;
    }
    /*获取连接connection对象*/
    public static Connection getConnection() throws SQLException {
        return ds.getConnection();
    }

}

创建UserDao类,提供login方法:

package com.wzc.dao;

import com.wzc.domain.User;
import com.wzc.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 只有用户名j~XxnyFf{X[8^i[hУKKYx+hk.У{[*V&>KУ[hzXi
YУhYX[KyJ&fF^KУhXZ[6WGFW.Y(WGFW.ikk9SУ~GFW.Y(WGFW.ikk9^hXyNK~x#ЮKh.EW6W&R(	2fwCW6W&^(	2fwCW6W&S#ЮXW6W&^[is#Юikk9^iKzxУ6WE&G#У"E&G#У2VFR7B[fnYyNJh8^XZyDf&VKi[yJУb673&66У&R673''W6f#Ч6vR627CРЦ626W#Ц6R&VЦV7CРЦf&Vf7Bf&vWDW6WFЧV&2672&VFW7BТFW7@ТV&2fFW7BТW6W"W6W"W6W"ТG'Т&VE&GW6W"VR""Т77FV&6W"Т7G&vV"&VE&GW6W"&VR"Т77FV&V"Т6F6Vv66W74W6WFТRF6G&6RТ6F66F&vWDW6WFТRF6G&6RТ6F6V6F6WFТRF6G&6RТ&SУFcУjX[>KfZKZJh~y[yNih~z[K{NZIyX[4fZJh~y[Xh^Z{J.zKK^XNih~zhn{~{XyNyX[>ih~z[ZJ~Z^YZIiJ
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:1060120
帖子:212021
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP