Java servlet 实现的简易购物车
2018-11-22 08:43:33来源:博客园 阅读 ()
首页
2.购买页
3.购物车页
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.书籍,购物车类的设计.
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- 国外程序员整理的Java资源大全(全部是干货) 2020-06-12
- 2020年深圳中国平安各部门Java中级面试真题合集(附答案) 2020-06-11
- 2020年java就业前景 2020-06-11
- 04.Java基础语法 2020-06-11
- DES/3DES/AES 三种对称加密算法实现 2020-06-11
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash