package com;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.dao.ProductDao;
import com.vo.Product;
@SuppressWarnings("serial")
public class ProductServlet extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
String method = request.getParameter("method");
System.out.println("method:" + method);
if (method.equals("findAll"))
{
this.findAll(request, response);
}
else if (method.equals("save"))
{
try
{
this.save(request, response);
} catch (Exception e)
{
e.printStackTrace();
}
}
else if (method.equals("update"))
{
try
{
this.update(request, response);
} catch (Exception e)
{
e.printStackTrace();
}
}
else if (method.equals("remove"))
{
try
{
this.remove(request, response);
} catch (Exception e)
{
e.printStackTrace();
}
}
}
/**
* 查询所有的方法
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
public void findAll(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
String str = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
str += "<catalog>";
ProductDao srv = new ProductDao();
List<Product> list = null;
list = srv.getAll();
Product product;
for (int i = 0; i < list.size(); i++)
{
product = (Product) list.get(i);
str += "<product productId=\"" + product.getId() + "\">";
str += "<id>" + product.getId() + "</id>";
str += "<productName>" + product.getProductName()
+ "</productName>";
str += "<remark>" + product.getRemark() + "</remark>";
str += "</product>";
}
str += "</catalog>";
out.write(str);
}
/**
* 保存的方法
* @param request
* @param response
* @throws Exception
*/
public void save(HttpServletRequest request, HttpServletResponse response)
throws Exception
{
String productName = convert(request.getParameter("productName"));
String remark = convert(request.getParameter("remark"));
Product entity = new Product();
entity.setProductName(productName);
entity.setRemark(remark);
ProductDao pd = new ProductDao();
pd.insert(entity);
}
public void update(HttpServletRequest request, HttpServletResponse response)
throws Exception
{
System.out.println("begin update");
Integer id = Integer.parseInt(request.getParameter("id"));
String productName = convert(request.getParameter("productName"));
String remark = convert(request.getParameter("remark"));
Product entity = new Product();
entity.setId(id);
entity.setProductName(productName);
entity.setRemark(remark);
ProductDao pd = new ProductDao();
pd.update(entity);
}
public void remove(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
System.out.println("begin remove");
Integer id = Integer.parseInt(request.getParameter("id"));
System.out.println("id:" + id);
ProductDao pd = new ProductDao();
pd.remove(id);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
this.doGet(request, response);
}
/*
* 字符转码
*/
public String convert(String str) throws Exception
{
return new String(str.getBytes("iso8859-1"), "utf-8");
}
}