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郝昊 实验内容详见:实验五
随机推荐
- X64下MmIsAddressValid的逆向及内存寻址解析
标 题: [原创]X64下MmIsAddressValid的逆向及内存寻址解析 作 者: 普通朋友 时 间: 2015-10-21,20:03:52 链 接: http://bbs.pediy.com ...
- phpstudy配置ssl
https://yunpan.cn/cPEyzVycbkiE3 (提取码:03aa) 1.重写规则:http://www.cnphp.info/htaccess-rewrite.html 2.相关文档 ...
- 《DSP using MATLAB》示例Example4.4
代码: x1 = [2, 3, 4]; x2 = [3, 4, 5, 6]; % x1 x2 sequences % n1 = 0:1:2; n2 = 0:1:3; n1 = 0:1:length(x ...
- iOS10 UI教程视图的几何形状
iOS10 UI教程视图的几何形状 视图属性中的一部分属性可以让定义的视图绘制在屏幕上.在讲解这些属性前,我们首先将讲解,定义视图的几何形状所涉及到的结构类型.这些结构类型如下: CGPoint:它表 ...
- TStringList 常用操作
//TStringList 常用方法与属性: var List: TStringList; i: Integer; begin List := TStringList.Create; ...
- wpf中dropdownButton控件下拉居中。。。
设置模版中popup控件的HorizontalOffset属性来控制居中. 还是对popup控件不熟,折腾了一会.
- jquery(ajax)与js(ajax)的比较
原始js: function update_mess(){ var account_name = $("#account").val(); var xmlhttp; if(wind ...
- html中frameset的详细使用方法
http://blog.csdn.net/csb5201314/article/details/5695417
- node.js--HTTP模块
HTTP模块 node.js提供了一个创建自己服务器的方式,用起来很简单,首先引用http模块 //引用HTTP模板 var http = require('http'); 创建服务实例:http.c ...
- Windows下安装Docker
放在三年前,你不认识Docker情有可原,但如果现在你还这么说,不好意思,只能说明你OUT了,行动起来吧骚年,很可能你们公司或者你即将要去的公司,或者你想去的公司很可能就会引入Docker,或者已经引 ...