码迷,mamicode.com
首页 > 其他好文 > 详细

期中测试---人口普查serlet代码

时间:2020-12-19 13:37:05      阅读:3      评论:0      收藏:0      [点我收藏+]

标签:pat   ati   static   ack   rgb   dex   klist   serial   super   

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);
    }

}

 

期中测试---人口普查serlet代码

标签:pat   ati   static   ack   rgb   dex   klist   serial   super   

原文地址:https://www.cnblogs.com/linmob/p/14139439.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!