package bean; public class studentInfo { private String IDnumber; private String name; private String socre; private String classroom; private String major; public String getIDnumber() { return IDnumber; } public void setIDnumber(String iDnumber) { IDnumber = iDnumber; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getClassroom() { return classroom; } public void setClassroom(String classroom) { this.classroom = classroom; } public String getMajor() { return major; } public void setMajor(String major) { this.major = major; } public studentInfo() { } public String getSocre() { return socre; } public void setSocre(String socre) { this.socre = socre; } public studentInfo(String iDnumber, String name, String socre, String classroom, String major) { super(); IDnumber = iDnumber; this.name = name; this.socre = socre; this.classroom = classroom; this.major = major; } public void setstudentInfo(String iDnumber, String name, String socre, String classroom, String major) { IDnumber = iDnumber; this.name = name; this.socre = socre; this.classroom = classroom; this.major = major; } @Override public String toString() { return "student [IDnumber=" + IDnumber + ", name=" + name + ", socre=" + socre + ", classroom=" + classroom + ", major=" + major + ", getIDnumber()=" + getIDnumber() + ", getName()=" + getName() + ", getClassroom()=" + getClassroom() + ", getMajor()=" + getMajor() + ", getSocre()=" + getSocre() + "]"; } }
servlet层——servlet.java
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 bean.studentInfo; import bean.dao; import bean.DButil; @WebServlet("/servletpeople") public class servletpeople extends HttpServlet{ private static final long serialVersionUID = 1L; dao dao=new dao(); protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); String method = req.getParameter("method"); if ("add".equals(method)) { add(req, resp); } else if ("delete".equals(method)) { delete(req, resp); } else if ("search".equals(method)) { search(req, resp); } else if ("list".equals(method)) { list(req, resp); } else if("getbyname".equals(method)) { getbyname(req,resp); }else if("searchxiugai".equals(method)) { searchxiugai(req,resp); } } private void getbyname(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); String name = req.getParameter("name"); studentInfo ten = dao.getbyname(name); if(ten==null) { req.setAttribute("message", "未找到该学生"); req.getRequestDispatcher("deletepeople.jsp").forward(req, resp); } else { req.setAttribute("ten", ten); req.getRequestDispatcher("servletpeople?method=delete").forward(req,resp); } } private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { req.setCharacterEncoding("utf-8"); String IDnumber=req.getParameter("IDnumber"); String Name = req.getParameter("name"); String major = req.getParameter("major"); String score = req.getParameter("score"); String classroom=req.getParameter("classroom"); studentInfo stu=new studentInfo(IDnumber,Name,score,classroom,major); if(dao.add(stu)) { req.setAttribute("message", "添加成功"); req.getRequestDispatcher("servletpeople?method=list").forward(req,resp); } else { req.setAttribute("message", "姓名重复,请重新录入"); req.getRequestDispatcher("addpeople.jsp").forward(req,resp); } } private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); List<studentInfo> tens = dao.list(); req.setAttribute("tens", tens); req.getRequestDispatcher("list.jsp").forward(req,resp); } private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); String name=req.getParameter("name"); if(dao.delete(name)) { req.setAttribute("message", "删除成功"); req.getRequestDispatcher("servletpeople?method=list").forward(req,resp); }else { req.setAttribute("message", "删除失败"); req.getRequestDispatcher("deletepeople.jsp").forward(req,resp); } } //查找 private void search(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); String IDnumber = req.getParameter("IDnumber"); String name=req.getParameter("name"); List<studentInfo> tens = dao.search(IDnumber,name); if(tens==null) { req.setAttribute("message", "没有该同学"); req.getRequestDispatcher("searchpeople.jsp").forward(req,resp); } else { req.setAttribute("tens", tens); req.getRequestDispatcher("list2.jsp").forward(req,resp); } } //查找要修改的人员 private void searchxiugai(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); String IDnumber=req.getParameter("IDnumber"); String name=req.getParameter("name"); String score=req.getParameter("score"); String classroom=req.getParameter("classroom"); String major=req.getParameter("major"); studentInfo stu = new studentInfo(IDnumber,name,score,classroom,major); boolean ten=dao.update(stu); studentInfo tens = dao.getbyname(name); if(ten==false) { req.setAttribute("message", "没有该同学"); req.getRequestDispatcher("xiugai.jsp").forward(req,resp); } else { System.out.println(tens.getSocre()); req.setAttribute("tens", tens); req.getRequestDispatcher("list2.jsp").forward(req,resp); } } private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); String IDnumber=req.getParameter("IDnumber"); String name=req.getParameter("name"); String score=req.getParameter("score"); String classroom=req.getParameter("classroom"); String major=req.getParameter("major"); studentInfo student=new studentInfo(IDnumber,name,score,classroom,major); studentInfo stu = dao.getbyname(name); boolean ten=dao.update(student); List<studentInfo> tens = dao.search(stu.getIDnumber(),stu.getName()); if(ten==true) { req.getRequestDispatcher("list2.jsp").forward(req,resp); } else { req.setAttribute("tens", tens); req.setAttribute("message", "修改失败,请重新修改"); req.getRequestDispatcher("list.jsp").forward(req,resp); } } }
明日计划
练习前端界面的设计