课堂测试---图书管理系统servlet代码

useservlet

package 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 javax.servlet.http.HttpSession;

import dao.*;
@WebServlet("/useservlet")
public class useservlet extends HttpServlet{
    private static final long serialVersionUID = 1L;

    public useservlet() {
        super();
    }
    usedao dao=new usedao();

    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String method = req.getParameter("method");
        if ("login".equals(method)) {
            login(req, resp);
        }else if ("lookBook".equals(method)) {
            lookBook(req, resp);
        }else if ("searchBook".equals(method)) {
            searchBook(req, resp);
        }else if("getbookbybid".equals(method)){
            getbookbybid(req,resp);
        }else if("cuiBook".equals(method)){
            cuiBook(req,resp);
        }else if("jieBooklist".equals(method)){
            jieBooklist(req,resp);
        }else if("huanBookbybid".equals(method)){
            huanBookbybid(req,resp);
        }else if("addDu".equals(method)){
            addDu(req,resp);
        }else if("addBook".equals(method)){
            addBook(req,resp);
        }else if("cuilist".equals(method)){
            cuilist(req,resp);
        }
    }

    private void login(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        String id = req.getParameter("id");
        HttpSession session =req.getSession();
        session.setAttribute("duid",id);
        String password = req.getParameter("password");
        use u=new use(id,password);
        req.setAttribute("duid",id);
        String form=dao.login(u);
        if(!form.equals("false")){
            req.setAttribute("message", "登录成功!");
            if(form.equals("读者"))req.getRequestDispatcher("dulist.jsp").forward(req, resp);
            if(form.equals("管理员"))req.getRequestDispatcher("genlist.jsp").forward(req, resp);
        }else{
            req.setAttribute("message", "登录失败!");
            req.getRequestDispatcher("index.jsp").forward(req, resp);
        }
    }

    private void lookBook(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        req.setCharacterEncoding("utf-8");
        List<book> books = dao.lookbook();
        req.setAttribute("books",books);
        req.getRequestDispatcher("lookBook.jsp").forward(req,resp);
    }

    private void searchBook(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
        req.setCharacterEncoding("utf-8");
        HttpSession session = req.getSession();
        String duid =(String) session.getAttribute("duid");
        String bname = req.getParameter("bname");
        String bwriter = req.getParameter("bwriter");
        List<book> books = dao.search(bname,bwriter);
        req.setAttribute("books", books);
        req.getRequestDispatcher("searchlist.jsp").forward(req,resp);
    }

    private void getbookbybid(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
        req.setCharacterEncoding("utf-8");
        String bid = req.getParameter("bid");
        HttpSession session = req.getSession();
        String duid =(String) session.getAttribute("duid");
        book book=dao.getBookbybid(bid);
        if(!(book.getBnum()).equals("0")) {
            String a=dao.getbook(book,duid);
            if(!a.equals("false"))
            {
                req.setAttribute("message", "借阅成功,请于"+ a +"前归还");
                req.getRequestDispatcher("dulist.jsp").forward(req,resp);
            }
        }
        req.setAttribute("message", "借阅失败");
        req.getRequestDispatcher("dulist.jsp").forward(req,resp);
    }

    private void cuiBook(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
        req.setCharacterEncoding("utf-8");
        HttpSession session = req.getSession();
        String duid =(String) session.getAttribute("duid");
        List<cui> cui = dao.searchcui(duid);
        req.setAttribute("cui", cui);
        req.getRequestDispatcher("cuilist.jsp").forward(req,resp);
    }

    private void cuilist(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
        req.setCharacterEncoding("utf-8");
        List<cui> cui = dao.searchcui();
        req.setAttribute("cui", cui);
        req.getRequestDispatcher("cuilist2.jsp").forward(req,resp);
    }
     
    private void jieBooklist(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
        req.setCharacterEncoding("utf-8");
        HttpSession session = req.getSession();
        String duid =(String) session.getAttribute("duid");
        List<huan> huan = dao.searchhuan(duid);
        req.setAttribute("huan", huan);
        req.getRequestDispatcher("huanlist.jsp").forward(req,resp);
    }

    private void huanBookbybid(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
        req.setCharacterEncoding("utf-8");
        String bid = req.getParameter("bid");
        HttpSession session = req.getSession();
        String duid =(String) session.getAttribute("duid");
        double money=dao.huanbook(bid,duid);
        if(money!=-1.0)
        {
                dao.ifcui(bid,duid);
                if(money!=0.0){
                    req.setAttribute("message", "还书成功,超期缴纳罚金"+money+"元");
                    req.getRequestDispatcher("dulist.jsp").forward(req,resp);
                }
                req.setAttribute("message", "还书成功");
                req.getRequestDispatcher("dulist.jsp").forward(req,resp);
        }
        req.setAttribute("message", "还书失败");
        req.getRequestDispatcher("dulist.jsp").forward(req,resp);
    }
    //------------------------------------------------------------------------------------------------------
    private void addDu(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        // TODO Auto-generated method stub
        String duid = req.getParameter("duid");
        String duname = req.getParameter("duname");
        String sex = req.getParameter("sex");
        String school = req.getParameter("school");
        du du=new du(duid,duname,sex,school);
        if(dao.addDu(du)) {
            req.setAttribute("message", "保存成功!");
            req.getRequestDispatcher("genlist.jsp").forward(req, resp);
        }else {
            req.setAttribute("message", "保存失败!");
            req.getRequestDispatcher("genlist.jsp").forward(req, resp);
        }
    }

    private void addBook(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        // TODO Auto-generated method stub
        String bid = req.getParameter("bid");
        String bname = req.getParameter("bname");
        String bwriter = req.getParameter("bwriter");
        String bout = req.getParameter("bout");
        String bnum = req.getParameter("bnum");
        book book = new book(bid, bname, bwriter, bout, bnum);
        if (dao.addBook(book)) {
            req.setAttribute("message", "保存成功!");
            req.getRequestDispatcher("genlist.jsp").forward(req, resp);
        } else {
            req.setAttribute("message", "保存失败!");
            req.getRequestDispatcher("genlist.jsp").forward(req, resp);
        }
    }
}

 

上一篇:计算机学院大学生程序设计竞赛(2015’12) 1002 Polygon


下一篇:实现strStr()的golang实现