Package com.saya.servlet

Source Code of com.saya.servlet.ProductServlet

package com.saya.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.saya.model.Product;
import com.saya.response.ProductsRs;
import com.saya.response.Response;

@SuppressWarnings("serial")
public class ProductServlet extends HttpServlet {
  protected void doPost(HttpServletRequest req, HttpServletResponse resp)
      throws IOException {
    resp.setContentType("application/json;charset=utf8");
    Boolean validate = Boolean.valueOf(true);
    String des = "Thêm mới sản phẩm thành công";
    String name = req.getParameter("Name");
    String description = req.getParameter("Description");
    if ((name != null) && (!name.equals(""))) {
      Product pro = Product.getProduct(name);
      if (pro == null) {
        validate = Product.createOrUpdateProduct(name, description);
        if (!validate.booleanValue())
          des = "Có lỗi khi thêm mới sản phẩm";
      } else {
        des = "Đã tồn tại sản phẩm";
        validate = Boolean.valueOf(false);
      }
    } else {
      des = "Tên sản phẩm không được để trống";
      validate = Boolean.valueOf(false);
    }

    Response rs = new Response();
    if (validate.booleanValue())
      rs.setCode("0");
    else {
      rs.setCode("1");
    }
    rs.setMessage(des);
    Gson gson = new Gson();
    resp.getWriter().println(gson.toJson(rs));
  }

  protected void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws IOException {
    resp.setContentType("application/json;charset=utf8");
    Boolean status = Boolean.valueOf(true);
    String des = "Lấy danh sách sản phẩm thành công";
    List lstProduct = new ArrayList();
    try {
      lstProduct = Product.getAllProduct();
    } catch (Exception ex) {
      des = ex.getMessage();
      status = Boolean.valueOf(false);
    }

    ProductsRs rs = new ProductsRs();
    if (status.booleanValue())
      rs.setCode("0");
    else {
      rs.setCode("1");
    }
    rs.setMessage(des);
    rs.setLstProduct(lstProduct);
    Gson gson = new Gson();
    resp.getWriter().println(gson.toJson(rs));
  }

}
TOP

Related Classes of com.saya.servlet.ProductServlet

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.