Servlet购物车源代码

连接数据库,取出需要的值

package com.servlet.dao.imp;

import java.sql.SQLException;

import java.util.ArrayList;
import java.util.List;
import com.servlet.bean.BookBean;
import com.servlet.dao.BookDao;
import com.servlet.uitl.DBHelper;

public class BookDaoImp extends DBHelper implements BookDao{

    @Override
    public List getBooks() {
        List list = new ArrayList();
        this.conn = this.open();
        String sql = "select id,name,unitPrice,photo from book";
        try {
            this.ps = this.conn.prepareStatement(sql);
            this.rs = this.ps.executeQuery();
            while(this.rs.next()){
                BookBean book = new BookBean();
                book.setId(rs.getInt("id"));
                book.setName(rs.getString("name"));
                book.setUnitPrice(rs.getInt("unitPrice"));
                book.setPhoto(rs.getString("photo"));
                list.add(book);
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return list;
    }

    public BookBean getBookById(int id){
        BookBean  book = new BookBean();
        this.conn = this.open();
        String sql = "select name,unitPrice,photo from book where id="+id;
        try {
            this.ps = this.conn.prepareStatement(sql);
            this.rs = this.ps.executeQuery();
            while(this.rs.next()){
                book.setName(rs.getString("name"));
                book.setUnitPrice(rs.getInt("unitPrice"));
                book.setPhoto(rs.getString("photo"));
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return book;
    }

}

ShowServlet类显示购买页面

package com.homework;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
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 com.servlet.bean.BookBean;
import com.servlet.server.imp.BookServerImp;

/**
 * Servlet implementation class Show
 */
@WebServlet("/show")
public class ShowServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ShowServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        BookServerImp bookServerImp = new BookServerImp();
        List list = bookServerImp.getBooks();
        out.print("");
        out.print("");
        out.print("");
        out.print("");
        for(BookBean l : list){
            out.print("" +
                    "");
        }
        out.print("
封面书名单价操作
"+l.getName()+""+l.getUnitPrice()+"" +
                    "购买
");
        out.print("");
        out.print("");
    }

}

ShopingServlet类添加购买的书籍,map存储加入到购物车中的书籍ID

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.servlet.bean.BookBean;
import com.servlet.server.imp.BookServerImp;

/**
 * Servlet implementation class Shoping
 */
@WebServlet("/shop")
public class ShopingServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ShopingServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        String id = request.getParameter("id");
        HttpSession session = request.getSession();
        BookBean book = null;
        BookServerImp bookServerImp = new BookServerImp();
        Map map = (Map) session.getAttribute("car");
        if(map==null){
            map = new HashMap();
            map.put(id, 1);
        }else{
            if(map.containsKey(id)){
                map.put(id, map.get(id)+1);
            }else{
                map.put(id, 1);
            }
        }
        session.setAttribute("car", map);
        Set set = map.keySet();
        out.print("");
        out.print("");
        out.print("返回
");
        out.print("
");
        out.print("");
        Cookie cookie = new Cookie("shopId",session.getId());
        cookie.setMaxAge(3600);
        response.addCookie(cookie);
        int count = 0;
        for(String bookid:set){
            book = bookServerImp.getBookById(Integer.parseInt(bookid));
            count = map.get(bookid)*book.getUnitPrice();
            out.print("");
        }
        out.print("
封面书名单价数量总价
"+book.getName()+""+book.getUnitPrice()+""+map.get(bookid)+""+count+"
");
        out.print("");
        out.print("");
        }
}


你可能感兴趣的:(Servlet购物车源代码)