谁能教我做个简单的购物袋,只要单击一个物品就跳到购物袋中去,先谢了

解决方案 »

  1.   

    package com.ebook.servlet;import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.*;
    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.ebook.bean.*;
    public class ViewCart extends HttpServlet { /**
     * 
     */
    private static final long serialVersionUID = 1434458861240420653L;
    /**
     * Destruction of the servlet. <br>
     */
    int count=1;
    public void destroy() {
    super.destroy(); // Just puts "destroy" string in log
    // Put your code here
    } /**
     * 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 {
    HttpSession session=request.getSession(false);
    Bookbean book=(Bookbean)session.getAttribute("book");
    Map map=new HashMap();
    request.setCharacterEncoding("GBK");
    response.setCharacterEncoding("GBK");
    if(session.getAttribute("map")==null)
    {
    Cartbean cart=new Cartbean();
    cart.setBook(book);
    cart.setCount(1);
    map.put(book.getIsbn(),cart);
    session.setAttribute("map", map);
    response.sendRedirect("/ebokk/html/viewcart.jsp");
    //request.getRequestDispatcher("/html/viewcart.jsp").forward(request, response);
    }
    else
    {
    String str=null;
    map=(HashMap)session.getAttribute("map");
    Set set= map.keySet();
    Iterator it=set.iterator();
    while(it.hasNext())
    {
    str=(String)it.next();
    if(book.getIsbn().equals(str))
    {
    break;
    }

    }
    if(book.getIsbn().equals(str))
    {
    count++;
    Cartbean cart=new Cartbean();
    cart.setBook(book);
    cart.setCount(count);
    map.put(book.getIsbn(),cart);
    session.setAttribute("map",map);
    response.sendRedirect("/ebokk/html/viewcart.jsp");
    }
    else
    {
    Cartbean cart=new Cartbean();
    cart.setBook(book);
    cart.setCount(1);
    map.put(book.getIsbn(),cart);
    session.setAttribute("map", map);
    response.sendRedirect("/ebokk/html/viewcart.jsp");
    }
    }
    } /**
     * The doPost method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to post.
     * 
     * @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 doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    doGet(request,response);
    } /**
     * Initialization of the servlet. <br>
     *
     * @throws ServletException if an error occure
     */
    public void init() throws ServletException {
    // Put your code here
    }}bookbean:package com.ebook.bean;public class Bookbean {
    private String isbn;
    private String title;
    private int editionNumber;
    private String copyrigtht;
    private int publisherid;
    private String imgfile;
    private double price;
    public String getCopyrigtht() {
    return copyrigtht;
    }
    public void setCopyrigtht(String copyrigtht) {
    this.copyrigtht = copyrigtht;
    }
    public int getEditionNumber() {
    return editionNumber;
    }
    public void setEditionNumber(int editionNumber) {
    this.editionNumber = editionNumber;
    }
    public String getImgfile() {
    return imgfile;
    }
    public void setImgfile(String imgfile) {
    this.imgfile = imgfile;
    }
    public String getIsbn() {
    return isbn;
    }
    public void setIsbn(String isbn) {
    this.isbn = isbn;
    }
    public double getPrice() {
    return price;
    }
    public void setPrice(double price) {
    this.price = price;
    }
    public int getPublisherid() {
    return publisherid;
    }
    public void setPublisherid(int publisherid) {
    this.publisherid = publisherid;
    }
    public String getTitle() {
    return title;
    }
    public void setTitle(String title) {
    this.title = title;
    }
    }购物车bean:
    package com.ebook.bean;
    public class Cartbean {
    private Bookbean book;
    private int count;
    public Bookbean getBook() {
    return book;
    }
    public Cartbean(Bookbean book,int count)
    {}
    public Cartbean()
    {}
    public void setBook(Bookbean book) {
    this.book = book;
    }
    public int getCount() {
    return count;
    }
    public void setCount(int count) {
    this.count = count;
    }
    }
      

  2.   

    请问private String isbn;这属性是指什么?楼上的朋友
      

  3.   

     
    private String isbn; //书本编号
    private String title; //书的名字
    private int editionNumber; //作者ID
    private String copyrigtht; //版权
    private int publisherid; //出版ID
    private String imgfile; //图片地址
    private double price; //价格
      

  4.   

    对了,你这运行过是正确的吗?我放在SERVLET中有错,在购物袋页面怎么读取呢?
      

  5.   

    你QQ多少,我可以加你吗,我的QQ是547568327