11.28
图书信息管理系统
今天发一下servlet层所有代码,发完这些后,一个完整的图书信息管理系统也就完全解决了
package libraryservlet; import java.io.IOException; 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.SchoolDaoImpl; import entity.School; /** * Servlet implementation class libraryaddservlet */ @WebServlet("/libraryaddservlet") public class libraryaddservlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public libraryaddservlet() { 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 //response.getWriter().append("Served at: ").append(request.getContextPath()); request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); String stuID=request.getParameter("stuID"); String stuname=request.getParameter("stuname"); String stusex=request.getParameter("stusex"); String college=request.getParameter("college"); String stupassword=request.getParameter("stupassword"); String bookID=request.getParameter("bookID"); String bookname=request.getParameter("bookname"); String author=request.getParameter("author"); String banname=request.getParameter("banname"); String booknum=request.getParameter("booknum"); String method=request.getParameter("method"); System.out.println(method); SchoolDaoImpl dao=new SchoolDaoImpl(); if("stu".equals(method)) { try{ System.out.println(1); dao.Stuadd(stuID,stupassword,stuname,stusex,college); request.getRequestDispatcher("manage.jsp").forward(request, response); }catch(Exception e){ System.out.println("添加失败"); e.printStackTrace(); } } if("book".equals(method)) { try{ System.out.println(1); dao.Bookadd(bookID,bookname,author,banname,booknum); request.getRequestDispatcher("manage.jsp").forward(request, response); }catch(Exception e){ System.out.println("添加失败"); e.printStackTrace(); } } if("bookreturnshow".equals(method)) { try{ java.util.List<School> list=dao.allbookreturnshow(); request.setAttribute("list", list); System.out.println("listshow"); request.getRequestDispatcher("allbookreturnshow.jsp").forward(request, response); }catch(Exception e){ System.out.println("未能正确显示出图书信息"); e.printStackTrace(); } } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
package libraryservlet; import java.io.IOException; 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.SchoolDaoImpl; import entity.School; /** * Servlet implementation class libraryservlet */ @WebServlet("/libraryservlet") public class libraryservlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public libraryservlet() { 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 //response.getWriter().append("Served at: ").append(request.getContextPath()); request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); String ID=request.getParameter("username"); HttpSession session=request.getSession(); session.setAttribute("ID", ID);//存储数据 request.setAttribute("ID", ID); String password=request.getParameter("password"); String method=request.getParameter("profession"); SchoolDaoImpl sd=new SchoolDaoImpl(); if("1".equals(method)) { try{ System.out.println(1); if("123456".equals(ID)&&"654321".equals(password)) { response.getWriter().println("<script type='text/javascript'> alert('登录成功')</script>"); request.getRequestDispatcher("manage.jsp").forward(request, response);} else { response.getWriter().println("<script type='text/javascript'> alert('密码或账号错误')</script>"); request.getRequestDispatcher("library.jsp").forward(request, response);} }catch(Exception e){ System.out.println("失败"); e.printStackTrace(); } } if("2".equals(method)) { try{ System.out.println(1); String stupass=sd.showstuname(ID); if(password.equals(stupass)) { SchoolDaoImpl sd1=new SchoolDaoImpl(); java.util.List<School> list=sd1.stushow(ID); request.setAttribute("list", list); response.getWriter().println("<script type='text/javascript'> alert('登录成功')</script>"); request.getRequestDispatcher("student.jsp").forward(request, response);} else { response.getWriter().println("<script type='text/javascript'> alert('账号或密码错误')</script>"); request.getRequestDispatcher("library.jsp").forward(request, response);} }catch(Exception e){ System.out.println("失败"); e.printStackTrace(); } } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
package libraryservlet; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; 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.SchoolDaoImpl; import entity.School; /** * Servlet implementation class Stuservlet */ @WebServlet("/Stuservlet") public class Stuservlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public Stuservlet() { 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 //response.getWriter().append("Served at: ").append(request.getContextPath()); request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); String method=request.getParameter("method"); SchoolDaoImpl sd=new SchoolDaoImpl(); String stuID=(String) request.getSession().getAttribute("stuID"); String stuname=(String) request.getSession().getAttribute("stuname"); String stusex=(String) request.getSession().getAttribute("stusex"); String college=(String) request.getSession().getAttribute("college"); if("show".equals(method)) { try{ java.util.List<School> list=sd.bookshow(); request.setAttribute("list", list); System.out.println("listshow"); request.getRequestDispatcher("stubookshow.jsp").forward(request, response); }catch(Exception e){ System.out.println("未能正确显示出图书信息"); e.printStackTrace(); } } if("selectbook".equals(method)) { try{ String fangshi=request.getParameter("fangshi"); System.out.println("查询方式为:"+fangshi); String xinxi=request.getParameter("xinxi"); System.out.println(xinxi); if("1".equals(fangshi)) { java.util.List<School> list=sd.selecebookname(xinxi); request.setAttribute("list", list); System.out.println("listshow"); request.getRequestDispatcher("selectshow.jsp").forward(request, response); } if("2".equals(fangshi)){ java.util.List<School> list=sd.selectbook(xinxi); request.setAttribute("list", list); System.out.println("listshow"); request.getRequestDispatcher("selectshow.jsp").forward(request, response); } }catch(Exception e){ System.out.println("未能正确显示出图书信息"); e.printStackTrace(); } } if("showselectbook".equals(method)) { try{ String fangshi=request.getParameter("fangshi"); System.out.println("查询方式为:"+fangshi); String xinxi=request.getParameter("xinxi"); System.out.println(xinxi); if("1".equals(fangshi)) { java.util.List<School> list=sd.selecebookname(xinxi); request.setAttribute("list", list); System.out.println("listshow"); request.getRequestDispatcher("stubookshow1.jsp").forward(request, response); } if("3".equals(fangshi)){ java.util.List<School> list=sd.selectbook(xinxi); request.setAttribute("list", list); System.out.println("listshow"); request.getRequestDispatcher("stubookshow1.jsp").forward(request, response); } }catch(Exception e){ System.out.println("未能正确显示出图书信息"); e.printStackTrace(); } } if("jieyue".equals(method)) { Date date = new Date(); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); System.out.println(formatter.format(date)); String timenow=null; String timenext=null; timenow=formatter.format(date); System.out.println(timenow); timenext=getAfterMonth(timenow); System.out.println(timenext); HttpSession session=request.getSession(); session.setAttribute("time", timenext); request.setAttribute("time",timenext); String bookID=(String) request.getSession().getAttribute("bookID"); String bookname=(String) request.getSession().getAttribute("bookname"); String author=(String) request.getSession().getAttribute("author"); String banname=(String) request.getSession().getAttribute("banname"); String booknum=(String) request.getSession().getAttribute("booknum"); if(booknum.equals("0")) { request.getRequestDispatcher("select1.jsp").forward(request, response); } else { int booknum1=Integer.valueOf(booknum); --booknum1; String booknum2=String.valueOf(booknum1); School school=new School(); school.setBookID(bookID); school.setBookname(bookname); school.setAuthor(author); school.setBanname(banname); school.setBooknum(booknum2); school.setStuID(stuID); school.setStuname(stuname); school.setStusex(stusex); school.setCollege(college); try{ sd.bookadd(school,timenow,timenext); SchoolDaoImpl sd1=new SchoolDaoImpl(); sd1.update(booknum2,bookID); System.out.println("listshow"); request.getRequestDispatcher("time.jsp").forward(request, response); }catch(Exception e){ System.out.println("未能正确显示出图书信息"); e.printStackTrace(); } } } if("showbook1".equals(method)) { try{ java.util.List<School> list=sd.bookshow2(stuID); request.setAttribute("list", list); System.out.println("listshow"); request.getRequestDispatcher("bookreturn.jsp").forward(request, response); }catch(Exception e){ System.out.println("未能正确显示出图书信息"); e.printStackTrace(); } } if("guihuan".equals(method)) { String bookID=request.getParameter("bookID"); try{ sd.delete(bookID); System.out.println("listshow"); request.getRequestDispatcher("Stuservlet?method=showbook1").forward(request, response); }catch(Exception e){ System.out.println("未能正确显示出图书信息"); e.printStackTrace(); } } if("bookreturnshow".equals(method)) { try{ java.util.List<School> list=sd.bookreturnshow(stuID); request.setAttribute("list", list); System.out.println("listshow"); request.getRequestDispatcher("bookreturnshow.jsp").forward(request, response); }catch(Exception e){ System.out.println("未能正确显示出图书信息"); e.printStackTrace(); } } } public static String getAfterMonth(String inputDate) { Calendar c = Calendar.getInstance();//获得一个日历的实例 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date date = null; try{ date = sdf.parse(inputDate);//初始日期 }catch(Exception e){ } c.setTime(date);//设置日历时间 c.add(Calendar.MONTH,3);//在日历的月份上增加3个月 String strDate = sdf.format(c.getTime());//的到你想要得3个月后的日期 return strDate; } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }