Java学习12.05
图书馆管理系统——servlet层
servlet.java
1 package Servlet; 2 import java.io.IOException; 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import javax.servlet.ServletException; 7 import javax.servlet.annotation.WebServlet; 8 import javax.servlet.http.HttpServlet; 9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletResponse; 11 12 import bean.dao; 13 import bean.Book; 14 import service.Service; 15 16 /** 17 * Servlet implementation class Servlet 18 */ 19 @WebServlet("/Servlet") 20 public class Servlet extends HttpServlet { 21 private static final long serialVersionUID = 1L; 22 23 /** 24 * Default constructor. 25 */ 26 public Servlet() { 27 // TODO Auto-generated constructor stub 28 } 29 30 /** 31 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 32 */ 33 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 34 // TODO Auto-generated method stub 35 //response.getWriter().append("Served at: ").append(request.getContextPath()); 36 doPost(request,response); 37 38 } 39 40 /** 41 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 42 */ 43 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 44 // TODO Auto-generated method stub 45 //doGet(request, response); 46 request.setCharacterEncoding("utf-8"); 47 response.setContentType("text/html;charset=utf-8"); 48 Book webactivity=new Book(); 49 50 Service service=new Service(); 51 52 53 String idBook=request.getParameter("idBook"); 54 String nameBook=request.getParameter("nameBook"); 55 String namePress=request.getParameter("namePress"); 56 String nameWritter=request.getParameter("nameWritter"); 57 String number=request.getParameter("number"); 58 String method=request.getParameter("method"); 59 60 61 webactivity.setIdBook(idBook); 62 webactivity.setNameBook(nameBook); 63 webactivity.setNamePress(namePress); 64 webactivity.setNameWritter(nameWritter); 65 webactivity.setNumber(number); 66 67 68 if("add".equals(method)) { 69 if(Service.add(webactivity)) { 70 System.out.print("添加成功"); 71 List<Book> activity11=Service.list(); 72 request.setAttribute("activity11",activity11); 73 request.setAttribute("message", "添加成功"); 74 request.getRequestDispatcher("managener.jsp").forward(request,response); 75 } 76 else { 77 System.out.println("添加失败"); 78 request.setAttribute("message", "添加失败,该书已存在"); 79 request.getRequestDispatcher("managener.jsp").forward(request,response); 80 } 81 } 82 83 84 85 86 87 88 if("search".equals(method)) { 89 ArrayList<Book> activity1=Service.search(webactivity); 90 if(activity1!=null) { 91 request.setAttribute("message", "查询成功"); 92 System.out.print("查询成功"); 93 request.setAttribute("activity1", activity1); 94 request.getRequestDispatcher("searchshow.jsp").forward(request,response); 95 } 96 else { 97 request.setAttribute("message", "查询失败"); 98 System.out.print("查询失败"); 99 request.getRequestDispatcher("searchshow.jsp").forward(request,response); 100 } 101 } 102 103 if("show".equals(method)) { 104 List<Book> activity11=Service.list(); 105 request.setAttribute("activity11",activity11); 106 request.getRequestDispatcher("listbook.jsp").forward(request,response); 107 } 108 109 if("jieyue".equals(method)) { 110 List<Book> activity11=Service.list(); 111 request.setAttribute("activity11",activity11); 112 request.getRequestDispatcher("jieyuechenggong.jsp").forward(request,response); 113 } 114 } 115 }
servletindex.java
1 package Servlet; 2 3 import java.io.IOException; 4 import java.util.ArrayList; 5 import java.util.List; 6 7 import javax.servlet.ServletException; 8 import javax.servlet.annotation.WebServlet; 9 import javax.servlet.http.HttpServlet; 10 import javax.servlet.http.HttpServletRequest; 11 import javax.servlet.http.HttpServletResponse; 12 13 import bean.studentInfo; 14 import service.Servicepeople; 15 16 /** 17 * Servlet implementation class Servletpeople 18 */ 19 @WebServlet("/Servletpeople") 20 public class Servletpeople extends HttpServlet { 21 private static final long serialVersionUID = 1L; 22 23 /** 24 * @see HttpServlet#HttpServlet() 25 */ 26 public Servletpeople() { 27 super(); 28 // TODO Auto-generated constructor stub 29 } 30 31 /** 32 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 33 */ 34 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 35 // TODO Auto-generated method stub 36 response.getWriter().append("Served at: ").append(request.getContextPath()); 37 } 38 39 /** 40 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 41 */ 42 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 43 // TODO Auto-generated method stub 44 //doGet(request, response); 45 46 request.setCharacterEncoding("utf-8"); 47 response.setContentType("text/html;charset=utf-8"); 48 studentInfo webactivity=new studentInfo(); 49 50 Servicepeople servicepeople=new Servicepeople(); 51 52 53 String name= request.getParameter("name"); 54 String IDnumber=request.getParameter("IDnumber"); 55 String sex=request.getParameter("sex"); 56 String major=request.getParameter("major"); 57 String method=request.getParameter("method"); 58 59 webactivity.setName(name); 60 webactivity.setIDnumber(IDnumber); 61 webactivity.setSex(sex); 62 webactivity.setMajor(major); 63 64 65 66 if("add".equals(method)) { 67 if(Servicepeople.add(webactivity)) { 68 System.out.print("添加成功"); 69 List<studentInfo> activity11=Servicepeople.list(); 70 request.setAttribute("activity11",activity11); 71 request.setAttribute("message", "添加成功!!!"); 72 request.getRequestDispatcher("managener.jsp").forward(request,response); 73 } 74 else { 75 System.out.println("添加失败"); 76 request.setAttribute("message", "添加失败,该用户已存在!!"); 77 request.getRequestDispatcher("managener.jsp").forward(request,response); 78 } 79 } 80 81 82 83 84 85 86 if("search".equals(method)) { 87 ArrayList<studentInfo> activity1=Servicepeople.search(webactivity); 88 if(activity1!=null) { 89 //request.setAttribute("message", "查询成功"); 90 //System.out.print("查询成功"); 91 request.setAttribute("activity11", activity1); 92 request.getRequestDispatcher("searchshow.jsp").forward(request,response); 93 } 94 else { 95 request.setAttribute("message", "查询失败"); 96 //System.out.print("查询失败"); 97 request.getRequestDispatcher("search.jsp").forward(request,response); 98 } 99 } 100 if("show".equals(method)) { 101 List<studentInfo> activity11=Servicepeople.list(); 102 request.setAttribute("activity11",activity11); 103 request.getRequestDispatcher("show.jsp").forward(request,response); 104 } 105 } 106 107 108 }
明天计划:
明天开始练习前端页面