Java servlet 实现的简易购物车

时间:2024-09-26 22:03:44

首页

Java servlet 实现的简易购物车

2.购买页

Java servlet 实现的简易购物车

3.购物车页

Java servlet 实现的简易购物车

1. 首页代码 发送一个post请求

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<img src="/img/bookcover/shoping.png" alt="购物">
<form action="goshop.do" method="post">
<p>欢迎您</p><br>
<input type="submit" value="进入购物">
</form>
</body>
</html>
2.处理首页post请求的controler
package cc.openhome.controler;
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 java.io.IOException;
import java.util.LinkedHashMap; @WebServlet("/goshop.do")
public class GoShop extends HttpServlet { void processRequest(HttpServletRequest req,HttpServletResponse resp)
throws ServletException,IOException
{
BookDb bs = new BookDb();
LinkedHashMap<String,Book> books = bs.getAll();
req.setAttribute("books",books);
req.getRequestDispatcher("shop.view").forward(req,resp); } @Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
processRequest(req,resp); } @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
processRequest(req,resp);
}
}
3 ,展示购物页面
package cc.openhome.view;

import cc.openhome.controler.*;
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 java.io.IOException;
import java.io.PrintWriter;
import java.util.LinkedHashMap; @WebServlet("/shop.view")
public class ShopView extends HttpServlet { protected void processRequest(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=UTF-8"); String bookid = req.getParameter("bookid");
int count = 0;
HttpSession session = req.getSession(); // 在点击了购买之后书的数量在加一,否则只刷新
if(session.getAttribute("count") != null && bookid != null){
Integer c = (Integer) session.getAttribute("count");
count = c + 1;
}
session.setAttribute("count",count); //购物
Cart cart = (Cart) req.getSession().getAttribute("cart");
if(cart == null){
cart = new Cart();
req.getSession().setAttribute("cart",cart);
} BusinessService bussiness = new BusinessService();
if(count > 0){
bussiness.buyBook(bookid,cart);
}
session.setAttribute("cart",cart); PrintWriter out = resp.getWriter();
out.println("<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01'" +
"Transitional//EN");
out.println("<html>");
out.println("<head>");
out.println("<title>购物网页</title>");
out.println("</head>");
out.println("<body>");
out.println("<form action='' method='post'>");
out.println("<table width='100%'>");
out.println("<td><img src='img/bookcover/shop.png' alt='小购物车' width='50'>" +
"<a href='cart.view'>已采购"+count+"书籍</a><td>");
out.println("</table>");
out.println("<table width='100%'>");
out.println("<tr>");
out.println("<td style='color:blue'>书籍编号<td>");
out.println("<td style='color:blue'>书名<td>");
out.println("<td style='color:blue'>图片</td>");
out.println("<td style='color:blue'>价格<td>");
out.println("<td style='color:blue'>购买<td>");
out.println("</tr>");
int i = 0;
LinkedHashMap <String,Book> books = (LinkedHashMap <String,Book>) req.getAttribute("books");
for (String value : books.keySet()){
i++;
Book book = books.get(value);
out.println("<tr>");
out.println("<td>"+book.getId()+"<td>");
out.println("<td>"+book.getName()+"<td>");
out.println("<td><img src='/img/bookcover/book"+i+".jpeg'></td>");
out.println("<td>"+book.getPrice()+"<td>");
out.println("<td><a href='"+resp.encodeURL("goshop.do")+"?bookid="+book.getId()+"'>购买此书</a><td>");
out.println("<tr>"); }
/*
int i,j = 0,k = 0;
int len = books.size();
for(i = 0;i < len; i++,j++,k++){
if(j % 3 == 0){
out.println("<tr>");
}
out.println("<td>");
out.println("<img src='/img/bookcover/book"+i+".jpeg'>");
out.println("</td>");
if(k % 3 == 0){
out.println("</tr>");
}
}
j = 0;
k = 0;
for( i = 0;i < len; i++,j++,k++){
if(j % 3 == 0){
out.println("<tr>");
}
out.println("<td>");
out.println("<a href='#'>购买此书</a>");
out.println("</td>");
if(k % 3 == 0){
out.println("</tr>");
}
}
*/
out.println("</table>");
out.println("</form>");
out.println("</body>");
out.println("</html>");
out.close();
} @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
processRequest(req, resp);
} @Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
processRequest(req, resp);
}
}
4.购物车页面
package cc.openhome.controler;

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 java.io.IOException;
import java.io.PrintWriter;
import java.util.Map; @WebServlet("/cart.view")
public class CartShow extends HttpServlet {
public void processRequest(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
resp.setContentType("text/html;charset=UTF-8");
HttpSession session = req.getSession();
Cart carts = (Cart) session.getAttribute("cart"); PrintWriter out = resp.getWriter();
out.println("<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01'" +
"Transitional//EN");
out.println("<html>");
out.println("<head>");
out.println("<title>购物结算</title>");
out.println("</head>");
out.println("<body>");
out.println("<form action='' method='post'>");
out.println("<table width='100%'>");
out.println("<td><img src='img/bookcover/shoping.png' alt='购物车' width='100'>");
out.println("</table>");
out.println("<table width='100%'>");
out.println("<tr>");
out.println("<td style='color:blue'>书籍编号<td>");
out.println("<td style='color:blue'>书名<td>");
out.println("<td style='color:blue'>图片</td>");
out.println("<td style='color:blue'>单价<td>");
out.println("<td style='color:blue'>数量<td>");
out.println("<td style='color:blue'>单本书总价<td>");
out.println("</tr>");
int i = 0;
Map<String,CartItem> bookmap = carts.getBookMap();
for (String value : bookmap.keySet()){
i++;
CartItem citem = bookmap.get(value);
Book bk = citem.getBook(); out.println("<tr>");
out.println("<td>"+bk.getId()+"<td>");
out.println("<td>"+bk.getName()+"<td>");
out.println("<td><img src='/img/bookcover/book"+i+".jpeg'></td>");
out.println("<td>"+bk.getPrice()+"<td>");
out.println("<td>"+citem.getQuantity()+"<td>");
out.println("<td>"+citem.getPrice()+"<td>");
out.println("</tr>");
}
out.println("<tr>");
out.println("<td style='color:green';font-size:200%'>总价</td>");
out.println("<td style='color:green';font-size:200%'> "+carts.getPrice()+"</td>");
out.println("</tr>");
out.println("</table>");
out.println("</form>");
out.println("</body>");
out.println("</html>");
out.close(); } @Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
processRequest(req, resp);
} @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
processRequest(req, resp);
}
}
5,书籍类
package cc.openhome.controler;

import java.util.ArrayList;

public class Book {
private String id;
private String name;
private double price;
public Book(){ }
public Book(String id,String name, double price){
this.id = id;
this.name = name;
this.price = price;
} public String getId() {
return id;
} public String getName() {
return name;
} public double getPrice() {
return price;
}
}
6,单个购物车类
package cc.openhome.controler;

public class CartItem {
private Book book;
private int quantity;
private double price; public double getPrice() {
return book.getPrice() * this.quantity;
} public Book getBook() {
return book;
} public void setBook(Book book) {
this.book = book;
} public int getQuantity() {
return quantity;
} public void setQuantity(int quantity) {
this.quantity = quantity;
} public void setPrice(double price) {
this.price = price;
}
}
7,购物车类
package cc.openhome.controler;

import java.util.LinkedHashMap;
import java.util.Map; public class Cart {
private Map<String,CartItem> bookMap = new LinkedHashMap<>(); private double price; public void addBook(Book book){
CartItem cartItem = bookMap.get(book.getId()); if(cartItem == null){
cartItem = new CartItem();
cartItem.setBook(book);
cartItem.setQuantity(1); bookMap.put(book.getId(),cartItem);
}else {
cartItem.setQuantity(cartItem.getQuantity() + 1);
}
} public double getPrice() {
double totalPrice = 0;
for(Map.Entry<String,CartItem> me : bookMap.entrySet()){
CartItem cartItem = me.getValue(); totalPrice += cartItem.getPrice();
}
return totalPrice;
} public Map<String, CartItem> getBookMap() {
return bookMap;
} public void setBookMap(Map<String, CartItem> bookMap) {
this.bookMap = bookMap;
} public void setPrice(double price) {
this.price = price;
}
}
8图书数据源
package cc.openhome.controler;

import java.util.LinkedHashMap;

public class BookDb {
private LinkedHashMap<String,Book> map = new LinkedHashMap<>(); public BookDb(){
map.put("1",new Book("1","java编程",10.00));
map.put("2",new Book("2","C++编程",10.00));
map.put("3",new Book("3","C编程",10.00));
} public LinkedHashMap<String,Book> getAll(){
return map;
}
public Book findBook(String id){
return map.get(id);
}
}
总结:对于个人而言:难点在于
1.页面数据的展示.
2.使用resp.encodeurl 在本页面刷新.
3.使用<a href> 重定向并且带了一个参数,把书的id发送给本页面处理.
4.http session 来存储发送数据.
5.书籍,购物车类的设计.