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

📄 cartservlet.java

📁 一个基于java工厂模式的 的实现
💻 JAVA
字号:
/*
 * CartServlet.java
 *
 * Created on 2007年4月20日, 下午3:52
 */

package com.ebuy.web.servlets;

import com.ebuy.web.beans.Goods;
import com.ebuy.web.beans.ShoppingCart;
import java.io.*;
import java.net.*;
import java.util.ArrayList;

import javax.servlet.*;
import javax.servlet.http.*;

/**
 *
 * @author Administrator
 * @version
 */
public class CartServlet extends HttpServlet
{
    
    /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
    {
        response.setContentType("text/html;charset=gb2312");
        request.setCharacterEncoding("gb2312");
        String exectype=request.getParameter("exectype");        
        if(exectype.equalsIgnoreCase("add"))
        {
            this.doAdd(request,response);
        }
        else if(exectype.equalsIgnoreCase("modify"))
        {
            this.doModify(request,response);
        }
        else if(exectype.equalsIgnoreCase("remove"))
        {
            this.doRemove(request,response);
        }   
    }
    private void doAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        
        String pid=request.getParameter("productid");
        String pname=request.getParameter("productname");
        String unitprice=request.getParameter("unitprice");
        String realprice=request.getParameter("realprice");
        Goods good=new Goods();
        good.setProductid(Integer.parseInt(pid));
        good.setProductname(pname);
        good.setUnitprice(Double.parseDouble(unitprice));
        good.setRealprice(Double.parseDouble(realprice));
        good.setQuantity(1);
        
        ShoppingCart cart=(ShoppingCart)request.getSession().getAttribute("cart");
        if(cart!=null)
        {
            cart.addGood(good);
        }
        else
        {
            cart=new ShoppingCart();
            cart.addGood(good);
            request.getSession().setAttribute("cart",cart);
        }
        response.sendRedirect("../mycart.jsp");
       
    }
    private void doModify(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
    {
        ShoppingCart cart=(ShoppingCart)request.getSession().getAttribute("cart");
        if(cart==null)
        {
            String msg="<script>window.alert('对不起,会话超时');window.location='../index.jsp';</script>";
            PrintWriter out=response.getWriter();
            out.write(msg);
            out.close();
            return;            
        }
        ArrayList<Goods> list=cart.getItems();
        int count=list.size();
        String val;
        for(int i=count-1;i>=0;i--)
        {
            val=request.getParameter(String.valueOf(i));
            cart.updateQuantityByIndex(i,Integer.parseInt(val));
        } 
         response.sendRedirect("../mycart.jsp");
    }
    private void doRemove(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
    {
        ShoppingCart cart=(ShoppingCart)request.getSession().getAttribute("cart");
        if(cart==null)
        {
            String msg="<script>window.alert('对不起,会话超时');window.location='../index.jsp';</script>";
            PrintWriter out=response.getWriter();
            out.write(msg);
            out.close();
            return;            
        }
        String[] val=request.getParameterValues("ckselect");
        int count=val.length;      
        int index;
        for(int i=count-1;i>=0;i--)
        {
            index=Integer.parseInt(val[i]);
            cart.removeGoodByIndex(index);
        }
        response.sendRedirect("../mycart.jsp");        
    }
    
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
    {
        processRequest(request, response);
    }
    
    /** Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
    {
        processRequest(request, response);
    }
    
    /** Returns a short description of the servlet.
     */
    public String getServletInfo()
    {
        return "Short description";
    }
    // </editor-fold>
}

⌨️ 快捷键说明

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