JavaWeb之登录功能的实现
项目说明
本项目实现了登录功能。在主页点击“我要登录”链接,跳转到登录页面。在登录页面,输入用户名和密码,点击登录,提交给LoginServlet做处理。查询数据库表中的数据,如果用户名和密码正确,则重定向到登录成功页面;如果用户名或密码错误,则请求转发到登录页面。
开发环境
开发工具:spring-tool-suite-4
数据库:Mysql
服务器:tomcat7
jdk:1.8
数据库的结构和数据
/*
Navicat MySQL Data Transfer
Source Server : localhost
Source Server Version : 50716
Source Host : localhost:3306
Source Database : test
Target Server Type : MYSQL
Target Server Version : 50716
File Encoding : 65001
Date: 2020-07-30 10:28:07
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for users
-- ----------------------------
DROP TABLE IF EXISTS `users`;
CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(100) NOT NULL,
`password` varchar(100) NOT NULL,
`email` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of users
-- ----------------------------
INSERT INTO `users` VALUES ('1', 'admin', '123456', 'admin@atguigu.com');
项目实现
(1)在bean包下,实现了User实体类,定义用户对象的私有成员变量,并用get和set方法进行封装,有参和无参和构造方法,重写toString方法
package com.atguigu.bean;
public class User {
private int id;
private String username;
private String password;
private String email;
public User() {
super();
}
public User(int id, String username, String password, String email) {
super();
this.id = id;
this.username = username;
this.password = password;
this.email = email;
}
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 String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", password=" + password + ", email=" + email + "]";
}
}
(2)在dao包下,定义UserDao接口,用UserDaoImpl类继承这个接口,并重写接口中的checkUsernameAndPassword方法,该方法可根据用户名和密码在数据库中查询对应的记录。有,则返回一个User对象,无则返回null
UserDao接口代码如下
package com.atguigu.dao;
import com.atguigu.bean.User;
public interface UserDao {
/**
* 根据用户名和密码在数据库中查询对应的记录
* @param username
* @param Password
* @return User 有此记录 null 无此记录
*/
User checkUsernameAndPassword(String username,String Password);
}
UserDaoImpl代码如下
package com.atguigu.dao.impl;
import com.atguigu.bean.User;
import com.atguigu.dao.BasicDao;
import com.atguigu.dao.UserDao;
public class UserDaoImpl implements UserDao {
//创建BasicDao对象
BasicDao basicDao = new BasicDao();
@Override
public User checkUsernameAndPassword(String username, String password) {
//写sql语句
String sql = "select id,username,password,email from users where username = ? and password = ?";
User user = basicDao.getBean(User.class, sql, username,password);
return user;
}
}
BasicDao类提供了对数据库进行增删改查的Dao,代码如下
package com.atguigu.dao;
import java.sql.Connection;
import java.sql.SQLException;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import com.atguigu.utils.JDBCUtils;
public class BasicDao {
/*
* 提供了对数据库进行增删改查的Dao
*/
private QueryRunner queryRunner = new QueryRunner();
/**
* 通用的增删改的方法
* @param sql
* @param params
* @return
*/
public int update(String sql,Object... params) {
//获取连接
Connection connection = JDBCUtils.getConnection();
int count = 0;
try {
count = queryRunner.update(connection, sql, params);
}catch(SQLException e) {
e.printStackTrace();
}finally {
JDBCUtils.releaseConnection(connection);
}
return count;
}
/**
* 获取一个对象的方法
* @param
* @param type
* @param sql
* @param params
* @return
*/
public T getBean(Class type,String sql,Object... params) {
//获取连接
Connection connection = JDBCUtils.getConnection();
T t = null;
try {
t = queryRunner.query(connection, sql, new BeanHandler(type), params);
}catch(SQLException e) {
e.printStackTrace();
}finally {
JDBCUtils.releaseConnection(connection);
}
return t;
}
}
(3)在servlet包下,定义了一个Servlet,即LoginServlet,使用doGet和doPost方法处理用户的登录请求
package com.atguigu.servlet;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.atguigu.bean.User;
import com.atguigu.dao.UserDao;
import com.atguigu.dao.impl.UserDaoImpl;
/**
* 处理用户登录的Servlet
*/
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public LoginServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取用户名和密码
String username = request.getParameter("username");
String password = request.getParameter("password");
//创建UserDao对象
UserDao userDao = new UserDaoImpl();
//调用UserDao里面验证用户名和密码的方法
User user = userDao.checkUsernameAndPassword(username, password);
if(user != null) {
//用户名和密码正确
response.sendRedirect(request.getContextPath()+"/pages/login_success.html");
}else {
//用户名或密码不正确
//获取转发器
RequestDispatcher requestDispatcher = request.getRequestDispatcher("/pages/login.html");
//进行请求转发
requestDispatcher.forward(request, response);
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
(4)test包,负责进行单元测试
ConnectionTest类测试数据库是否连接成功
package com.atguigu.test;
import java.sql.Connection;
import org.junit.jupiter.api.Test;
import com.atguigu.utils.JDBCUtils;
class ConnectionTest {
@Test
void test() {
Connection connection = JDBCUtils.getConnection();
System.out.println(connection);
}
}
UserdaoTest类测试UserDao接口的方法能否实现
package com.atguigu.test;
import org.junit.jupiter.api.Test;
import com.atguigu.bean.User;
import com.atguigu.dao.UserDao;
import com.atguigu.dao.impl.UserDaoImpl;
class UserDaoTest {
UserDao userDao = new UserDaoImpl();
@Test
void testCheckUsernameAndPassword() {
User user = userDao.checkUsernameAndPassword("admin", "123456");
System.out.println(user);
}
}
(5)在utils包下,定义JDBCUtils工具类,用于获取和释放Connection连接
package com.atguigu.utils;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
import javax.sql.DataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
/*
* 获取连接和释放连接的工具类
*/
public class JDBCUtils {
private static DataSource dataSource;
static {
try {
//1、读取druid.properties文件
Properties pro = new Properties();
pro.load(JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties"));
//2、连接连接池
dataSource = DruidDataSourceFactory.createDataSource(pro);
}catch(Exception e) {
e.printStackTrace();
}
}
//获取连接
public static Connection getConnection() {
Connection connection = null;
try {
connection = dataSource.getConnection();
}catch(SQLException e) {
e.printStackTrace();
}
return connection;
}
//释放连接
public static void releaseConnection(Connection connection) {
if(connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
(6)在config源文件下,定义druid配置文件,配置数据库连接池的相关属性
# key=value
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test?rewriteBatchedStatements=true
username=root
password=root
initialSize=10
minIdle=5
maxActive=20
maxWait=5000
(7)jar包说明,可在maven中央仓库下载相应的jar包,复制到WEB-INF下的lib文件夹下
(8)前端页面说明
index,html页面代码
Insert title here
我要登录
我要注册
login.html页面代码
Insert title here
欢迎登录
login_success.html代码
Insert title here
登录成功
回首页