protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
PrintWriter out = response.getWriter();
String op = request.getParameter("operation");
if (op.trim().equals("addcategory")) {
// fetching data from category form
String title = request.getParameter("catTitle");
String description = request.getParameter("catDesc");
// create category objet to set an attribute
Category ct = new Category();
ct.setCategoryTitle(title);
ct.setCategoryDescription(description);
// category save in database
CategoryDao categoryDao = new CategoryDao(FactoryProvider.getFactory());
int catId = categoryDao.saveCategory(ct);
HttpSession session = request.getSession();
session.setAttribute("message", "New category added successfully!!"+catId);
response.sendRedirect("admin.jsp");
return;
} else if (op.trim().equals("addProduct")) {
System.out.println("running addproduct conditionnnnnnnnnnn");
String pName = request.getParameter( "pName");
String pPhoto = request.getParameter("pPhoto");
String pDesc = request.getParameter("pDesc");
int pPrice = Integer.parseInt(request.getParameter(("pPrice")));
int pDiscount = Integer.parseInt(request.getParameter("pDiscount"));
int pQuantity = Integer.parseInt(request.getParameter("pQuantity"));
int catId = Integer.parseInt(request.getParameter("catId"));
Part part = request.getPart("pPhoto");
Product p = new Product();
p.setpName(pName);
p.setpPhoto(pPhoto);
p.setpDesc(pDesc);
p.setpPrice(pPrice);
p.setpDiscount(pDiscount);
p.setpQuantity(pQuantity);
p.setpPhoto(part.getSubmittedFileName());
// get Category by id
CategoryDao cdao = new CategoryDao(FactoryProvider.getFactory());
Category category = cdao.getCategoryById(catId);
p.setCategory(category);
//product save
//constructor ask for SessionFactory
ProductDao pdao = new ProductDao(FactoryProvider.getFactory());
//pic upload
//Find out the path to upload photo
@SuppressWarnings("deprecation")
String path = request.getRealPath("img") + File.separator + "products"+ File.separator +
part.getSubmittedFileName();
out.println("path:: "+path);
//uploading data
try {
FileOutputStream fos = new FileOutputStream(path);
InputStream is = part.getInputStream();
//reading data
byte[] data=new byte[is.available()];
is.read(data);
//writing the data
fos.write(data);
fos.close();
} catch (Exception e) {
// TODO: handle exception
}
