package com.frog.vote.servlet.front; import java.io.IOException; import java.sql.Timestamp; import java.util.ArrayList; import java.util.List; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.frog.vote.bean.UserBean; import com.frog.vote.cons.Const; import com.frog.vote.dao.entity.Vote; import com.frog.vote.factory.ServiceFactory; public class VoteServlet ex tends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("gbk"); resp.setContentType("text/html;charset=gbk"); // 先取出操作参数 String action = req.getParameter("action"); ServletContext application = getServletContext(); // 显示的列表 if ("show".equals(action)) { // 调用service层方法得到所有的 String temp = req.getParameter("page"); int page = 1; if (temp != null) { try { page = Integer.parseInt(temp); } catch (NumberFormatException e) { e.printStackTrace(); } } UserBean ub = (UserBean)req.getSession().getAttribute(Const.USER); int uid = ub.getId(); req.setAttribute("page", ServiceFactory .getVoteService().listUserPage(page,uid)); application.getRequestDispatcher("/WEB-INF/front/list.jsp") .forward(req, resp); return; } else if ("save".equals(action) || "update".equals(action)) { String sid = req.getParameter("id"); String[] answers = req.getParameterValues("choice"); List list = new ArrayList(); int temp; for (String answer : answers) { temp = Integer.parseInt(answer); list.add(temp); } int tid = Integer.parseInt(sid); UserBean ub = (UserBean)req.getSession().getAttribute(Const.USER); Vote vote = new Vote(); vote.setUid(ub.getId()); vote.setTid(tid); vote.setVoteTime(new Timestamp(System.currentTimeMillis())); vote.setAnswer(list); vote.setIp(req.getRemoteAddr()); List listErr = null; if ("update".equals(action)) { // flag = ServiceFactory.getVoteService().update(vote); } else { vote.setStatus(Vote.STATUS_ACTIVE); listErr = ServiceFactory.getVoteService().save(vote); } if (listErr==null) { // resp.sendRedirect(req.getContextPath()+"/main?action=welcome"); application.getRequestDispatcher("/WEB-INF/front/success.jsp").forward(req, resp); return; } req.setAttribute("err", listErr); req.getRequestDispatcher("/err.jsp").forward(req, resp); return; } else if ("delete".equals(action)) { String gid = req.getParameter("gid"); int gidNumber = Integer.parseInt(gid); boolean flag = ServiceFactory.getVoteService().delete(gidNumber); if (flag) { req.getRequestDispatcher("/admin/goods?action=list").forward( req, resp); return; } req.getRequestDispatcher("/err.jsp").forward(req, resp); return; } else if ("modify".equals(action)) { String gid = req.getParameter("gid"); int gidNumber = Integer.parseInt(gid); Vote vote = ServiceFactory.getVoteService().getById(gidNumber); req.setAttribute("detai", vote); if (vote != null) { req.getRequestDispatcher("/admin/goods/modify.jsp").forward( req, resp); return; } req.getRequestDispatcher("/err.jsp").forward(req, resp); return; } } }