javaWeb项目2_网上商城_day2

一:最新和热门商品

public class IndexServlet extends HttpServlet {
	
	//查询最热和最新商品
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ProductService service=new ProductService();
		List<Product> hotProductList=service.findHotProductList();
		List<Product> newProductList=service.findNewProductList();
		
		request.setAttribute("hotProductList", hotProductList);
		request.setAttribute("newProductList", newProductList);
		request.getRequestDispatcher("/index.jsp").forward(request, response);
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}


public class ProductService {
	public List<Product> findHotProductList() {
		ProductDao dao=new ProductDao();
		 List<Product> hotProductList=null;
		try {
			 hotProductList=dao.findHotProductList();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return hotProductList;
	}

	public List<Product> findNewProductList() {
		ProductDao dao=new ProductDao();
		 List<Product> newProductList=null;
		try {
			newProductList=dao.findNewProductList();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return newProductList;
	}

}


public class ProductDao {
	public List<Product> findHotProductList() throws SQLException {
		QueryRunner runner=new QueryRunner(DataSourceUtils.getDataSource());
		String sql="select * from product where is_hot=? limit ?,?";
		return runner.query(sql, new BeanListHandler<Product>(Product.class), 1,0,9);
	}
	public List<Product> findNewProductList() throws SQLException {
		QueryRunner runner=new QueryRunner(DataSourceUtils.getDataSource());
		String sql="select * from product order by pdate desc limit ?,?";
		return runner.query(sql, new BeanListHandler<Product>(Product.class),0,9);
	}

}

	
<c:forEach var="newPro" items="${newProductList}">
    <div class="col-md-2" style="text-align:center;height:200px;padding:10px 0px;">
        <a href="product_info.htm">
        <img src="${pageContext.request.contextPath }/${newPro.pimage}" width="130"         height="130" style="display: inline-block;"></a>
      <p><a href="product_info.html" style='color:#666'>${newPro.pname }</a></p>
      <p><font color="#E4393C" style="font-size:16px">&yen;${newPro.shop_price }</font></p>
    </div>
</c:forEach>

二:怎么默认打开就是加载好的数据

到web.xml里面删除,只留下default.jsp,然后创建一个default.jsp

	<%
		response.sendRedirect(request.getContextPath()+"/indexServlet");
	%>

三:页面使用ajax显示类别菜单,注意这是表头,所有的页面都需要

<script type="text/javascript">
	$(function(){
		var content="";
		$.post(
			"${pageContext.request.contextPath}/categoryListServlet",
				function(data){
				//[{"cid":"xxx","cname":"xxxx"},{},{}]
				//动态创建<li><a href="#">${category.cname }</a></li>
					for(var i=0;i<data.length;i++){
						content+="<li><a href='#'>"+data[i].cname+"</a></li>";
					}
					//将拼接好的li放置到ul中
					$("#categoryUl").html(content);
				},
			"json"
			
		);
	});
</script>


public class CategoryListServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		CategoryService service=new CategoryService();
		List<Category> categoryList=service.findCategoryList();
		Gson gson=new Gson();
		String json=gson.toJson(categoryList);
		response.setContentType("text/html;charset=utf-8");
		response.getWriter().write(json);
		
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}


public class CategoryService {

	public List<Category> findCategoryList() {
		CategoryDao dao=new CategoryDao();
		List<Category> categoryList=null;
		try {
			categoryList = dao.findCategoryList();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return categoryList;
	}

}

public class CategoryDao {
	public List<Category> findCategoryList() throws SQLException {
		QueryRunner runner=new QueryRunner(DataSourceUtils.getDataSource());
		String sql="select * from category";
		return runner.query(sql, new BeanListHandler<Category>(Category.class));
	}

}

四:页面使用redis缓存显示类别菜单

将改的不频繁的东西放在缓存中,

先从缓存中读取,若有,直接使用,没有从数据库查询,然后将查到的数据放到缓存中

public class CategoryListServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		CategoryService service=new CategoryService();
		//先从缓存中查询categoryList 如果有直接使用 没有在从数据库中查询 存到缓存中
		//1、获得jedis对象 连接redis数据库
		Jedis jedis = JedisPoolUtils.getJedis();
		String categoryListJson =jedis.get("categoryListJson");
		//2、判断categoryListJson是否为空
		if(categoryListJson==null){
			System.out.println("缓存没有数据 查询数据库");
			//准备分类数据
			List<Category> categoryList =service.findCategoryList();
			Gson gson=new Gson();
			categoryListJson=gson.toJson(categoryList);
			jedis.set("categoryListJson", categoryListJson);
		}
		response.setContentType("text/html;charset=utf-8");
		response.getWriter().write(categoryListJson);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}

五:分页显示某种类别的商品列表

点击对应的跳转到对应的商品分页

//跳转到对应的分页商品
<script type="text/javascript">
	$(function(){
		var content="";
		$.post(
			"${pageContext.request.contextPath}/categoryListServlet",
				function(data){
				//[{"cid":"xxx","cname":"xxxx"},{},{}]
				//动态创建<li><a href="#">${category.cname }</a></li>
					for(var i=0;i<data.length;i++){
						content+="<li><a href='${pageContext.request.contextPath}/productListByCid?cid="+data[i].cid+"'>"+data[i].cname+"</a></li>";
					}
					//将拼接好的li放置到ul中
					$("#categoryUl").html(content);
				},
			"json"
			
		);
	});
</script>


public class PageBean<T> {
	private int currentPage;
	private int currentCount;
	private int totalPage;
	private int totalCount;
	private List<T> list;

}

public class ProductListByCid extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ProductService service=new ProductService();
		//获取商品分类的cid
		String cid=request.getParameter("cid");
		
		//将商品的当前页和当前页显示的条数传过来
		String currentPageStr=request.getParameter("currentPage");
		if(currentPageStr==null){
			currentPageStr="1";
		}
		int currentPage=Integer.parseInt(currentPageStr);
		int currentCount=12;
		PageBean pageBean=service.findProductListByCid(cid,currentPage,currentCount);
		
		request.setAttribute("pageBean", pageBean);
		request.setAttribute("cid", cid); //是为了方便页面用分类id
		request.getRequestDispatcher("/product_list.jsp").forward(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}


public class ProductService {
	//分页显示数据
	public PageBean findProductListByCid(String cid, int currentPage, int currentCount) {
		ProductDao dao=new ProductDao();
		//封装一个PageBean 返回web层
		PageBean<Product> pageBean=new PageBean();
		//1、封装当前页
		pageBean.setCurrentPage(currentPage);
		//2、封装每页显示的条数
		pageBean.setCurrentCount(currentCount);
		//3、封装总条数
		int totalCount=0;
		try {
			totalCount = dao.findTotalCount(cid);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		pageBean.setTotalCount(totalCount);
		//4、封装总页数
		int totalPage=(int) Math.ceil(1.0*totalCount/currentCount);
		pageBean.setTotalPage(totalPage);
		//5、当前页显示的数据
		// select * from product where cid=? limit ?,?
		int index=(currentPage-1)*currentCount;
		List<Product> list=null;
		try {
			list = dao.findProductListByPage(cid,index,currentCount);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		pageBean.setList(list);
		return pageBean;
	}

}

public class ProductDao {
	public int findTotalCount(String cid) throws SQLException {
		QueryRunner runner=new QueryRunner(DataSourceUtils.getDataSource());
		String sql="select count(*) from product where cid=?";
		Long query =(Long) runner.query(sql, new ScalarHandler(),cid);
		return query.intValue();
	}

	public List<Product> findProductListByPage(String cid, int index, int currentCount) throws SQLException {
		QueryRunner runner=new QueryRunner(DataSourceUtils.getDataSource());
		String sql="select * from product where cid=? limit ?,?";
		List<Product> list=runner.query(sql, new BeanListHandler<Product>(Product.class), cid,index,currentCount);
		return list;
	}
}


//页面分页的显示
<!-- 上一页 -->
<c:if test="${pageBean.currentPage==1 }">
		<li class="disabled"><a href="javascript:void(0)" aria-label="Previous"><span
		aria-hidden="true">&laquo;</span></a></li>
</c:if>

<c:if test="${pageBean.currentPage!=1 }">
		<li><a href="${pageContext.request.contextPath}/productListByCid?cid=${cid}&currentPage=${pageBean.currentPage-1}" aria-label="Previous"><span
		aria-hidden="true">&laquo;</span></a></li>
</c:if>

<!-- 显示每一页 -->
<c:forEach var="page" begin="1" end="${pageBean.totalPage }">
	<!-- 判断是否是当前页 -->
	<c:if test="${page==pageBean.currentPage }">
		<li class="active"><a href="javascript:void(0);">${page}</a></li>
	</c:if>
	<c:if test="${page!=pageBean.currentPage}">                                       
		 <li ><a href="${pageContext.request.contextPath}/productListByCid?cid=${cid}&currentPage=${page}">${page}</a></li> 
	</c:if>
</c:forEach>


<!-- 下一页 -->
<c:if test="${pageBean.currentPage==pageBean.totalPage }">
		<li class="disabled">
			<a href="javascript:void(0)" aria-label="Next"> <span aria-hidden="true">&raquo;</span>
			</a>
		</li>
</c:if>


<c:if test="${pageBean.currentPage!=pageBean.totalPage }">
		<li>
			<a href="${pageContext.request.contextPath}/productListByCid?cid=${cid}&currentPage=${pageBean.currentPage+1}" aria-label="Next"> 
				<span aria-hidden="true">&raquo;</span>
			</a>
		</li>
</c:if>

六:显示某种商品的详细信息

<c:forEach var="pro" items="${pageBean.list}">
	<div class="col-md-2" style="height:250px">
		<a href="${pageContext.request.contextPath }/productInfoServlet?pid=${pro.pid}"> <img src="${pageContext.request.contextPath }/${pro.pimage}"
			width="170" height="170" style="display: inline-block;">
		</a>
		<p>
			<a href="${pageContext.request.contextPath }/productInfoServlet?pid=${pro.pid}" style='color: green'>${pro.pname }</a>
		</p>
		<p>
			<font color="#FF0000">商城价:&yen;${pro.shop_price }</font>
		</p>
	</div>
</c:forEach>


public class ProductInfoServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String pid=request.getParameter("pid");
		ProductService service=new ProductService();
		Product product=service.findProductByPid(pid);
		request.setAttribute("product", product);
		request.getRequestDispatcher("/product_info.jsp").forward(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}


public class ProductService {
    public Product findProductByPid(String pid) {
		ProductDao dao=new ProductDao();
		Product product=null;
		try {
			product = dao.findProductByPid(pid);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return product;
	}

}

public class ProductDao {
    public Product findProductByPid(String pid) throws SQLException {
		QueryRunner runner=new QueryRunner(DataSourceUtils.getDataSource());
		String sql="select * from product where pid=?";
		return runner.query(sql, new BeanHandler<Product>(Product.class), pid);
	}

}

七:通过某种商品的详细信息,点击返回列表页面,回到原来的页面(浏览历史记录做准备)

八:商品的历史浏览记录

当点击商品的详情时,返回后就会有商品的pid,此时就可以有历史记录   限制商品历史记录的个数

先在productInfoServlet中将数据存储到cookie中,然后在productListServlet里面将cookie中的数据取出来

 

 

 

 

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

guoyebing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值