javaweb实验五
product类:
package com.lab;
public class Product {
private int id; // 商品编号
private String name; // 商品名称
private String description; // 商品描述
private float price; // 商品价格
// 构造方法
public Product(int id, String name, String description, float price) {
this.id = id;
this.name = name;
this.description = description;
this.price = price;
}
// 各属性的setter方法和getter方法
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
}
showshoppingitem类:
package com.lab;
import java.io.Serializable;
public class ShoppingItem implements Serializable {
private Product product; // 商品信息
private int quantity; // 商品数量
public ShoppingItem(Product product, int quantity) {
this.product = product;
this.quantity = quantity;
}
// 属性的getter方法和setter方法
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
}
ShoppingCartServlet
package com.lab;
import com.lab.Product;
import com.lab.ShoppingItem;
import java.io.*;
import java.util.ArrayList;
//import java.awt.List;
import java.util.List;
import java.awt.Component;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.Serializable;
import javax.servlet.annotation.WebServlet;
@WebServlet(name = "ShoppingCartServlet", urlPatterns = {
"/products", "/viewProductDetails",
"/addToCart", "/viewCart", "/deleteItem" })
public class ShoppingCartServlet extends HttpServlet {
// products是存放所有商品的List对象
private List <Product> products = new ArrayList <Product>();
@Override
public void init() throws ServletException {
products.add(new Product(1, "单反相机",
"尼康性价比最高的单反相机",4159.95F));
products.add(new Product(2, "三星手机",
"三星公司的最新iPhone5产品", 1199.95F));
products.add(new Product(3, "笔记本电脑",
"联想公司的新一代产品",5129.95F));
products.add(new Product(4, "平板电脑",
"苹果公司的新产品",1239.95F));
}
@Override
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {
String uri = request.getRequestURI();
// 根据请求URI决定调用哪个方法
if (uri.endsWith("/products")) {
showProductList(response);
} else if (uri.endsWith("/viewProductDetails")) {
showProductDetails(request, response);
} else if (uri.endsWith("viewCart")) {
showCart(request, response);
} else if (uri.endsWith("deleteItem")) {
deleteItem(request, response);
}
}
// 该方法用于显示所有商品信息
private void showProductList(HttpServletResponse response)
throws IOException {
response.setContentType("text/html;charset=gb2312");
PrintWriter out = response.getWriter();
out.println("<html><head><title>商品列表</title>" +
"</head><body><p>商品列表</p>");
out.println("<ul>");
for (Product product : products) {
out.println("<li>" + product.getName() + "("
+ product.getPrice()
+ ") (" + "<a href='viewProductDetails?id="
+ product.getId() + "'>详细信息</a>)");
}
out.println("</ul>");
out.println("<a href='viewCart'>查看购物车</a>");
out.println("</body></html>");
}
private void showProductDetails(HttpServletRequest request,
HttpServletResponse response) throws IOException {
response.setContentType("text/html;charset=gb2312");
PrintWriter out = response.getWriter();
int productId = 0;
try {
productId = Integer.parseInt(
request.getParameter("id"));
} catch (NumberFormatException e){
}
// 根据商品号返回商品对象
Product product = getProduct(productId);
if (product != null) {
out.println("<html><head>"
+ "<title>商品详细信息</title></head>"
+ "<body><p>商品详细信息</p>"
+ "<form method='post' action='addToCart'>");
// 这里使用隐藏表单域存储商品号信息
out.println("<input type='hidden' name='id' "
+ "value='" + productId + "'/>");
out.println("<table>");
out.println("<tr><td>商品名:</td><td>"
+ product.getName() + "</td></tr>");
out.println("<tr><td>说明:</td><td>"
+ product.getDescription() + "</td></tr>");
out.println("<tr><td>价格:</td><td>"
+ product.getPrice() + "</td></tr>");
out.println("<tr>" + "<tr>"
+ "<td><input name='quantity'/></td>"
+ "<td><input type='submit' value='购买'/>"
+ "</td>"
+ "</tr>");
out.println("<tr><td colspan='2'>"
+ "<a href='products'>商品列表</a>"
+ "</td></tr>");
out.println("</table>");
out.println("</form></body>");
} else {
out.println("No product found");
}
}
// 根据商品号返回商品对象方法
private Product getProduct(int productId) {
for (Product product : products) {
if (product.getId() == productId) {
return product;
}
}
return null;
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
// 将购买的商品添加到购物车中
int productId = 0;
int quantity = 0;
try {
productId = Integer.parseInt(
request.getParameter("id"));
quantity = Integer.parseInt(request
.getParameter("quantity"));
} catch (NumberFormatException e) { }
Product product = getProduct(productId);
if (product != null && quantity >= 0) {
// 创建一个商品条目
ShoppingItem shoppingItem = new ShoppingItem(
product, quantity);
HttpSession session = request.getSession();
// 在会话对象中查找购物车对象
List<ShoppingItem> cart = (List<ShoppingItem>) session
.getAttribute("cart");
if (cart == null) {
// 如果在会话对象上找不到购物车对象,则创建一个
cart = new ArrayList<ShoppingItem>();
// 将购物车对象存储到会话对象上
session.setAttribute("cart", cart);
}
// 将商品添加到购物车对象中
cart.add(shoppingItem);
}
showProductList(response);
}
public void deleteItem(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
int productId = 0,i=0;
try {
productId = Integer.parseInt(
request.getParameter("id"));
} catch (NumberFormatException e) { }
HttpSession session = request.getSession();
List<ShoppingItem> cart = (List<ShoppingItem>) session.getAttribute("cart");
for (i=0;i<cart.size();i++){
ShoppingItem p=cart.get(i);
if (p.getProduct().getId()==productId)
{
cart.remove(i);
break;
}
}
response.sendRedirect("http://localhost:8080/helloapp/viewCart");
}
private void showCart(HttpServletRequest request,
HttpServletResponse response) throws IOException {
response.setContentType("text/html;charset=gb2312");
PrintWriter out = response.getWriter();
out.println("<html><head><title>购物车</title></head>");
out.println("<body><a href='products'>" +
"商品列表</a>");
HttpSession session = request.getSession();
List<ShoppingItem> cart = (List<ShoppingItem>) session
.getAttribute("cart");
if (cart != null) {
out.println("<table>");
out.println("<tr><td style='width:50px'>数量"
+ "</td>"
+ "<td style='width:80px'>商品</td>"
+ "<td style='width:80px'>价格</td>"
+ "<td style='width:80px'>小计</td>"
+ "<td style='width:80px'>是否删除</td></tr>");
double total = 0.0;
for (ShoppingItem shoppingItem : cart) {
Product product = shoppingItem.getProduct();
int quantity = shoppingItem.getQuantity();
if (quantity != 0) {
float price = product.getPrice();
out.println("<tr>");
out.println("<td>" + quantity + "</td>");
out.println("<td>" + product.getName()
+ "</td>");
out.println("<td>" + price + "</td>");
// 计算小计并实现四舍五入
double subtotal = ((int)(price * quantity*100+0.5))/100.00;
out.println("<td>" + subtotal + "</td>");
out.println("<td><a href=deleteItem?id=" +
product.getId()+">"+"删除</a>" + "</td>");
total += subtotal;
out.println("</tr>");
}
}
out.println("<tr><td colspan='4' "
+ "style='text-align:right'>"
+ "总计:" + total + "</td></tr>");
out.println("</table>");
}
out.println("</table></body></html>");
}
}
javaweb实验五的更多相关文章
- 20145215&20145307《信息安全系统设计基础》实验五 网络通信
小组成员:20145215卢肖明.20145307陈俊达 实验报告链接:信息安全系统设计基础--实验五实验报告
- 20145216 20145330 《信息安全系统设计基础》 实验五 简单嵌入式WEB 服务器实验
20145216 20145330 <信息安全系统设计基础> 实验五 简单嵌入式WEB 服务器实验 实验报告封面 实验步骤 1.阅读理解源码 进入/arm2410cl/exp/basic/ ...
- 20145208《信息安全系统设计基础》实验五 简单嵌入式WEB 服务器实验
20145208<信息安全系统设计基础>实验五 简单嵌入式WEB 服务器实验 20145208<信息安全系统设计基础>实验五 简单嵌入式WEB 服务器实验
- 20145315&20145307《信息安全系统设计基础》实验五
20145315&20145307<信息安全系统设计基础>实验五 北京电子科技学院(BESTI) 实 验 报 告 课程:信息安全系统设计基础 班级:1453 1452 姓名:陈俊达 ...
- 实验五(简单嵌入式WEB服务器实验)问题总结
实验五问题总结 问题链接:<信息安全系统设计基础>实验五实验报告 虽然将07_httpd文件中全部拷贝进了bc中,文件夹中拥有Makefile文件,但是还是无法通过make得到该文件夹中c ...
- 20145210 20145226 《信息安全系统设计基础》实验五 简单嵌入式WEB服务器实验
20145210 20145226 <信息安全系统设计基础>实验五 简单嵌入式WEB服务器实验 结对伙伴:20145226 夏艺华 实验报告封面 实验目的与要求 · 掌握在ARM开发板实现 ...
- Java实验五
20145113 Java实验五 网络编程及安全 实验内容 对于客户端与服务器端:修改原代码,使其可以实现连续的传消息,并且传送文件. 对于加解密部分: 对于原先的加密只加密"hello w ...
- 20145316&20145229实验五:网络通信
20145316&20145229实验五:网络通信 结对伙伴:20145316 博客链接:http://www.cnblogs.com/xxy745214935/p/6130897.html
- 20145301&20145321&20145335实验五
20145301&20145321&20145335实验五 这次实验我的组员为:20145301赵嘉鑫.20145321曾子誉.20145335郝昊 实验内容详见:实验五
随机推荐
- java大数取模
题目链接:http://lightoj.com/volume_showproblem.php?problem=1214 用java写大数果然是方便多了! import java.math.BigInt ...
- ios录音
#import "ViewController.h" #import <AVFoundation/AVFoundation.h> @interface ViewCont ...
- DOM兼容
-firstChild firstElementChild var oFirst = oUl.firstChild || oUl.firstElementChild; -lastChild la ...
- 记一次小团队Git实践(上)
公司规模不大,成立之初,选择了svn作为版本控制系统.这对于用惯了git的我来说,将就了一段时间后,极为不爽,切换到git-svn勉强能用.随后,因为产品需要发布不同的版本,而git-svn对远程分支 ...
- Wireshark工具创建过滤器的方式
Wireshark工具创建过滤器的方式 [实例1-3]现在要抓取目的或来源地址为192.168.5.9的封包.在图1.5中添加如下所示的条件: tcp dst port 3128 添加后单击Star ...
- 使用Genymotion安装APK出现错误INSTALL_FAILED_CPU_ABI_INCOMPATIBLE的解决办法
当我们安装好Genymotion后,把Android运用部署到上面调试时,console控制台会报错:Installation error: INSTALL_FAILED_CPU_ABI_INCOMP ...
- 短信猫 TIdTCPServer TIdTCPClient
短信猫 服务端: IdTCPServer1: TIdTCPServer; IdAntiFreeze1: TIdAntiFreeze; unit UnitSever; interface uses Wi ...
- SpringMVC中向服务器传递时间参数时出现的问题
1. 问题描述: 今天在SpringMVC应用中上传参数的时候遇到如下问题: The request sent by the client was syntactically incorrect 这说 ...
- MFC 打开文件夹 调用其他程序 打开文件
ShellExecute(NULL,TEXT("OPEN"),要打开的文件的路径,NULL,NULL,SW_SHOWNORMAL); ShellExecute(NULL, &quo ...
- UVa12633 Super Rooks on Chessboard(容斥 + FFT)
题目 Source http://acm.hust.edu.cn/vjudge/problem/42145 Description Let’s assume there is a new chess ...