文章目录
- 1.前置知识
- 1.项目开发阶段
- 2.Java经典三层架构
- 3.项目具体分层(包方案)
- 4.MVC
- 2.开发环境搭建
- 1.新建普通javaweb项目,导入jar包
- 2.创建项目结构
- 3.搭建前端页面
- 3.会员注册前端js校验
- 1.需求分析
- 2.代码
- login.html
- 3.结果
- 4.调试阶段
- 1.验证信息一闪而过
- 原因:
- 4.会员注册后端
- 1.需求分析
- 2.程序框架图
- 3.数据库表设计
- 会员表设计
- 4.建立javabean与表的映射
- Member.java
- 5.引入德鲁伊连接池工具类
- JDBCUtilsByDruid.java
- 6.引入BasicDao
- BasicDao.java
- 7.编写druid配置文件
- druid.properties
- 编写测试类,测试连接
- JDBCUtilsByDruidTest.java
- 8.编写MemberDao
- 1.MemberDao.java
- 2.MemberDaoImpl.java
- 3.进行单元测试
- MemberDaoTest.java
- 测试结果:
- 9.编写MemberService
- 1.MemberService.java
- 2.MemberServiceImpl.java
- 3.进行单元测试
- MemberServiceTest.java
- 测试结果
- 10.login.html优化
- 1.设置base路径
- 2.修改所有../../为空
- 3.运行login.html
- 4.修改前端js验证通过逻辑
- 11.编写RegisterServlet
- 1.修改login.html
- 2.引入两个前端页面
- 3.修改两个页面的base路径
- 4.RegisterServlet.java
- 12.注册功能展示
- 注册界面
- 注册成功
- 注册失败返回首页
1.前置知识
1.项目开发阶段
2.Java经典三层架构
3.项目具体分层(包方案)
4.MVC
2.开发环境搭建
1.新建普通javaweb项目,导入jar包
2.创建项目结构
3.搭建前端页面
3.会员注册前端js校验
1.需求分析
2.代码
login.html
<!-- 引入jquery-->
<script type="text/javascript" src="../../script/jquery-3.6.0.min.js"></script>
<script>
$(function () { //页面加载
//绑定注册按钮点击事件
$("#sub-btn").click(function () {
//获取username
var usernameVal = $("#username").val();
// alert(usernameVal);
//1.编写正则表达式验证用户名
var usernamePattern = /^\w{6,10}$/;
//验证
if (!usernamePattern.test(usernameVal)) {
//使用前端给的span展示错误信息
$("span[class='errorMsg']").text("用户名格式不对,需要6到10个字符");
return false; //不提交
}
//2.编写正则表达式验证密码
//获取密码
var passwordVal = $("#password").val();
//正则
var passwordPattern = /^\w{6,10}$/;
//验证
if (!passwordPattern.test(passwordVal)) {
//使用前端给的span展示错误信息(普通选择器)
$("span.errorMsg").text("密码格式不对,需要6到10个字符");
return false;
}
//3.两次密码需要相同
//获取第二次输入的密码
var repwdVal = $("#repwd").val();
//与第一次的密码进行比较
if (repwdVal != passwordVal) {
$("span.errorMsg").text("两次密码不相同!");
return false;
}
//4.验证邮箱
//得到邮箱
var emailVal = $("#email").val();
//正则
var emailPattern = /^[\w-]+@([a-zA-Z]+\.)+[a-zA-Z]+$/; //java中是两个斜杠,js是一个
//验证
if(!emailPattern.test(emailVal)) {
$("span.errorMsg").text("电子邮件格式不对!");
return false;
}
//暂时先算过关,先不提交
$("span.errorMsg").text("验证通过!");
return false;
})
})
</script>
3.结果
4.调试阶段
1.验证信息一闪而过
原因:
没有在js中return false,阻止表单提交
4.会员注册后端
1.需求分析
2.程序框架图
3.数据库表设计
会员表设计
-- 创建家具网购数据库表
CREATE DATABASE home_furnishing;
USE home_furnishing;
-- 创建会员表
CREATE TABLE `member` (
`id` INT PRIMARY KEY auto_increment,
`username` VARCHAR(32) NOT NULL UNIQUE,
`password` VARCHAR(32) NOT NULL,
`email` VARCHAR(64)
)
-- 测试数据
INSERT INTO member VALUES(NULL, 'admin', MD5('admin'), '1721469477@qq.com')
4.建立javabean与表的映射
Member.java
package com.sxs.furns.entity;
/**
* @author 孙显圣
* @version 1.0
*/
public class Member {
private Integer id;
private String username;
private String password;
private String email;
//无参构造方法!!!用于反射调用
public Member(){}
public Member(Integer id, String username, String password, String email) {
this.id = id;
this.username = username;
this.password = password;
this.email = email;
}
public Integer getId() {
return id;
}
public void setId(Integer 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;
}
}
5.引入德鲁伊连接池工具类
JDBCUtilsByDruid.java
package com.sxs.furns.utils;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
/**
* @author 孙显圣
* @version 1.0
*/
public class JDBCUtilsByDruid {
//静态数据源引用(jdbc的接口)
private static DataSource dataSource;
//静态代码块,在类加载时为数据源引用赋值
static {
//1.读取配置文件
Properties properties = new Properties();
try {
//使用类加载器读取配置文件,配置文件放在resources文件夹
//这句话的意思是,使用JDBCUtilsByDruid类读取在resources文件夹下的配置文件,druid.properties
properties.load(JDBCUtilsByDruid.class.getClassLoader().getResourceAsStream("druid.properties"));
} catch (IOException e) {
throw new RuntimeException(e);
}
//2.使用配置文件,创建德鲁伊数据源对象
try {
dataSource = DruidDataSourceFactory.createDataSource(properties);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
//编写getConnection方法
public static Connection getConnection() {
try {
return dataSource.getConnection();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
//把Connection对象放回连接池
public static void close(ResultSet resultSet, Statement statement, Connection connection) {
try {
if (resultSet != null) {
resultSet.close();
}
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
6.引入BasicDao
BasicDao.java
package com.sxs.furns.dao;
import com.sxs.furns.utils.JDBCUtilsByDruid;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
/**
* @author 孙显圣
* @version 1.0
* 开发BasicDAO , 是其他DAO的父类
*/
public class BasicDao<T> { //泛型指定具体类型
private QueryRunner qr = new QueryRunner();
//开发通用的dml方法, 针对任意的表
public int update(String sql, Object... parameters) {
Connection connection = null;
try {
connection = JDBCUtilsByDruid.getConnection();
int update = qr.update(connection, sql, parameters);
return update;
} catch (SQLException e) {
throw new RuntimeException(e); //将编译异常->运行异常 ,抛出
} finally {
JDBCUtilsByDruid.close(null, null, connection);
}
}
//返回多个对象(即查询的结果是多行), 针对任意表
/**
*
* @param sql sql 语句,可以有 ?
* @param clazz 传入一个类的Class对象 比如 Actor.class
* @param parameters 传入 ? 的具体的值,可以是多个
* @return 根据Actor.class 返回对应的 ArrayList 集合
*/
public List<T> queryMulti(String sql, Class<T> clazz, Object... parameters) {
Connection connection = null;
try {
connection = JDBCUtilsByDruid.getConnection();
return qr.query(connection, sql, new BeanListHandler<T>(clazz), parameters);
} catch (SQLException e) {
throw new RuntimeException(e); //将编译异常->运行异常 ,抛出
} finally {
JDBCUtilsByDruid.close(null, null, connection);
}
}
//查询单行结果 的通用方法
public T querySingle(String sql, Class<T> clazz, Object... parameters) {
Connection connection = null;
try {
connection = JDBCUtilsByDruid.getConnection();
return qr.query(connection, sql, new BeanHandler<T>(clazz), parameters);
} catch (SQLException e) {
throw new RuntimeException(e); //将编译异常->运行异常 ,抛出
} finally {
JDBCUtilsByDruid.close(null, null, connection);
}
}
//查询单行单列的方法,即返回单值的方法
public Object queryScalar(String sql, Object... parameters) {
Connection connection = null;
try {
connection = JDBCUtilsByDruid.getConnection();
return qr.query(connection, sql, new ScalarHandler(), parameters);
} catch (SQLException e) {
throw new RuntimeException(e); //将编译异常->运行异常 ,抛出
} finally {
JDBCUtilsByDruid.close(null, null, connection);
}
}
}
7.编写druid配置文件
druid.properties
#key=value
driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/home_furnishing?rewriteBatchedStatements=true
#url=jdbc:mysql://localhost:3306/girls
username=root
password=root
#initial connection Size
initialSize=10
#min idle connecton size
minIdle=5
#max active connection size
maxActive=50
#max wait time (5000 mil seconds)
maxWait=5000
编写测试类,测试连接
JDBCUtilsByDruidTest.java
package com.sxs.furns.test;
import com.sxs.furns.utils.JDBCUtilsByDruid;
import org.junit.Test;
import java.sql.Connection;
/**
* @author 孙显圣
* @version 1.0
*/
public class JDBCUtilsByDruidTest {
@Test
public void getConnection() {
//测试连接
Connection connection = JDBCUtilsByDruid.getConnection();
System.out.println(connection);
JDBCUtilsByDruid.close(null, null, connection);
}
}
8.编写MemberDao
1.MemberDao.java
package com.sxs.furns.dao;
import com.sxs.furns.entity.Member;
/**
* @author 孙显圣
* @version 1.0
*/
public interface MemberDao {
//1.通过用户名返回Member
public Member queryMemberByUsername(String username);
//2.保存Member对象到数据库的member表
public int saveMember(Member member);
}
2.MemberDaoImpl.java
package com.sxs.furns.dao.impl;
import com.sxs.furns.dao.BasicDao;
import com.sxs.furns.dao.MemberDao;
import com.sxs.furns.entity.Member;
/**
* @author 孙显圣
* @version 1.0
*/
public class MemberDaoImpl extends BasicDao<Member> implements MemberDao {
/**
* 通过用户名,返回对应的Member
* @param username 用户名
* @return 对应的Member对象,如果没有则返回null
*/
@Override
public Member queryMemberByUsername(String username) {
//查询单行数据
Member member = querySingle("select * from member where username = ?", Member.class, username);
return member;
}
/**
* 保存一个会员
* @param member 传入Member对象
* @return 返回-1就是失败,返回其他的数字就是受影响的行数
*/
@Override
public int saveMember(Member member) {
//获取member对象信息
String username = member.getUsername();
String password = member.getPassword();
String email = member.getEmail();
//插入到member表中
return update("insert into member values(?,?,md5(?),?)", null, username, password, email);
}
}
3.进行单元测试
MemberDaoTest.java
package com.sxs.furns.test;
import com.sxs.furns.dao.MemberDao;
import com.sxs.furns.dao.impl.MemberDaoImpl;
import com.sxs.furns.entity.Member;
import org.junit.Test;
/**
* @author 孙显圣
* @version 1.0
*/
public class MemberDaoTest {
private MemberDao memberDao = new MemberDaoImpl();
@Test
public void queryMemberByUsername() {
if (memberDao.queryMemberByUsername("admin") == null) {
System.out.println("该用户不存在");
} else {
System.out.println("该用户存在");
}
}
@Test
public void saveMember() {
Member member = new Member(null, "jack", "123", "123@qq.com");
if (memberDao.saveMember(member) == 1) {
System.out.println("添加成功");
} else {
System.out.println("添加失败");
}
}
}
测试结果:
9.编写MemberService
1.MemberService.java
package com.sxs.furns.service;
import com.sxs.furns.entity.Member;
/**
* @author 孙显圣
* @version 1.0
*/
public interface MemberService {
//注册用户
public boolean registerMember(Member member);
//判断用户名是否存在
public boolean isExistsUsername(String username);
}
2.MemberServiceImpl.java
package com.sxs.furns.service.impl;
import com.sxs.furns.dao.MemberDao;
import com.sxs.furns.dao.impl.MemberDaoImpl;
import com.sxs.furns.entity.Member;
import com.sxs.furns.service.MemberService;
/**
* @author 孙显圣
* @version 1.0
*/
public class MemberServiceImpl implements MemberService {
//定义一个MemberDao的属性
MemberDao memberDao = new MemberDaoImpl();
/**
* 将注册用户信息插入到数据库中
* @param member 注册用户信息
* @return 成功返回true,失败返回false
*/
@Override
public boolean registerMember(Member member) {
return memberDao.saveMember(member) == 1 ? true : false;
}
/**
* 判断用户是否存在
* @param username 用户名
* @return 如果存在,返回true,否则返回false
*/
@Override
public boolean isExistsUsername(String username) {
return memberDao.queryMemberByUsername(username) == null ? false : true;
}
}
3.进行单元测试
MemberServiceTest.java
package com.sxs.furns.test;
import com.sxs.furns.entity.Member;
import com.sxs.furns.service.MemberService;
import com.sxs.furns.service.impl.MemberServiceImpl;
import org.junit.Test;
/**
* @author 孙显圣
* @version 1.0
*/
public class MemberServiceTest {
private MemberService memberService = new MemberServiceImpl();
@Test
public void registerMember() {
Member member = new Member(null, "jack", "123", "123@qq.com");
if (!memberService.registerMember(member)) {
System.out.println("注册失败");
} else {
System.out.println("注册成功");
}
}
@Test
public void isExistsUsername() {
if (!memberService.isExistsUsername("jack")) {
System.out.println("用户不存在");
} else {
System.out.println("用户存在");
}
}
}
测试结果
10.login.html优化
1.设置base路径
<!-- base路径设置 -->
<base href="http://localhost:8080/jiaju_mail/">
2.修改所有…/…/为空
3.运行login.html
4.修改前端js验证通过逻辑
11.编写RegisterServlet
1.修改login.html
2.引入两个前端页面
3.修改两个页面的base路径
4.RegisterServlet.java
package com.sxs.furns.web;
import com.sxs.furns.entity.Member;
import com.sxs.furns.service.MemberService;
import com.sxs.furns.service.impl.MemberServiceImpl;
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;
/**
* @author 孙显圣
* @version 1.0
*/
@WebServlet(urlPatterns = "/registerServlet")
public class RegisterServlet extends HttpServlet {
//定义属性
private MemberService memberService = new MemberServiceImpl();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//接受用户注册信息
String username = req.getParameter("username");
String password = req.getParameter("password");
String email = req.getParameter("email");
//判断用户名是否在数据库中
if (!memberService.isExistsUsername(username)) { //不在数据库中,可以注册
Member member = new Member(null, username, password, email);
//注册
if (memberService.registerMember(member)) {
//请求转发
req.getRequestDispatcher("/views/member/register_ok.html").forward(req, resp);
} else {
//请求转发
req.getRequestDispatcher("/views/member/register_fail.html").forward(req, resp);
}
} else {
//返回注册页面
req.getRequestDispatcher("/views/member/login.html").forward(req, resp);
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}