【黑马程序员】JavaEE就业班同步笔记第一阶段:JavaWeb之商城案例Part01
1 网上商城的实战的案例:
1.1 需求:
1.2 数据库设计:
1.3 网上商城用户模块的代码实现:(前台)
1.3.1 编写一个通用的Servlet:
传统方式的开发一个请求对应一个Servlet:这样的话会导致一个模块的Servlet 过多,导致整个项目的Servlet都会很多.能不能做一个处理?让一个模块致用一个Servlet处理请求.
商品查询所有:
http://localhost:8080/store_2.0/ProductServlet?method=findById 传统:
[AppleScript] 纯文本查看复制代码
?
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15public class UserServlet extends HttpServlet{
public void service(HttpServletRequest
req,HttpServletResponse resp){
// 接收参数:
String methodName =
request.getParameter(“method”);
if(“regist”.equals(m ethodName)){
regist(req,resp);
}else if(“login”.equals(methodName)){
login(req,resp);
}
}
public void regist(HttpServletRequest req,HttpServletResponse resp){
}
public void login(HttpServletRequest req,HttpServletResponse resp){
}
}
[AppleScript] 纯文本查看复制代码?
01 02 03 04 05 06 07 08 09 10 11 12 13public class ProductServlet extends HttpServlet{
public void service(HttpServletRequest
req,HttpServletResponse resp){
// 接收参数:
String methodName =
request.getParameter(“method”);
if(“findAll”.equals(methodName)){
findAll(req,resp);
}else if(“findById”.equals(methodName)){
findById(req,resp);
}
}
public void findAll(HttpServletRequest req,HttpServletResponse
14 15resp){
}
public void findById(HttpServletRequest req,HttpServletResponse resp){
}
}
反射:
[AppleScript] 纯文本查看复制代码?
1
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 1 0 1 1 1 2 1 3 1 4public class BaseServlet extends HttpServlet{
public void service(HttpServletRequest
req,HttpServletResponse resp){
//
http://localhost:8080/store_2.0/UserServlet?method=regist
//
http://localhost:8080/store_2.0/ProductServlet?method=findAll
String methodName =
request.getParameter(“method”);
// 获得Class:
Class clazz = this.getClass();
Method method =
clazz.getMethod(methodName,HttpServletRequest.class,HttpSerlvetResp onse.class);
String path =
(String)method.invoke(this,req,resp);
if(path != null){
req.getRequestDispatcher(path).forward( req,resp);
}
}
}
[AppleScript] 纯文本查看复制代码
?
1p ublic class UserServlet extends BaseServlet {
2 3 4 5 6 7 8
public String regist(HttpServletRequest req,HttpServletResponse resp){
return “/login.jsp”;
}
public String login(HttpServletRequest req,HttpServletResponse resp){
return “/index.jsp”;
}
}
[AppleScript] 纯文本查看复制代码?
1 2 3 4 5 6 7public class ProductServlet extends BaseServlet{ public String findAll(HttpServletRequest req,HttpServletResponse resp){
}
public String findById(HttpServletRequest req,HttpServletResponse resp){
}
}
1.3.2 搭建开发环境:
【步骤一】:引入开发jar包 * mysql 1 * c3p0 1 * dbutils 1 * beanutils 2 * jstl 2 * mail 1 * fileupload 2
【步骤二】:创建包结构:
【步骤三】:引入工具类和配置文件:【步骤四】:编写通用的Servlet: 1.3.3 用户模块的代码实现:
【创建数据库和表】:
[AppleScript] 纯文本查看复制代码
?
01 02 03 04 05 06 07 08 09 10 11 12 13CREATE TABLE `user` (
`uid` varchar(32) NOT NULL,
`username` varchar(20) DEFAULT NULL,
`password` varchar(20) DEFAULT NULL,
`name` varchar(20) DEFAULT NULL,
`email` varchar(30) DEFAULT NULL,
`telephone` varchar(20) DEFAULT NULL, `birthday` date DEFAULT NULL,
`sex` varchar(10) DEFAULT NULL,
`state` int(11) DEFAULT NULL,
`code` varchar(64) DEFAULT NULL,
PRIMARY KEY (`uid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
【引入页面】
【创建相关类】
【用户的注册功能】
在首页上点击【注册】链接:
跳转到注册页面:
在注册页面中输入相关的信息:
* 进行表单校验:JQuery实现校验.
* 异步用户名校验:
点击【注册】按钮:提交到Servlet:
在Servlet中调用业务层调用Dao完成保存操作,同时发送一封激活邮件: 【发送激活邮件】
邮件发送的相关的概念:
* 邮箱服务器 :如果一台电脑安装了邮箱服务器的软件,这台电脑称为是邮箱服务器.
* 电子邮箱 :其实就是邮箱服务器上的一块空间,通过电子邮箱账号访问这块空间的数据.
* 收发邮件的协议:
* 发邮件:SMTP协议:SMTP(Simple Mail Transfer Protocol)即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式。25默认端口号
* 收邮件:POP3协议:POP3,全名为“Post Office Protocol - Version 3”,即“邮局协议版本3”。是TCP/IP协议族中的一员。默认端口是110 * 收邮件:IMAP协议:IMAP(Internet Mail Access Protocol,Internet 邮件访问协议)以前称作交互邮件访问协议(Interactive Mail Access Protocol)。IMAP是斯坦福大学在1986年开发的一种邮件获取协议。
* 收发邮件的过程:
【搭建邮箱服务器】
?安装易邮邮箱服务器:
?配置易邮邮箱服务器:
* 修改域名:
* 注册账号:
?客户端收发邮件的软件: * OutLook :微软,收费的. * FoxMail :免费的.
?安装收邮件客户端软件:
【邮件发送的代码】
【激活用户】
在邮箱中点击【激活连接】
提交到Servlet:
* 根据激活码查询用户:
* 修改用户状态:
* 将激活码置为null: 页面跳转:
【用户登录】
在首页上点击【登录】链接. 跳转到登录页面:
在登录页面中输入信息:
点击【登录】按钮:
【用户退出】
在首页上点击【退出】链接
提交到Servlet:销毁session. 页面跳转:
2 前台分类模块的功能:
2.1 需求:
2.2 分析:
使用异步的方式完成分类的加载: 2.3 代码实现:
在首页上添加事件: [AppleScript] 纯文本查看复制代码
?
1 2 3$(function(){
异步加载分类的数据; });
创建分类的表: [AppleScript] 纯文本查看复制代码?
1 2 3 4 5CREATE TABLE `category` (
`cid` varchar(32) NOT NULL,
`cname` varchar(20) DEFAULT NULL,
PRIMARY KEY (`cid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
创建分类的相关的类:
相关代码的实现:异步加载分类,显示JSON数据:
每个页面上都由分类的信息,每次进行页面跳转的时候都需要去连接数据库进行查询:这样效率很低.需要优化当前程序!!!
* 将数据存入到缓存中,每次获取的时候从缓存中进行获取.
* EHCache :Hibernate框架二级缓存使用
* Memcached
* Redis
* 使用缓存的技术优化程序!!!
* 引入ehcache的包:
* 代码实现: [AppleScript] 纯文本查看复制代码
?
0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1@Override
public List
/*CategoryDao categoryDao = new CategoryDaoImpl();
return categoryDao.findAll();*/
/**
* 使用缓存优化程序,先从缓存中获取数据
* * 获取到:直接返回
* * 获取不到:查询数据库,将记录存入到缓存中.
*/
// 读取配置文件
CacheManager cacheManager =
CacheManager.create(CategoryServiceImpl.class.getClassLoader().getR esourceAsStream("ehcache.xml"));
// 从配置文件中获取名称为categoryCache缓存区
Cache cache = cacheManager.getCache("categoryCache");
// 判断缓存中是否有list集合:
Element element = cache.get("list");
List
if(element == null){
// 缓存中没有数据
System.out.println("缓存中没有数据 ,查询数据库=====");
CategoryDao categoryDao = new CategoryDaoImpl();
list = categoryDao.findAll();
element = new Element("list",list);
cache.put(element);
}else{
// 缓存中已经存在数据
System.out.println("缓存中有数据 ,没有查询数据库=====");
list =
(List
8 1 9 2 0 2 1 2 2 2 3 2 4 2 5 2 6 2 7 2 8 2 9 3 0 3 1 3 2
}
return list; }
3 前台商品信息的显示:
3.1 首页上最新商品和热门商品的显示:
创建商品表
[AppleScript] 纯文本查看复制代码
?
01 02 03 04 05 06 07CREATE TABLE `product` (
`pid` varchar(32) NOT NULL,
`pname` varchar(50) DEFAULT NULL,
`market_price` double DEFAULT NULL,
`shop_price` double DEFAULT NULL,
`pimage` varchar(200) DEFAULT NULL,
`pdate` date DEFAULT NULL,
08 09 10 11 12 13 14 15
`is_hot` int(11) DEFAULT NULL,
`pdesc` varchar(255) DEFAULT NULL,
`pflag` int(11) DEFAULT NULL,
`cid` varchar(32) DEFAULT NULL,
PRIMARY KEY (`pid`),
KEY `sfk_0001` (`cid`),
CONSTRAINT `sfk_0001` FOREIGN KEY (`cid`) REFERENCES `category` (`cid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
创建相关的类:
在IndexServlet调用业务层完成查询即可: 3.2 显示某个分类的商品:
在首页上点击某个分类:
提交到Servlet:传递分类的ID,当前页数. 3.3 显示某个商品详情:
在商品列表页面上点击【某个商品】
提交到Servlet:传递一个pid.
3.4 登录的验证码及自动登录及记住用户: 验证码---session、jq动态绑定
自动登录—--cookie+filter
记住用户名---cookie