<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'FalseLogin.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> This is my JSP page. <br> <hr/><div align="center"><font size="6"> </font><font size="6" color="#ff8040"> 尊敬的用户请登录之后在进行购买操作!</font> </div> <br/> <div align="center"> <a href="Login.jsp">登陆</a> </div></body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <%@ page import="com.Bookinfo.web.util.Book" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'Continue.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> This is my JSP page. <br> <hr/><div align="center"> 尊敬的用户:<font color="#ff8040"><%=session.getAttribute("LoginName") %></font>,你已经成功购买此书! <%! double num=0; String bookName; %> <% List<Book> listBook=(ArrayList)session.getAttribute("CartMessage"); num=0; for(int i=0;i<listBook.size();i++){ Book b=listBook.get(i); num=b.getPrice(); bookName=b.getName(); %> <%} %> 您购买的这本书的名字是:<%=bookName %> 您购买的这本书的单价是:<%=num %> </div> <br/> <div align="center"><a href="BookTable.jsp">继续购买</a> <a href="index.jsp">返回首页</a></div> </body> </html>
package com.Bookinfo.Servlet.util; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class CancleServlet extends HttpServlet { /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); request.getSession().removeAttribute("LoginName"); response.sendRedirect("index.jsp"); } }
package com.Bookinfo.Servlet.util; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import com.Bookinfo.BFace.util.CartIface; import com.Bookinfo.BImple.util.CartImple; import com.Bookinfo.web.util.Book; public class CartServlet extends HttpServlet { /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); HttpSession sess=request.getSession(); //判断登录名是否为空 if(sess.getAttribute("LoginName")==null){ response.sendRedirect("FalseLogin.jsp"); }else{ String bookId=request.getParameter("bookId");//得到用户购买的图书的id //将用户的图书的id存放到购物车中去中 List<Integer> cart=(List<Integer>)sess.getAttribute("cartFrist"); //将图书id添加进去 cart.add(Integer.parseInt(bookId)); //得到购物车里面的信息 CartIface ciface=new CartImple(); List<Integer> listCartID=(List<Integer>) sess.getAttribute("cartFrist"); List<Book> listbook=new ArrayList<Book>(); for (int i = 0; i <listCartID.size(); i++) { Book bo=ciface.getBookById(listCartID.get(i)); listbook.add(bo); } sess.setAttribute("CartMessage",listbook); response.sendRedirect("Continue.jsp"); } } }
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <%@ page import="com.Bookinfo.web.util.Book" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <%! int sumBook=0; %> <%if(session.getAttribute("LoginName")!=null){ %> <%=session.getAttribute("LoginName") %> 购物车里面有 <% List<Book> listBook=(ArrayList)session.getAttribute("cartFrist"); sumBook=listBook.size(); %> <%=sumBook %>件商品 <% }else{ %> <a href="Login.jsp">登陆</a> <% } %> <a href="Cancle">退出</a> <a href="bookTable">查看信息</a> <a href="CartMessageServlet">查看购物车</a> </body> </html>