关于图片失效的问题,后面题主尽量重新填坑,因为我也忘了那些地方都是些什么图
第一阶段:表单验证
1、新建一个模块
2、把书城的静态资源拷贝到bookStore工程下
3、登录注册页面表单验证
在用户登录注册页面对输入数据进行一些规则限制
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>尚硅谷会员注册页面</title>
<link href="../../static/css/style.css" type="text/css" rel="stylesheet">
<script type="text/javascript" src="../../static/script/jquery-1.7.2.js"></script>
<script type="text/javascript">
// 页面加载完成之后
$(function () {
//给注册绑定单击事件
$("#sub_btn").click(function () {
//验证用户名:必须由字母数字下划线组成,并且长度为5到12位
var username = $("#username").val();
var patt = /^[0-9a-zA-Z_-]{5,12}$/;
if(!patt.test(username)){
$("span.errorMsg").text("用户名不合法!");
return false;
}
//验证密码:必须由字母数字下划线组成,并且长度为5到12位
var passtext = $("#password").val();
var patt = /^\w{5,12}$/;
if(!patt.test(passtext)){
$("span.errorMsg").text("请重新输入密码!");
return false;
}
//验证确认密码:和密码相同
var pwd = $("#repwd").val();
if(passtext != pwd){
$("span.errorMsg").text("两次密码输入不一致!");
return false;
}
//邮箱验证:xxxxx@xxx.com
var emitext = $("#email").val();
var emailpatt = /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/
if(!emailpatt.test(emitext)){
$("span.errorMsg").text("您的邮箱格式有误!");
return false;
}
//验证码:现在只需要验证用户已输入。
var codetext = $("#code").val();
codetext = $.trim(codetext);
if(codetext == null || codetext == ""){
$("span.errorMsg").text("请输入验证码!");
return false;
}
$("span.errorMsg").text();
return false;
});
});
</script>
<style type="text/css">
.login_form{
height:420px;
margin-top: 25px;
}
</style>
</head>
<body>
<div id="login_header">
<img class="logo_img" alt="" src="../../static/img/logo.gif" >
</div>
<div class="login_banner">
<div id="l_content">
<span class="login_word">欢迎注册</span>
</div>
<div id="content">
<div class="login_form">
<div class="login_box">
<div class="tit">
<h1>注册尚硅谷会员</h1>
<span class="errorMsg"></span>
</div>
<div class="form">
<form action="userServlet" method="post">
<input type="hidden" name="action" value="regist">
<label>用户名称:</label>
<input class="itxt" type="text" placeholder="请输入用户名"
autocomplete="off" tabindex="1" name="username" id="username" />
<br />
<br />
<label>用户密码:</label>
<input class="itxt" type="password" placeholder="请输入密码"
autocomplete="off" tabindex="1" name="password" id="password" />
<br />
<br />
<label>确认密码:</label>
<input class="itxt" type="password" placeholder="确认密码"
autocomplete="off" tabindex="1" name="repwd" id="repwd" />
<br />
<br />
<label>电子邮件:</label>
<input class="itxt" type="text" placeholder="请输入邮箱地址"
autocomplete="off" tabindex="1" name="email" id="email" />
<br />
<br />
<label>验证码:</label>
<input class="itxt" type="text" name="code" style="width: 150px;" id="code" />
<img alt="" src="../../static/img/code.bmp" style="float: right; margin-right: 40px">
<br />
<br />
<input type="submit" value="注册" id="sub_btn" />
</form>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
第二阶段:用户登录注册
1、JavaEE三层架构
- Web层/视图展现层
- 获取请求参数,封装成Bean对象
- 调用Service业务层处理业务
- 响应数据给客户端请求转发、重定向
- Servlet程序,Webwork,Strtus1.x,Strtus2.x,SpringMVC
- Service业务层
- 处理业务逻辑
- 调用持久层保存到数据库
- Spring框架
- Dao持久层
- Dao持久层,只负责跟数据库交互
- 执行CRUD操作
- Create 添加
- Read 读/查
- Update 修改
- Delete 删除
- JDBC,DbUtils,JdbcTemplate,Mybatis,Hiberante,JPA
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-tTETvfQX-1647832234568)(file:///C:/Users/ZHANGY~1/AppData/Local/Temp/企业微信截图_1638522597892.png)]
分层的目的是为了降低代码耦合度,方便日后项目的维护和升级
搭建书城项目开发环境:
web层 com.company.web/servlet/controller
service层 com.company.service Service接口包
com.company.service.impl Service接口实现类
dao层 com.company.dao Dao接口包
com.company.dao.impl Dao接口实现类
实体类对象 com.company.bean/pojo/entity/domain JavaBean类
测试包 com.company.test/junit
工具类 com.company.utils
2、创建书城需要的数据库和表
drop database if exists book;
create database book;
use book;
create table t_user(
`id` int primary key auto_increment,
`username` varchar(20) not null unique,
`password` varchar(32) not null ,
`email` varchar(200)
);
insert into t_user(`username`,`password`,`email`) values('admin','admin','admin@company');
3、编写数据库对应的JavaBean对象
package com.yitong.bean;
/**
* ClassName: User
* Description:
* date: 2021/12/6 9:56
*
* @author zhangyingying
* @since JDK 1.8
*/
public class User {
private int id;
private String username;
private String password;
private String email;
public User() {
}
public User(int id, String username, String password, String email) {
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 + '\'' +
'}';
}
}
4、编写工具类JdbcUtils
package com.yitong.utils;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
/**
* ClassName: JdbcUtils
* Description:
* date: 2021/12/6 10:01
*
* @author zhangyingying
* @since JDK 1.8
*/
public class JdbcUtils {
//使用德鲁伊提供的数据库连接池获取连接
private static DruidDataSource dataSource;
//加载创建数据库连接池
static {
try {
//新建配置文件对象
Properties properties = new Properties();
//读取jdbc.properties配置文件
InputStream inputStream = JdbcUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");
//从流中加载数据到Properties对象中
properties.load(inputStream);
//创建数据库连接池(使用德鲁伊提供的数据库连接池工厂,创建数据库连接池)
dataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(properties);
} catch (Exception e) {
e.printStackTrace();
}
}
/*获取数据库连接池中的连接*/
public static Connection getConnection(){
Connection conn = null;
try {
//通过数据库连接池,获取数据库连接
conn = dataSource.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
//返回连接
return conn;
}
/*关闭连接,放回数据库连接池*/
public static void close(Connection conn){
if (conn != null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
5、编写Base Dao类
package com.yitong.dao;
import com.yitong.utils.JdbcUtils;
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;
/**
* ClassName: BaseDao
* Description:
* date: 2021/12/6 14:55
*
* @author zhangyingying
* @since JDK 1.8
*/
//抽象类
public abstract class BaseDao {
//使用apache提供的DbUtils操作数据库
private QueryRunner queryRunner = new QueryRunner();
/**
* @Description: update()方法用来执行:Insert/update/Delete语句
* @author: zhangyingying
* @date: 2021/12/6 15:10
* @return :如果返回-1,说明执行失败,返回其他表示影响的行数
*/
//这里没有考虑事务问题
public int update(String sql,Object ... args){
//连接数据库
Connection connection = JdbcUtils.getConnection();
try {
//使用dbutils提供的通用更新方法更新
int update = queryRunner.update(connection, sql, args);
return update;
} catch (SQLException e) {
e.printStackTrace();
}finally {
//关闭连接
JdbcUtils.close(connection);
}
return -1;
}
/**
* @Description:查询一条数据返回值,BeanHandler
* @author: zhangyingying
* @date: 2021/12/6 16:18
* @param: * @Param: type
* @Param: sql
* @Param: args
* @return:T
*/
public <T> T queryForOne(Class<T> type,String sql,Object ... args){
Connection connection = JdbcUtils.getConnection();
try {
T query = queryRunner.query(connection, sql, new BeanHandler<T>(type), args);
return query;
} catch (SQLException e) {
e.printStackTrace();
}finally{
JdbcUtils.close(connection);
}
return null;
}
/**
* @Description:查询多条数据返回值,BeanListHandler
* @author: zhangyingying
* @date: 2021/12/6 16:18
* @param: * @Param: type
* @Param: sql
* @Param: args
* @return:java.util.List<T>
*/
//Class<T> type,用于接收对象.class类型。因为不确定传过来的对象类型,因此用泛型T来表示
public <T> List<T> queryForList(Class<T> type,String sql,Object ... args){
Connection connection = JdbcUtils.getConnection();
try {
//new BeanListHandler<T>(type)通过反射机制获取对象类型
List<T> query = queryRunner.query(connection, sql, new BeanListHandler