结合所学的Servlet知识点及web三层架构,做一个简单的登录小案例。
以及输入账号密码登录后跳转的主页面
登录需要从数据库校验对应数据,通过jdbc进行连接数据库,配置jdbc。
配置jdbc.properties
在resource中添加jdbc.properties配置文件
driverClass = com.mysql.cj.jdbc.Driver
username = root
password = 123456
url = jdbc:mysql://localhost:3306/whn1?characterEncoding=UTF-8
加入Config配置类
创建util包,加入config配置类
package com.by.util;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
public class Config {
private static String username;
private static String password;
private static String url;
private static String driverClass;
static{
//特殊Hashtable
Properties prop = new Properties();
try {
//读取类路径下的配置文件
InputStream is = Config.class.getClassLoader().getResourceAsStream("jdbc.properties");
//InputStream in = new FileInputStream("javaweb\\user_mgr\\src\\main\\resources\\jdbc.properties");
//加载配置文件内容
prop.load(is);
// acquire the properties from file
username = prop.getProperty("username");
password = prop.getProperty("password");
url = prop.getProperty("url");
driverClass = prop.getProperty("driverClass");
// register the driver
if(driverClass != null && url != null && username != null && password != null){
//加载驱动
Class.forName(driverClass);
}
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
} catc