⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cartcountfilter.java

📁 在线购物系统,ajax+jsp实现
💻 JAVA
字号:
package com.accp.gz.th.zm.client.servlet.filter;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import com.accp.gz.th.zm.client.javabean.*;

public class CartCountFilter extends HttpServlet implements Filter {

    private FilterConfig filterConfig;
    //Handle the passed-in FilterConfig
    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig = filterConfig;
    }

    //Process the request/response pair
    public void doFilter(ServletRequest request, ServletResponse response,
                         FilterChain filterChain) {
        try {
            // 计算购物车的总价格
            HttpSession session = ((HttpServletRequest) request).getSession();
            ArrayList cartlist = null;
            Object objCartList = session.getAttribute("cartlist");
            if (objCartList == null) {
                cartlist = new ArrayList();
                session.setAttribute("cartlist", cartlist);
            } else {
                cartlist = (ArrayList) objCartList;
            }

            float totalpriceofcart = 0;
            for (int i = 0; i < cartlist.size(); i++) {
                CartBean bean = (CartBean) cartlist.get(i);
                totalpriceofcart += bean.getPrice() * bean.getNumber();
            }
            session.setAttribute("totalpriceofcart", new Float(totalpriceofcart));


            filterChain.doFilter(request, response);
        } catch (ServletException sx) {
            filterConfig.getServletContext().log(sx.getMessage());
        } catch (IOException iox) {
            filterConfig.getServletContext().log(iox.getMessage());
        }
    }

    //Clean up resources
    public void destroy() {
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -