Servlet.java
package Servlet; import Dao.*; 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 java.io.IOException; import java.util.List; @WebServlet("/Servlet") public class Servlet extends HttpServlet{ private static final long serialVersionUID = 1L; public Servlet() { super(); } Dao dao=new Dao(); 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); }if("look".equals(method)){ look(req,resp); }if("search".equals(method)){ search(req,resp); }if("getbookbyname".equals(method)){ getbookbyname(req,resp); }/*if("jieBooklist".equals(method)){ jieBooklist(req,resp); }if("huanBookbybid".equals(method)){ huanBookbybid(req,resp); }*/if("addBook".equals(method)){ addBook(req,resp); } /* if("".equals(method)){ (req,resp); } */ } //------------------------------------------------------------------------------------------------------ private void login(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { // TODO Auto-generated method stub String id = req.getParameter("id"); HttpSession session =req.getSession(); session.setAttribute("id",id); String password = req.getParameter("password"); System.out.println("servlet.login"+" id="+id+" password="+password); User user=dao.login(id); System.out.println(user.getForm()); if(user.getPassword()!=null&&user.getPassword().equals(password)) { req.setAttribute("message", "登录成功"); if (user.getForm().equals("管理员")) { req.getRequestDispatcher("admin.jsp").forward(req, resp); } else if (user.getForm().equals("员工")) { req.getRequestDispatcher("teacher.jsp").forward(req, resp); } }else{ req.setAttribute("message","登录失败"); req.getRequestDispatcher("index.jsp").forward(req,resp); } } private void look(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { req.setCharacterEncoding("utf-8"); List<book> books = dao.look(); req.setAttribute("books",books); req.getRequestDispatcher("look.jsp").forward(req,resp); } private void search(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); String name = req.getParameter("name"); List<book> books = dao.search(name); req.setAttribute("books", books); req.getRequestDispatcher("searchlist.jsp").forward(req,resp); } private void getbookbyname(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); String name = req.getParameter("name"); boolean a=dao.huan(name); if(a) { req.setAttribute("message", "删除成功"); req.getRequestDispatcher("teacher.jsp").forward(req,resp); } req.setAttribute("message", "删除失败"); req.getRequestDispatcher("teacher.jsp").forward(req,resp); } private void addBook(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { // TODO Auto-generated method stub String name = req.getParameter("name"); String sex = req.getParameter("sex"); String age = req.getParameter("age"); String unit = req.getParameter("unit"); String work = req.getParameter("work"); String swage = req.getParameter("swage"); String a1 = req.getParameter("a1"); String a2 = req.getParameter("a2"); String b1 = req.getParameter("b1"); String b2 = req.getParameter("b2"); String ewage=String.valueOf(Integer.valueOf(swage)+Integer.valueOf(a1)+Integer.valueOf(a2)-Integer.valueOf(b1)-Integer.valueOf(b2)); book book = new book(name,sex,age,unit,work,ewage); if (dao.addBook(book)) { req.setAttribute("message", "保存成功!"); req.getRequestDispatcher("admin.jsp").forward(req, resp); } else { req.setAttribute("message", "保存失败!"); req.getRequestDispatcher("admin.jsp").forward(req, resp); } } }