1.新建一个商品JavaBean为Product,属性有商品名称、商品价格、商品分类、库存量、商品描述
package JavaBean;
public class Product {
private String pname;
private double price;
private String[] fenlei;
private double shuliang;
private String describle;
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String[] getFenlei() {
return fenlei;
}
public void setFenlei(String[] fenlei) {
this.fenlei = fenlei;
}
public double getShuliang() {
return shuliang;
}
public void setShuliang(double shuliang) {
this.shuliang = shuliang;
}
public String getDescrible() {
return describle;
}
public void setDescrible(String describle) {
this.describle = describle;
}
}
2.新建一个添加商品的JSP页面,来添加商品
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>商品</title>
</head>
<body>
<h2>添加商品</h2>
<form action="productlist.jsp" method="get" name="productInfo">
商品名称:<input type="text" name="pname"/><br/><br/>
商品价格:<input type="text" name="price"/><br/><br/>
商品分类:<select name="fenlei">
<option value="服装">服装</option>
<option value="家电">家电</option>
<option value="护肤">护肤</option>
</select><br/><br/>
库存量:
<input type="text" name="shuliang"/><br/><br/>
商品描述:
<input type="text" name="describle" /><br/><br/>
<input type="submit" value="添加"/><br/>
</form>
</body>
</html>
3.新建商品列表页面用来显示添加的商品信息
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="JavaBean.Product,java.util.ArrayList" %><!--导包-->
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>商品列表</title>
</head>
<body>
<% //获取表单提交的信息
String name=request.getParameter("pname");
double price=Double.parseDouble(request.getParameter("price"));
String[] fenlei=request.getParameterValues("fenlei");
double shuliang=Double.parseDouble(request.getParameter("shuliang"));
String describle=request.getParameter("describle");
Product product=new Product();//创建Product类对象
product.setPname(name);
product.setPrice(price);
product.setFenlei(fenlei);
product.setShuliang(shuliang);
product.setDescrible(describle);
ArrayList<Product> productlist=null;
if(application.getAttribute("productlist")!=null){
productlist=(ArrayList<Product>)application.getAttribute("productlist");
}//如果集合不为空,则集合中获取到新提交的信息
else{
productlist=new ArrayList<Product>();//初始化集合对象
}//如果集合为空,则集合中获取提交的信息
productlist.add(product);//集合的add方法
application.setAttribute("productlist",productlist);
%>
<h2>商品信息如下:</h2>
<table><!--创建表格-->
<tr>
<th>商品名称</th>
<th>商品价格</th>
<th>商品分类</th>
<th>库存量</th>
<th>商品描述</th>
</tr>
<%
for(int i=0;i<productlist.size();i++){//遍历循环集合
Product pro=productlist.get(i);
%>
<tr>
<td><%=pro.getPname() %></td>
<td><%=pro.getPrice() %></td>
<td><%=pro.getShuliang() %></td>
<td><%=pro.getDescrible() %></td>
<td>
<%//遍历数组
if(pro.getFenlei()!=null){
String[] h=pro.getFenlei();
for(int j=0;j<h.length;j++){
out.print(h[j]);
}
}
%>
</td>
</tr>
<%
}
%>
</table>
<a href="addproduct.jsp">返回添加页面</a>
</body>
</html>
运行结果如下: