图书管理系统:
servlet:
package sevlet; import java.time.*; import java.time.format.DateTimeFormatter; import java.text.*; import java.io.IOException; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import bean.dao; import bean.reader; import bean.book; /** * Servlet implementation class Lsevlet */ @WebServlet("/Lsevlet") public class Lsevlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public Lsevlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doPost(request,response); response.getWriter().append("Served at: ").append(request.getContextPath()); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); String method=request.getParameter("method"); dao p=new dao(); if(method.equals("login")) { String user=request.getParameter("user"); String num=request.getParameter("num"); String password=request.getParameter("password"); if(user.equals("管理员")) { try { if(p.checkM(num, password)) { List<book> d=new ArrayList<book>(); d=p.findbp1(); for(book P:d) { System.out.println(P.getHome()); } request.setAttribute("Pastbook",d); request.getRequestDispatcher("loginM.jsp").forward(request, response); } else { System.out.println("用户名或密码错误!请重新登录!"+num+password); request.getRequestDispatcher("user.jsp").forward(request, response); } } catch (SQLException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } else if(user.equals("读者")) { try { if(p.checkR(num, password)) { List<book> c=new ArrayList<book>(); c=p.findb(); request.setAttribute("Abook",c); reader a=new reader(); a=p.Rlook(num); List<book> d=new ArrayList<book>(); d=p.findbp(a.getName()); List<book> e=new ArrayList<book>(); e=p.findbm(a.getName()); request.setAttribute("Pbook",d); request.setAttribute("Mbook",e); request.setAttribute("Areader",a); request.getRequestDispatcher("loginR.jsp").forward(request, response); } else { System.out.println("用户名或密码错误!请重新登录!"+num+password); request.getRequestDispatcher("user.jsp").forward(request, response); } } catch (SQLException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } } else if(method.equals("addR")) { String num=request.getParameter("num"); String name=request.getParameter("name"); String sex=request.getParameter("sex"); String school=request.getParameter("school"); String password=request.getParameter("password"); reader P=new reader(); P.setNum(num); P.setName(name); P.setSex(sex); P.setSchool(school); try { if(p.addReader(P, password)) { System.out.println("添加成功"); request.getRequestDispatcher("loginM.jsp").forward(request, response); } } catch (SQLException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } catch (ServletException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } catch (IOException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } else if(method.equals("addB")) { String ID=request.getParameter("ID"); String name=request.getParameter("name"); String writter=request.getParameter("writter"); String home=request.getParameter("home"); String num=request.getParameter("num"); book P=new book (); P.setNum(num); P.setName(name); P.setID(ID); P.setHome(home); P.setWritter(writter); try { if(p.addBook(P)) { System.out.println("添加成功"); request.getRequestDispatcher("loginM.jsp").forward(request, response); } } catch (SQLException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } catch (ServletException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } catch (IOException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } else if(method.equals("seach")) { String user=request.getParameter("user"); String name=request.getParameter("name"); List<book> c=new ArrayList<book>(); try { c=p.findb(); } catch (SQLException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } List<book> b=new ArrayList<book>(); if(user.equals("书名")) { try { b=p.findb1("name",name); } catch (SQLException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } else { try { b=p.findb1("writter",name); } catch (SQLException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } request.setAttribute("Abook",c); request.setAttribute("Abook1",b); reader a=(reader) request.getSession().getAttribute("Areader"); System.out.println(a.getName()); request.setAttribute("Areader",a); request.getRequestDispatcher("seach1.jsp").forward(request, response); } else if(method.equals("borrow")) { String user=request.getParameter("user"); String name=request.getParameter("name"); List<book> c=new ArrayList<book>(); try { c=p.findb(); } catch (SQLException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } List<book> b=new ArrayList<book>(); if(user.equals("书名")) { try { b=p.findb1("name",name); } catch (SQLException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } else if(user.equals("作者")) { try { b=p.findb1("writter",name); } catch (SQLException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } else if(user.equals("编号")) try { b=p.findb1("ID",name); } catch (SQLException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } else if(user.equals("出版社")) try { b=p.findb1("hone",name); } catch (SQLException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } request.setAttribute("Abook",c); request.setAttribute("Abook1",b); reader a=(reader) request.getSession().getAttribute("Areader"); System.out.println(a.getName()); request.setAttribute("Areader",a); request.getRequestDispatcher("borrow1.jsp").forward(request, response); } else if(method.equals("borrow2")) { LocalDate date = LocalDate.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy"); String time = date.format(formatter); String ID=(String) request.getSession().getAttribute("ID"); reader a=(reader) request.getSession().getAttribute("Areader"); System.out.println(ID); try { p.borrow(ID, a,time,p.lasttime(time)); } catch (SQLException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } List<book> c=new ArrayList<book>(); try { c=p.findb(); } catch (SQLException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } request.setAttribute("Abook",c); request.setAttribute("Areader",a); request.getRequestDispatcher("loginR.jsp").forward(request, response); } else if(method.equals("huan")) { String ID=(String)request.getSession().getAttribute("ID"); reader a=(reader) request.getSession().getAttribute("Areader"); try { p.huan(ID, a); } catch (SQLException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } List<book> c=new ArrayList<book>(); try { c=p.findb(); } catch (SQLException e1) { // TODO 自动生成的 catch 块 e1.printStackTrace(); } request.setAttribute("Abook",c); List<book> d=new ArrayList<book>(); try { d=p.findbp(a.getName()); } catch (SQLException e1) { // TODO 自动生成的 catch 块 e1.printStackTrace(); } List<book> e=new ArrayList<book>(); try { e=p.findbm(a.getName()); } catch (SQLException e1) { // TODO 自动生成的 catch 块 e1.printStackTrace(); } request.setAttribute("Pbook",d); request.setAttribute("Mbook",e); request.setAttribute("Areader",a); request.getRequestDispatcher("loginR.jsp").forward(request, response); } } }