session的一小案例,模仿网购的购物车增加商品

productlist.jsp页面的实现

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <br />
    <br />
    <a href="<%=request.getContextPath()%>/shopping.pdo?shoppingname='联想(lenovo)拯救者R7'">联想(lenovo)拯救者R7</a>
    <br />
    <br />
    <a href="<%=request.getContextPath()%>/shopping.pdo?shoppingname='联想(lenovo)拯救者R8'">联想(lenovo)拯救者R8</a>
    <br />
    <br />
    <a href="<%=request.getContextPath()%>/shopping.pdo?shoppingname='联想(lenovo)拯救者R9'">联想(lenovo)拯救者R9</a>
    <br />
    <br />
    <a href="<%=request.getContextPath()%>/shopping.pdo?shoppingname='联想(lenovo)拯救者R10'">联想(lenovo)拯救者R10</a>
</body>
</html>

productdetails.jsp页面的实现

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <% 
    String pname =(String)request.getAttribute("p");
    out.println(pname);
    
    %>
    <br />
    <br />
    拿到其他....该产品的详细参数

    <br />
    <br />
    <br />
    <br />

    <a
        style="display: block; width: 100px; height: 35px; line-height: 35px; text-align: center; color: red;"
        href="<%=request.getContextPath()%>/addcart.pdo?pname=<%=pname %>">加入购物车</a>
</body>
</html>

shoppingcart.jsp页面的实现

<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <%
    List<String> products = (List<String>) session.getAttribute("car");

    for(String s:products){
        out.println(s+"<br/><br/>");
        
    }
    %>

</body>
</html>

servlet容器的实现

ShopController

package cn.course.mvcproject.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;

@WebServlet(urlPatterns = { "*.pdo" })
public class ShopController extends HttpServlet {

    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // 在这一个方法里边能处理增删改查所有功能
        // 设置字符集
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        String mn = req.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, req, resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void shopping(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String pname = req.getParameter("shoppingname");
        req.setAttribute("p", pname);
        req.getRequestDispatcher("/productdetails.jsp").forward(req, resp);
    }

    private void addcart(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        
        String pname = req.getParameter("pname");
        // 添加到购物车
        HttpSession session = req.getSession(true);
        
        List<String> products = (List<String>) session.getAttribute("car");
        
        if (products == null) {
            products = new ArrayList<>();

        }
        products.add(pname);
        
        session.setAttribute("car", products);

        resp.setCharacterEncoding("UTF-8");
        resp.sendRedirect(req.getContextPath() + "/shoppingcart.jsp");

    }
}
 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值