session实现简单的购物车

一、实现的功能

  • (1) 利用session实现购物车中的物品添加。
  • (2)使用servlet实现添加物品的功能(交互层)。
  • (3)一共有三个界面。第一个用来显示物品的列表的页面,第二个用来显示添物品的界面,第三个用来显示添加到购物车的信息页面。

二、代码实现

  • (1)物品列表页面:productlist.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
	<a href="<%=request.getContextPath()%>/shopping.pdo?pname='LENOVO R7'">联想拯救者R7</a>
	<br><br>
	<a href="<%=request.getContextPath()%>/shopping.pdo?pname='LENOVO R8'">联想拯救者R8</a>
	<br><br>
	<a href="<%=request.getContextPath()%>/shopping.pdo?pname='LENOVO R9'">联想拯救者R9</a>
	<br><br>
	<a href="<%=request.getContextPath()%>/shopping.pdo?pname='LENOVO R10'">联想拯救者R10</a>
	<br><br>
</body>
</html>
  • (2)添加购物车页面:producttails.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
		<%
			String pname = (String)request.getAttribute("p");
			out.println(pname);
		%>
		<br><br>
		拿到其他的......该产品的详细参数
		
		<br><br>
		<a style="display: block;width: 100px ; height: 35px ;line-height :35px; text-decoration : none;background: red; color:#fff ;text-align: center;" href="<%=request.getContextPath() %>/addcart.pdo?pname=<%=pname %>" >加入购物车</a>
</body>
</html>
  • (3)显示添加成功后的信息页面:shoppingcart.jsp
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
		<%
			List<String> products = (List<String>)session.getAttribute("car");
			for(String s: products){
				out.print(s+"<br><br>");
			}
		%>
</body>
</html>
  • (4)交互层:ShopController.java
package com.controller;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

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;

/**
 * Servlet implementation class ShopController
 */
@WebServlet(urlPatterns = {"*.pdo"})
public class ShopController extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ShopController() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		//设置字符集
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("text/html; charset=utf-8");
		
		//在这个方法里边处理所有的增删改查的功能
		String mn = request.getServletPath();
		mn = mn.substring(1);
		mn = mn.substring(0 , mn.length()-4);
		
		//利用反射
		try {
			//获取方法名,并且根据具体的去调用下边的方法
			Method method = this.getClass().getDeclaredMethod(mn,HttpServletRequest.class , HttpServletResponse.class);
			method.invoke(this,request,response);
			
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
		
		

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	
	private void shopping (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//设置字符集
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("text/html; charset=utf-8");
		String pname = request.getParameter("pname");
		request.setAttribute("p", pname);
		request.getRequestDispatcher("/producttails.jsp").forward(request, response);;
	}
	
	private void addcart (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//设置字符集
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("text/html; charset=utf-8");
		
		String pname = request.getParameter("pname");
		//添加购物车
		HttpSession session = request.getSession(true);
		
		List<String> products = (List<String>)session.getAttribute("car");
		if(products == null) {
			products = new ArrayList<>();
		}
		//把添加的物品放入List集合
		products.add(pname);
		//放入session中表示添加成功
		session.setAttribute("car", products);
		
		//response.getWriter().print("添加成功!");
		response.sendRedirect(request.getContextPath() + "/shoppingcart.jsp");
	}
}

  • 1
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值