Servlet

package com.eshop.servlets;

import java.io.IOException;
import java.io.PrintWriter;
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;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.eshop.beans.Good;
import com.eshop.beans.Shoping;
import com.eshop.beans.User;
import com.eshop.dao.GoodADO;
import com.eshop.dao.Shopping;
import com.eshop.dao.UserDAO;
import com.eshop.dao.Impl.GoodDAOImpl;
import com.eshop.dao.Impl.ShoppingImpl;
import com.eshop.dao.Impl.UserDAOImpl;

/**
* Servlet implementation class ShopCartServlet
*/
@WebServlet("/ShopCartServlet")
public class ShopCartServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public ShopCartServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
@Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
        doPost(request, response);
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
@Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
        PrintWriter out = response.getWriter();
        response.setContentType("text/html;charset=utf-8");
        UserDAO action = new UserDAOImpl();
        GoodADO goodAction = new GoodDAOImpl();
        Shopping shopAction = new ShoppingImpl();
        String type = request.getParameter("type");
        System.out.println("userAction:" + type);
        HttpSession session = request.getSession();
        String str = (String) session.getAttribute("USERNAME");
        List<User> list = action.getUser(str);
        User user = list.get(0);
        if (str.equals("") || str == null) {
            out.print("false");
            out.flush();
            out.close();
            return;
        }
        if (type.equals("c1")) {
            String id = request.getParameter("gid");
            List<Good> goodlist = new ArrayList<Good>();
            goodlist = goodAction.getGoodById(id);
            Good good = goodlist.get(0);
            Shoping cart = new Shoping();
            cart.setDeal(false);
            cart.setDel(false);
            cart.setGid(Integer.parseInt(id));
            cart.setId(user.getId());
            if (shopAction.addShoping(cart) > 0) {
                out.print("true");
            } else {
                out.print("false");
            }
        } else if (type.equals("c2")) {
            List<Shoping> cartlist = new ArrayList<Shoping>();
            cartlist = shopAction.getShoping(str);
            JSONObject json = new JSONObject();
            JSONArray array = new JSONArray();
            for (int i = 0; i < cartlist.size(); i++) {
                JSONObject member = new JSONObject();
                Shoping cart = cartlist.get(i);
                member.put("id", cart.getId());
                member.put("sid", cart.getSid());
                member.put("gid", cart.getGid());
                member.put("date", cart.getDate() + "");
                member.put("deal", cart.isDeal());
                member.put("del", cart.isDel());
                array.add(member);
            }
            json.put("mydata", array);
            json.put("len", cartlist.size());
            int page = 0;
            if (cartlist.size() % 6 == 0) {
                page = cartlist.size() / 6;
            } else {
                page = cartlist.size() / 6 + 1;
            }
            json.put("page", page);
            out.print(json.toString());
        } else if (type.equals("c3")) {
            String sid = request.getParameter("sid");
            Shoping cart = new Shoping();
            cart.setDeal(true);
            cart.setDel(Boolean.parseBoolean(request.getParameter("del")));
            cart.setGid(Integer.parseInt(request.getParameter("gid")));
            cart.setId(user.getId());
            cart.setSid(Integer.parseInt(sid));
            if (shopAction.updateShoping(cart) > 0) {
                out.print("true");
            } else {
                out.print("false");
            }
        } else if (type.equals("c4")) {
            String sid = request.getParameter("sid");
            Shoping cart = new Shoping();
            cart.setDeal(Boolean.parseBoolean(request.getParameter("deal")));
            cart.setDel(true);
            cart.setGid(Integer.parseInt(request.getParameter("gid")));
            cart.setId(user.getId());
            cart.setSid(Integer.parseInt(sid));
            if (shopAction.updateShoping(cart) > 0) {
                out.print("true");
            } else {
                out.print("false");
            }
        }
        out.flush();
        out.close();
}

}

你可能感兴趣的:(S)