@WebServlet("/admin/addBuyCourse") public class AddBuyCourseController extends HttpServlet { private static final long serialVersionUID = 1L; public AddBuyCourseController() { super(); // TODO Auto-generated constructor stub } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub String isbn = request.getParameter("isbn"); String bookName = request.getParameter("bookName"); User user = (User) request.getSession().getAttribute("loginUser"); Date createTime = new Date(); StudentBuy buy = new StudentBuy(); buy.setIsbn(isbn); buy.setBookName(bookName); String studentNo = user.getUsername(); String username = user.getDisplayName(); buy.setStudentNo(studentNo); buy.setStudentName(username); buy.setCreateTime(createTime); CoursesService courseService = new CoursesService(); ScoreService scoreService = new ScoreService(); Score query = scoreService.getISBN(isbn); if(query != null) { request.setAttribute("msg", "isbn为"+isbn+"的教材已经存在"); request.getRequestDispatcher("/WEB-INF/pages/error.jsp").forward(request, response); return; } // courseService.addStudentBuy(buy); request.getRequestDispatcher("/coursesUrl").forward(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
时间: 2024-04-21 09:27:05 浏览: 91
基于javaweb上机报告.doc
这是一个Java Servlet类,用于处理添加用户购买课程的请求。它接收来自客户端的HTTP请求,并从请求中获取必要的参数,如书的ISBN、书名等。接着,它从HTTP会话中获取当前登录用户的信息,并将这些信息保存到一个StudentBuy对象中。然后,它通过调用CoursesService类的addStudentBuy()方法将StudentBuy对象保存到数据库中。如果ISBN已经存在,则会将错误消息设置为请求属性,并将请求转发到错误页面。最后,它将请求转发到另一个URL,以便显示用户购买的课程。
阅读全文