今日进行了web实验。
体验了新的增删改查的书写方式。
package servlets; import java.io.IOException; import java.util.*; import jakarta.servlet.ServletException; import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import org.json.*; import book.Book; @WebServlet("*.action") public class AjaxController extends HttpServlet { private static final long serialVersionUID = 1L; public AjaxController() { super(); // TODO Auto-generated constructor stub } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); String actionUrl = request.getServletPath(); // 获取客户端的访问URL地址信息 if (actionUrl.equals("/list.action")) { // 查询所有图书 ArrayList<Book> list = Book.getBookList(); // 调用BookInfo的getBookList方法完成 // 使用JSONArray对象将结果构建为json对象并输出到客户端 JSONArray jsonArray = new JSONArray(); for (int i = 0; i < list.size(); i++) { Book book = list.get(i); Map<String, Object> map = new HashMap<String, Object>(); map.put("id", book.getId()); map.put("bookname", book.getBookname()); map.put("author", book.getAuthor()); map.put("price", book.getPrice()); JSONObject BookObj = new JSONObject(map); jsonArray.put(BookObj); } // 向客户端返回json结果 response.getWriter().print(jsonArray.toString()); } else if (actionUrl.equals("/add.action")) { // 增加图书操作 Book bi = new Book(); bi.setBookname(request.getParameter("bookname")); bi.setAuthor(request.getParameter("author")); bi.setPrice(request.getParameter("price")); int r = Book.addBook(bi); // 调用BookInfo的addBook方法完成 // 向客户端返回结果 response.getWriter().print(r); } else if (actionUrl.equals("/edit.action")) { // 编辑图书操作 String id = request.getParameter("id"); Book bi = Book.getBookById(id); // 调用BookInfo的getBookById方法完成 // 将该对象构建为json数据 Map<String, Object> map = new HashMap<String, Object>(); map.put("id", bi.getId()); map.put("bookname", bi.getBookname()); map.put("author", bi.getAuthor()); map.put("price", bi.getPrice()); JSONObject BookObj = new JSONObject(map); // 向客户端返回结果 response.getWriter().print(BookObj.toString()); } else if (actionUrl.equals("/update.action")) { // 更新图书操作 Book bi = new Book(); bi.setId(request.getParameter("id")); bi.setBookname(request.getParameter("bookname")); bi.setAuthor(request.getParameter("author")); bi.setPrice(request.getParameter("price")); int r = Book.updateBook(bi);// 调用BookInfo的updateBook方法完成 response.getWriter().print(r); // 向客户端返回结果 } else if (actionUrl.equals("/delete.action")) { // 删除图书操作 String id = request.getParameter("id"); int r = Book.deleteBook(id); // 调用BookInfo的deleteBook方法完成 response.getWriter().print(r); // 向客户端返回结果 } } }
标签:总结,map,每日,request,bi,getParameter,Book,5.19,response From: https://www.cnblogs.com/syhxx/p/17416147.html