商城首页(4)

四.页面的详细信息显示
当点入图片代表访问该商品
此时要访问一个servlet资源,转发到详细信息页面
访问资源时携带该商品的pid确定此商品,到数据库查询商品.放入request域中
在页面中获取此商品对象的相应值,放入相应的位置
web

public class ProductInfoServlet extends HttpServlet {

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

service

public class ProductInfoService {

	public Product getProductInfo(String pid) {
		ProductInfoDao dao=new ProductInfoDao();
		Product pro=null;
		try {
			pro=dao.getProductInfo(pid);
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		return pro;
	}

}

dao

public class ProductInfoDao {

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

}

此时加入一个

<a href="${pageContext.request.contextPath }/categoryProduct?cid=${cid}&currentPage=${currentPage}">返回商品列表</a>

点击此链接回到原先列表,访问servlet,还要带着参数cid,currentPage,所以要把cid与currentPage一层层传下来,这时才可访问到之前的页面
五.浏览记录
技术:cookie
当点入图片访问详情时,就是浏览该商品,(把点进去的这件商品加入到浏览记录中)
在之前写的为商品的详细信息准备数据的资源中拼接cookie串,把浏览过的商品的pid拼接到一起,
首先要先获取所有的cookie,(判断获得的Cookie数组是否为null)
得到一个Cookie[]数组遍历每个cookie找到name为pids的cookie串,对其进行分割,得到String[],把数组转化成一个LinkedList集合,更方便对其进行串的拼接

List list=Arrays.asList(str);
LinkedList link= new LinkedList(list);

判断该集合中是否含有此pid如有则remove掉原先的再把它addFirst添加到头部
如无此pid则直接addFirst,遍历集合用StringBuffer进行串的拼接,这是cookie串
如果无pids的cookie,说明之前的浏览记录为空,
拼完cookie串后就是创造cookie对象,
Cookie cookie=new Cookie(“name”,“value”);
response.addCookie(cookie);
此时客户端会带着这个cookie串去访问资源,
当再次回到列表页面时,获取cookie,得到所有的pid,用jstl显示所有浏览记录;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值