复习了一下javaBean先关的内容,写了一个购物车功能:
先看看效果图吧:
所需要的工具与技术:javaEE(Eclipse)、Tomcat9.0、、javaBean技术
废话不多说,上代码:
GoodsSingle.java用于封装商品信息的“值javaBean”
package qau.edu;
public class GoodsSingle {
private String name;//商品名
private float price;//商品价格
private int num;//商品数量
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
}
MyTools.java工具类javaBean
package qau.edu;
import java.io.UnsupportedEncodingException;
public class MyTools {
// 转换中文乱码的问题
public static String toChinese(String str) {
if (str == "")
str = "";
try {
// 通过String类的构造方法,将指定的字符串转换成gbk编码
str = new String(str.getBytes("iso-8859-1"), "UTF-8");
} catch (UnsupportedEncodingException e) {
str = "";
e.printStackTrace();
}
return str;
}
//将String数据类型转换为int数据类型
public static int strToint(String str){
if(str==null||str.equals(""))
str="0";
int i=0;
try {
i=Integer.parseInt(str);
} catch (NumberFormatException e) {
i=0;
e.printStackTrace();
}
return i;
}
}
shopCar.java实现购物车功能的javaBean
package qau.edu;
import java.util.ArrayList;
public class ShopCar {
private ArrayList buylist = new ArrayList();
public ArrayList getBuylist() {
return buylist;
}
/**
* 向购物车中添加商品 参数single为GooleSigle类的对象
*/
public void addItem(GoodsSingle single) {
if (buylist.size() == 0) {
GoodsSingle temp = new GoodsSingle();
temp.setName(single.getName());
temp.setNum(single.getNum());
temp.setPrice(single.getPrice());
buylist.add(temp);
} else {
int i = 0;
// 遍历集合查看是否已经存在药品添加的商品
for (; i < buylist.size(); i++) {
GoodsSingle temp = (GoodsSingle) buylist.get(i);
if (temp.getName().equals(single.getName())) {
temp.setNum(temp.getNum() + 1);
break;
}
}
if (i > buylist.size()) {
GoodsSingle temp = new GoodsSingle();
temp.setName(single.getName());
temp.setNum(single.getNum());
temp.setPrice(single.getPrice());
buylist.add(temp);
}
}
}
/**
* 从购物车中移除指定的商品
*/
public void removeItem(String name){
for (int i = 0; i < buylist.size(); i++) {
GoodsSingle temp=(GoodsSingle) buylist.get(i);
if(temp.getName().equals(MyTools.toChinese(name))){
if(temp.getNum()>1){
temp.setNum(temp.getNum()-1);
break;
}
else if(temp.getNum()==1){
buylist.remove(i);
}
}
}
}
/**
* 清空购物车
*/
public void clearCar(){
buylist.clear();
}
}
index.jsp 用于初始化商品信息的列表(正式开发中应使用数据库进行数据的存储)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="java.util.ArrayList"%>
<%@page import="qau.edu.GoodsSingle"%>
Insert title here
<%!
static ArrayList goodslist = new ArrayList();
static {
String[] names = { "苹果", "香蕉", "菠萝", "西瓜", "葡萄" };
float[] prince = { 2.8f, 3.1f, 2.5f, 2.3f, 2.4f };
for (int i = 0; i < 5; i++) {
GoodsSingle single = new GoodsSingle();
single.setName(names[i]);
single.setPrice(prince[i]);
single.setNum(1);
goodslist.add(i, single);
}
}
%>
<%
session.setAttribute("goodslist", goodslist);
response.sendRedirect("show.jsp");
%>
show.jsp 用于显示商品列表
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="java.util.ArrayList"%>
<%@page import="qau.edu.GoodsSingle"%>
Insert title here
<%
ArrayList goodslist =(ArrayList)session.getAttribute("goodslist");
%>
提供商品如下: |
名称 |
价格 |
购买 |
<%
if (goodslist == null || goodslist.size() == 0) {
%>
没有商品显示! |
<%
} else {
for (int i = 0; i < goodslist.size(); i++) {
GoodsSingle single = (GoodsSingle) goodslist.get(i);
%>
<%=single.getName()%> |
<%=single.getPrice()%> |
购买 |
<%
}
}
%>
查看购物车 |
docar.jsp 处理相应的action
<%@page import="com.sun.xml.internal.ws.encoding.MtomCodec"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="java.util.ArrayList"%>
<%@page import="qau.edu.GoodsSingle"%>
<%@page import="qau.edu.MyTools"%>
Insert title here
<%
String action = request.getParameter("action");
if (action == "")
action = "";
if (action.equals("buy")) {
//购买商品
ArrayList goodslist = (ArrayList) session.getAttribute("goodslist");
int id = MyTools.strToint(request.getParameter("id"));
GoodsSingle single = (GoodsSingle) goodslist.get(id);
myCar.addItem(single);
response.sendRedirect("show.jsp");
} else if (action.equals("remove")) {
//移除商品
String name = request.getParameter("name");
myCar.removeItem(name);
response.sendRedirect("shopcar.jsp");
} else if (action.equals("clear")) {
myCar.clearCar();
response.sendRedirect("shopcar.jsp");
} else {
response.sendRedirect("show.jsp");
}
%>
shopcar.jsp 显示购物车中的内容,清空购物车或删除商品
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="java.util.ArrayList"%>
<%@page import="qau.edu.GoodsSingle"%>
Insert title here
<%
ArrayList buylist = myCar.getBuylist();
float total = 0;
%>
购买商品如下: |
名称 |
价格(元/斤) |
数量 |
总价(元) |
移除(-1/次) |
<%
if (buylist == null || buylist.size() == 0) {
%>
您的购物车为空! |
<%
} else {
for (int i = 0; i < buylist.size(); i++) {
GoodsSingle single = (GoodsSingle) buylist.get(i);
String name = single.getName();
float price = single.getPrice();
int num = single.getNum();
float money = ((int) ((price * num + 0.05f) * 10)) / 10f;
total += money;
%>
<%=name%> |
<%=price%> |
<%=num%> |
<%=money%> |
移除 |
<%
}
}
%>
应付金额:<%=total%> |
继续购买 |
清空购物车 |
关于购物车功能的相关代码已经全部贴在这了,下面是我在运行中出现的问题以及解决方法:
在catalina.properties中添加或者修改一个注释内容(改成下面这个模样):tomcat.util.http.parser.HttpParser.requestTargetAllow=|{}
至于原理是什么可以去百度搜索400-bad request的错误