<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%> <html> <body> <% ArrayList books = new ArrayList(); books.add("三国演义"); books.add("西游记"); books.add("水浒传"); //将books放进session 名字为book session.setAttribute("book",books); %> <a href=MyJsp2.jsp>查看购物车</a> </body> </html> <%@ page language="java" import="java.util.*" pageEncoding="gb2312"%> <html> <body> 购物车内容为: <hr> <% //从session中取出名为book的 放入数组中 ArrayList books = (ArrayList)session.getAttribute("book"); for(int i = 0;i<books.size();i++) { String book = (String)books.get(i); out.println(book + "<BR>"); } %> </body>
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%> <html> <body> <form action = "MyJsp3.jsp" method = "post"> 请你输入书本:<input name ="book", type = "text"> <input type = "submit" value = " 添加到购物车"> </form> <hr> <% //取出名为books的 ArrayList book = (ArrayList)session.getAttribute("books"); if(book==null) //如果没有 也就是第一次 { out.print("wzz"); book = new ArrayList(); session.setAttribute("books",book); } String book1 = request.getParameter("book"); if(book1!=null) { book1 = new String(book1.getBytes("ISO-8859-1")); book.add(book1); } %> 购物车里的内容:<br> <% for(int i =0;i<book.size();i++) { //从数组里取出 out.println(book.get(i)+"<BR>"); } %> </body> </html>
购物车一
时间: 2024-10-12 15:26:20