package servlets; import java.io.IOException; import java.util.ArrayList; 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 student.bean.studentInfo; /** * Servlet implementation class studentController */ @WebServlet("/studentController") public class studentController extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public studentController() { 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()); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("utf-8"); String actionUrl = request.getServletPath(); // 获取客户请求的Servlet地址 if (actionUrl.equals("/index.do")) { // 查询所有图书 ArrayList<studentInfo> list = studentInfo.getstudentList(); // 调用studentInfo的getBookList方法查询所有图书,赋值给list request.setAttribute("list", list); // 在request增加属性list,其结果为list对象 request.getRequestDispatcher("/index.jsp").forward(request, response);// 重定向至index.jsp进行显示 } else if (actionUrl.equals("/addview.do")) { // 新增图书显示页面 request.getRequestDispatcher("add.html").forward(request, response); } else if (actionUrl.equals("/add.do")) { // 新增图书 studentInfo bi = new studentInfo(); bi.setName(request.getParameter("Name")); bi.setSex(request.getParameter("Sex")); bi.setBirth(request.getParameter("Birth")); int r = studentInfo.addstudent(bi); // 调用studentInfor的addstudent方法完成 if (r == 1) request.getRequestDispatcher("success.html").forward(request, response); // 成功的话重定向至success.html else request.getRequestDispatcher("failure.html").forward(request, response); // 失败的话重定向至failure.html } else if (actionUrl.equals("/edit.do")) { // 客户端要对指定id的图书进行修改 String id = request.getParameter("id"); studentInfo bi = studentInfo.getstudentById(id); // 调用studentInfo的getstudentById方法获取图书信息,赋值给bi对象 request.setAttribute("bi", bi); // 将bi对象增加到request的属性中 request.getRequestDispatcher("/edit.jsp").forward(request, response);// 重定向至edit.jsp进行显示 } else if (actionUrl.equals("/update.do")) { // 用户输入要修改的图书的信息之后需要保存到数据库 studentInfo bi = new studentInfo(); bi.setId(request.getParameter("id")); bi.setName(request.getParameter("name")); bi.setSex(request.getParameter("sex")); bi.setBirth(request.getParameter("birth")); int r = studentInfo.updatestudent(bi);// 调用studentInfo的updatestudent方法实现 if (r == 1) request.getRequestDispatcher("/success.html").forward(request, response);// 成功的话重定向至success.html else request.getRequestDispatcher("/failure.html").forward(request, response);// 失败的话重定向至failure.html } else if (actionUrl.equals("/delete.do")) { // 用户需要删除指定id的图书 String id = request.getParameter("id"); int r = studentInfo.deletestudent(id); // 调用studentInfo的deletestudent方法实现 if (r == 1) request.getRequestDispatcher("/success.html").forward(request, response);// 成功的话重定向至success.html else request.getRequestDispatcher("/failure.html").forward(request, response);// 失败的话重定向至failure.html } } }
标签:总结,26,studentInfo,request,bi,getParameter,html,2023.5,response From: https://www.cnblogs.com/wllovelmbforever/p/17435677.html