1. 程式人生 > 實用技巧 >期中測試---人口普查serlet程式碼

期中測試---人口普查serlet程式碼

Billservlet

package web.servlet;
import java.io.IOException;
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 dao.BillDao; import dao.Bill; @WebServlet("/BillServlet") public class BillServlet extends HttpServlet{ private static final long serialVersionUID = 1L; public BillServlet() { super(); } BillDao dao=new BillDao(); protected void service(HttpServletRequest req, HttpServletResponse resp) throws
ServletException, IOException { req.setCharacterEncoding("utf-8"); String method = req.getParameter("method"); if ("add".equals(method)) { add(req, resp); }else if ("getbillbyidcard".equals(method)) { getBillByIdcard(req, resp); }else if
("dellist".equals(method)) { dellist(req,resp); }else if ("delete".equals(method)) { delete(req,resp); }else if ("looklist".equals(method)) { looklist(req,resp); }else if ("getbillbyidcard2".equals(method)) { getBillByIdcard2(req, resp); }else if ("modifylist".equals(method)) { modifylist(req,resp); }else if ("modify".equals(method)) { modify(req,resp); }else if ("search".equals(method)) { search(req,resp); }else if ("getbillbyidcard3".equals(method)) { getBillByIdcard3(req, resp); } } //------------------------------------------------------------------------------------------------------ private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { // TODO Auto-generated method stub String gender = req.getParameter("gender"); String house = req.getParameter("house"); String sqare = req.getParameter("sqare"); String jian = req.getParameter("jian"); String owname = req.getParameter("owname"); String idcard = req.getParameter("idcard"); String sex = req.getParameter("sex"); String zu = req.getParameter("zu"); String address = req.getParameter("address"); Bill bill=new Bill(gender,house,sqare,jian,owname,idcard,sex,zu,address); if(dao.add(bill)) { req.setAttribute("message", "儲存成功!"); req.getRequestDispatcher("BillServlet?method=looklist").forward(req, resp); }else { req.setAttribute("message", "儲存失敗!"); req.getRequestDispatcher("add.jsp").forward(req, resp); } } //------------------------------------------------------------------------------------------------------ private void getBillByIdcard(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); String idcard = req.getParameter("idcard"); Bill bill = dao.getBillByIdcard(idcard); req.setAttribute("bill", bill); req.getRequestDispatcher("delete.jsp").forward(req,resp); } private void dellist(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { req.setCharacterEncoding("utf-8"); List<Bill> bills = dao.dellist(); req.setAttribute("bills", bills); req.getRequestDispatcher("dellist.jsp").forward(req,resp); } private void looklist(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { req.setCharacterEncoding("utf-8"); List<Bill> bills = dao.dellist(); req.setAttribute("bills", bills); req.getRequestDispatcher("looklist.jsp").forward(req,resp); } private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); String idcard = req.getParameter("idcard"); if(dao.delete(idcard)) { req.setAttribute("message", "刪除成功"); req.getRequestDispatcher("BillServlet?method=looklist").forward(req,resp); } else req.setAttribute("message", "刪除失敗"); req.getRequestDispatcher("index.jsp").forward(req,resp); } //--------------------------------------------------------------------------------------------------------------------------------- private void getBillByIdcard2(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); String idcard = req.getParameter("idcard"); Bill bill = dao.getBillByIdcard(idcard); req.setAttribute("bill", bill); req.getRequestDispatcher("modify.jsp").forward(req,resp); } private void modifylist(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); List<Bill> bills = dao.modifylist(); req.setAttribute("bills",bills); req.getRequestDispatcher("modifylist.jsp").forward(req,resp); } private void modify(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); String oldidcard = req.getParameter("oldidcard"); String gender = req.getParameter("gender"); String house = req.getParameter("house"); String sqare = req.getParameter("sqare"); String jian = req.getParameter("jian"); String owname = req.getParameter("owname"); String idcard = req.getParameter("idcard"); String sex = req.getParameter("sex"); String zu = req.getParameter("zu"); String address = req.getParameter("address"); Bill bill = new Bill(gender,house,sqare,jian,owname,idcard,sex,zu,address); boolean a=dao.modify(bill,oldidcard); if(a==true) { req.setAttribute("message", "修改成功"); req.getRequestDispatcher("BillServlet?method=looklist").forward(req,resp); } req.setAttribute("message", "修改失敗"); req.getRequestDispatcher("index.jsp").forward(req,resp); } //--------------------------------------------------------------------------------------------------------------------------------- private void search(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); String owname = req.getParameter("owname"); String sex = req.getParameter("sex"); String zu = req.getParameter("zu"); String address = req.getParameter("address"); List<Bill> bills = dao.search(owname,sex,zu,address); req.setAttribute("bills", bills); req.getRequestDispatcher("searchlist.jsp").forward(req,resp); } private void getBillByIdcard3(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); String idcard = req.getParameter("idcard"); Bill bill = dao.getBillByIdcard(idcard); req.setAttribute("bill", bill); req.getRequestDispatcher("searesult.jsp").forward(req,resp); } }