目录
一.项目概述
二.项目源码
1.登录页面的实现(login.jsp)
表单信息提交到UserServlet类
2.注册页面(register.jsp)
注册信息提交到RegisterServlet类
商品首页(shouye.jsp)
3.商品列表(shoplist.jsp)
AddshowinfoServlet
通过传递过来的id查询商品表的数据,将查询出的商品封装到session中,请求转发到shopinfo.jsp页面
shopinfo.jsp
同样的方式通过传递商品id值去增加购物车功能
AddCartServlet
cart.jsp
自己业余时间瞎琢磨的,还有许多功能未实现,仅供参考
2.使用IDEA开发工具 + jsp + mybatis 框架 +mysql数据库 + javaScipt +jquery
3.数据库中的数据准备
二.项目源码
1.登录页面的实现(login.jsp)
登录页面
登录页面
未注册?点击注册
//1.为表单登录绑定提交事件
document.getElementById("regist").onsubmit = function() {
//2.获取填写的用户名和密码
let username = document.getElementById("username").value;
let password = document.getElementById("password").value;
//3.判断用户名是否符合规则
let reg1 = /^[a-zA-Z]{4,16}$/;
if(!reg1.test(username)) {
alert("用户名不符合规则,请输入4 ~ 16位纯字母!");
return false;
}
//4.判断密码是否符合规则
let reg2 = /^[d]{6}$/;
if(!reg2.test(password)) {
alert("密码不符合规则,请输入6位纯数字密码!");
return false;
}
//5.如果所有条件都不满足,则提交表单
return true;
}
表单信息提交到UserServlet类
package com.hbsi.controller;
import com.hbsi.bean.User;
import com.hbsi.service.UserService;
import com.hbsi.service.impl.UserServiceImpl;
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;
import java.util.List;
@WebServlet("/UserServlet")
public class UserServlet extends HttpServlet {
UserService service=new UserServiceImpl();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("UTF-8");
resp.setContentType("text/html;charset=UTF-8");
String username = req.getParameter("username");
String password = req.getParameter("password");
User user=new User(username,password);
User user1= service.selectUsermassage(user);
//判断用户名
if(username==null || "".equals(username)) {
//2.1用户名为空,重定向到登录页面
resp.sendRedirect("/cookie/login.jsp");
return;
}
//2.2 用户名不为空,将用户名存入到绘话域
if(user1 != null) {
req.getSession().setAttribute("user1", user1);
resp.sendRedirect("/cookie/usermassage.jsp");
}else {
req.setAttribute("msg","该用户名未注册或密码错误!!!");//封装请求属性
req.getRequestDispatcher("login.jsp").forward(req,resp);
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
2.注册页面(register.jsp)
注册页面
注册新用户
注册信息提交到RegisterServlet类
package com.hbsi.controller;
import com.hbsi.bean.RegisterFormBean;
import com.hbsi.bean.User;
import com.hbsi.service.UserService;
import com.hbsi.service.impl.UserServiceImpl;
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;
@WebServlet("/registservlet")
public class RegistServlet extends HttpServlet {
UserService service=new UserServiceImpl();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter("username");
String password = req.getParameter("password");
String name = req.getParameter("name");
String email = req.getParameter("email");
String phone = req.getParameter("phone");
String address = req.getParameter("address");
RegisterFormBean formBean = new RegisterFormBean();
formBean.setUsername(username);
formBean.setPassword(password);
formBean.setName(name);
formBean.setEmail(email);
formBean.setPhone(phone);
formBean.setAddress(address);
if(!formBean.validate()){
req.setAttribute("formBean",formBean);
//请求转发到register.jsp
req.getRequestDispatcher("/regist.jsp").forward(req,resp);
return;
}
User user=new User(username,password,name,email,phone,address);
Integer insert = service.insert(user);
if(insert == 1){
resp.getWriter().println("注册成功
");
resp.getWriter().write("3秒后自动跳转到登录页面...");
resp.setHeader("Refresh","3;URL=/cookie/login.jsp");
}else {
resp.getWriter().println("注册失败
");
resp.getWriter().write("3秒后自动回到注册页面...");
resp.setHeader("Refresh","3;URL=/cookie/regist.jsp");
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
商品首页(shouye.jsp)
首页
首 页
商品分类
热 销
个人中心
注 册
登 录
3.商品列表(shoplist.jsp)
商品首页
goodsList = goodsService.selectGoods();
request.setAttribute("goodsList",goodsList);
%>
【】
¥
function showinfo(id) {
window.location.href = "AddshowinfoServlet?id=" + id;
}
给buttont添加点击事件,匿名函数中将商品id作为参数传递给AddshowinfoServlet
AddshowinfoServlet
package com.hbsi.controller;
import com.hbsi.bean.Goods;
import com.hbsi.service.GoodsService;
import com.hbsi.service.impl.GoodsServiceImpl;
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;
import java.util.List;
@WebServlet("/AddshowinfoServlet")
public class AddshowinfoServlet extends HttpServlet {
GoodsService goodsService=new GoodsServiceImpl();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String id = req.getParameter("id");
List goodsinfo = goodsService.selectgoodsById(Integer.valueOf(id));
for (Goods goods : goodsinfo) {
req.setAttribute("goods",goods);
}
req.getRequestDispatcher("/shopinfo.jsp").forward(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
通过传递过来的id查询商品表的数据,将查询出的商品封装到session中,请求转发到shopinfo.jsp页面
shopinfo.jsp
Title
$("html,body", window.parent.document).animate({ scrollTop: 0 }, 1000);
商品详情
蛋糕编号:${goods.id}
蛋糕名称:${goods.name}
蛋糕类型:${goods.getType().getName()}
蛋糕介绍:${goods.intro}
蛋糕价格:¥${goods.price}
库存: ${goods.stock}
function addCart(id){
window.location.href = "AddCartServlet?id=" + id;
}
同样的方式通过传递商品id值去增加购物车功能
AddCartServlet
package com.hbsi.controller;
import com.hbsi.bean.Goods;
import com.hbsi.bean.Order;
import com.hbsi.bean.OrderItem;
import com.hbsi.service.GoodsService;
import com.hbsi.service.impl.GoodsServiceImpl;
import com.hbsi.util.PricesUtils;
import javax.servlet.ServletContext;
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 javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@WebServlet("/AddCartServlet")
public class AddCartServlet extends HttpServlet {
GoodsService goodsService=new GoodsServiceImpl();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String id = req.getParameter("id");
List goodsList = goodsService.selectgoodsById(Integer.valueOf(id));
HttpSession session = req.getSession();
//判断session是否存在购物车
Map cart = (Map) session.getAttribute("cart");
if(cart == null){
cart = new HashMap();
}
//考虑购物车是否存在该商品,
for (Goods goods : goodsList) {
OrderItem orderItem = cart.get(goods.getId());
if(orderItem == null){
orderItem = new OrderItem();
//若不存在,将该商品添加进购物车
orderItem.setGoods(goods);
//设初始数量为1
orderItem.setAmount(1);
orderItem.setPrice(goods.getPrice());
}else{
//若存在,则数量+1
orderItem.setAmount(orderItem.getAmount()+1);
orderItem.setPrice(orderItem.getPrice()+goods.getPrice());
}
cart.put(goods.getId(),orderItem);
session.setAttribute("cart",cart);
}
req.getRequestDispatcher("/cart.jsp").forward(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
cart.jsp
Title
--%>
${ item.key}--%>
${ item.value}--%>
--%>
我的购物车
map = (Map) request.getSession().getAttribute("cart");
if(map == null){
return;
}
double total=0;
for(Map.Entry entry : map.entrySet()) {
total=total+entry.getValue().getPrice();
%>
蛋糕名称 :
蛋糕价格 :¥
-
小计 :
删除订单