项目要求
package packg.cn1; import java.io.IOException; import java.io.PrintWriter; 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 packag.cn.User; /** * Servlet implementation class LoginServlet */ @WebServlet("/LoginServlet") public class LoginServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public LoginServlet() { 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()); response.setContentType("text/html;charset=UTF-8"); //request.setCharacterEncoding("utf-8"); //获取页面元素内容 String userName = request.getParameter("username"); String password = request.getParameter("password"); String checkCode = request.getParameter("check_code"); String savedCode = (String) request.getSession().getAttribute("check_code"); PrintWriter pw = response.getWriter(); if (("xyz").equals(userName) && ("123456").equals(password) && checkCode.equals(savedCode)) { User user = new User(); user.setUserNname(userName); user.setPassword(password); request.getSession().setAttribute("user",user); response.sendRedirect("IndexServlet"); }else if(checkCode.equals(savedCode)){ pw.write("用户名或密码错误,登录失败"); response.getWriter().print("您还没有登录,请登录"); }else { pw.write("验证码错误"); } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
package packg.cn1; import java.io.FileNotFoundException; import java.io.IOException; import java.util.ArrayList; import java.util.Iterator; import java.util.Map; 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 javax.servlet.http.HttpSession; import dao.BookDB; import oracle.jrockit.jfr.openmbean.ProducerDescriptorType; import packag.cn.Cart; import packag.cn.Book; import service.BusinessService; /** * Servlet implementation class DeletServlet * @param* @param*/ @WebServlet("/DeleteServlet") public class DeleteServletextends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public DeleteServlet() { 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()); //获取得到用户想要删除哪个书本的id String id = request.getParameter("bookid"); //获取该用户相对应的购物车对象 Cart cart = (Cart) request.getSession().getAttribute("cart"); try { //删除购物车的商品,也应该是在BusinessService中有的功能,于是乎又回到BusinessService中写代码 BusinessService businessService = new BusinessService(); // businessService.deleteBook(id, cart); businessService.deleteBook(id, cart); //删除购物车的商品后,也应该直接跳转回去购物车的显示页面中 request.getRequestDispatcher("listCart.jsp").forward(request, response); } catch (FileNotFoundException e) { request.setAttribute("message", "购物车空了!"); request.getRequestDispatcher("/listBook.jsp").forward(request, response); } catch (Exception e) { e.printStackTrace(); request.setAttribute("message", "删除中出现了异常~待会再试试呗!"); request.getRequestDispatcher("listCart.jsp").forward(request, response); } //调用购物车删除方法 // if (id == null) { // String url = "Servlet"; // response.sendRedirect(url); // return; // } // //Book book = BookDB.getBook(id); // Book book = (Book) BookDB.getAll(); // HttpSession session = request.getSession(); // //List cart = (List) session.getSession(); // @SuppressWarnings({"unchecked","rawtyoes"}) // //List cart = (List) session.getAttribute("cart"); // //ArrayListcart = (ArrayList) session.getAttribute("cart"); // ArrayListcart1 = (ArrayList) session.getAttribute("cart"); // cart1.remove(book); // // String url = "CartServlet"; // // response.sendRedirect(url); // String newurl = response.encodeRedirectUrl(url); // response.sendRedirect(newurl); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }