使用MongoDB和JSP实现一个简单的购物车系统实例(8)

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"https://www.jb51.net/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="https://www.jb51.net/<%=basePath%>"> <title>My JSP 'https://www.jb51.net/bottom.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> </head> <body> <hr> <h4>Copyright @2016 舞动的心</h4> </body> </html>

首页中间主体main_shop.jsp页面代码;

<%@ page contentType="text/html;charset=GBK" import="java.util.*,com.liuzhen.shop.MongodbBean" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"https://www.jb51.net/"; String cp=request.getParameter("cp");//cp为分页数 //int page_number = DBBean.getTotalPage(); //int currpage = page_number; int currpage=(cp==null||cp=="")?1:Integer.parseInt(cp); String[][] ss = MongodbBean.getGoodList(currpage); int n = MongodbBean.getlength(ss); %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="https://www.jb51.net/<%=basePath%>"> <title>购物页面</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <link type="text/css" href="https://www.jb51.net/css/login.css"> </head> <body> <div> <div> <table> <tr> <th>物品编号</th> <th>物品名</th> <th>定价</th> <th>求购</th> </tr> <% for(int i=0;i<n;i++) { %> <tr> <td><%= ss[i][0] %></td> <td><%= ss[i][1] %></td> <td><%= ss[i][2] %></td> <td><a href="Buy.jsp?sid=<%= ss[i][0] %>&action=buy">购买</a></td> </tr> <% } %> </table> <% int tp=MongodbBean.getTotalPage(); if(currpage!=1) { %> <a href="main_shop.jsp?cp=<%=currpage-1%>">上一页</a> <% } if(currpage!=tp) { %> <a href="main_shop.jsp?cp=<%=currpage+1%>">下一页</a> <% } %> <form action="https://www.jb51.net/main_shop.jsp"> <select onchange="document.myform.submit()"> <% for(int i=1;i<=tp;i++) { %> <option value="<%=i%>" <%= (i==currpage)?"selected":"" %>>第<%=i%>页</option> <% } %> </select> </form> </div> <div> <button>查看购物车</button> </div> </div> </body> </html>

实现查看商品的功能,主要是通过调用MongodbBean.java类来实现,其具体代码如下:

package com.liuzhen.shop; import java.util.Map; import java.util.Vector; import com.mongodb.*; public class MongodbBean { static int span=5; //设定JSP页面表单单页显示物品信息行数为5行 //返回数据库中全部货物编号sid public static String[] getGood_sid(){ String[] good_sid_temporary = new String[100]; //定义一个长度为100的暂时存放货物编号的一维数组 // 连接到 mongodb 服务 MongoClient mongoClient = new MongoClient( "localhost" , 27017 ); //此处采用无用户名和密码验证方式登陆 @SuppressWarnings("deprecation") DB db = mongoClient.getDB( "library" ); //连接到数据库library DBCollection coll = db.getCollection("good"); //获取library数据库中集合good System.out.println("Collection userInfo selected successfully"); DBCursor cursor = coll.find(); //查询集合good中文档信息 int i=0; while (cursor.hasNext()) { //检索集合good中所有文档信息 DBObject show = cursor.next(); @SuppressWarnings("rawtypes") Map show1 = show.toMap(); //将检索结果show(Bson类型)转换为Map类型 String tosid = (String)show1.get("sid"); //提取Map中字段名为sid的属性值 good_sid_temporary[i] = tosid; //将数据库中查询的货物编号存储入数组good_sid i++; } String[] good_sid = new String[i]; //根据查询数据遍历集合中文档信息i值来确定最终返回数组长度 for(int j=0;j<i;j++){ good_sid[j] = good_sid_temporary[j]; } return good_sid; } //返回数据库中全部货物名称sname public static String[] getGood_sname(){ String[] good_sname_temporary = new String[100]; //定义一个长度为100的暂时存放货物名称的一维数组 // 连接到 mongodb 服务 MongoClient mongoClient = new MongoClient( "localhost" , 27017 ); //此处采用无用户名和密码验证方式登陆 @SuppressWarnings("deprecation") DB db = mongoClient.getDB( "library" ); //连接到数据库library DBCollection coll = db.getCollection("good"); //获取library数据库中集合good System.out.println("Collection userInfo selected successfully"); DBCursor cursor = coll.find(); //查询集合good中文档信息 int i=0; while (cursor.hasNext()) { //检索集合good中所有文档信息 DBObject show = cursor.next(); @SuppressWarnings("rawtypes") Map show1 = show.toMap(); //将检索结果show(Bson类型)转换为Map类型 String tosname = (String)show1.get("sname"); //提取Map中字段名为sname的属性值 good_sname_temporary[i] = tosname; //将数据库中查询的货物名称存储入数组good_sname i++; } String[] good_sname = new String[i]; //根据查询数据遍历集合中文档信息i值来确定最终返回数组长度 for(int j=0;j<i;j++){ good_sname[j] = good_sname_temporary[j]; } return good_sname; } //返回数据库中全部货物价格sprice public static String[] getGood_sprice(){ String[] good_sprice_temporary = new String[100]; //定义一个长度为100的暂时存放货物价格的一维数组 // 连接到 mongodb 服务 MongoClient mongoClient = new MongoClient( "localhost" , 27017 ); //此处采用无用户名和密码验证方式登陆 @SuppressWarnings("deprecation") DB db = mongoClient.getDB( "library" ); //连接到数据库library DBCollection coll = db.getCollection("good"); //获取library数据库中集合good System.out.println("Collection userInfo selected successfully"); DBCursor cursor = coll.find(); //查询集合good中文档信息 int i=0; while (cursor.hasNext()) { //检索集合good中所有文档信息 DBObject show = cursor.next(); @SuppressWarnings("rawtypes") Map show1 = show.toMap(); //将检索结果show(Bson类型)转换为Map类型 String tosprice = (String)show1.get("sprice"); //提取Map中字段名为sname的属性值 good_sprice_temporary[i] = tosprice; //将数组库中查询的货物价格存储入数组good_sprice i++; } String[] good_sprice = new String[i]; //根据查询数据遍历集合中文档信息i值来确定最终返回数组长度 for(int j=0;j<i;j++){ good_sprice[j] = good_sprice_temporary[j]; } return good_sprice; } //根据分页当前page数,从数据库中获取当前单个页面货物种类的具体信息,并以二维数据返回具体信息 public static String[][] getGoodList(int page) { String[][] result=null; Vector<String[]> v=new Vector<String[]>(); //定义一个Vector集合,一个记录存放一个货物的具体信息 String[] good_sid = getGood_sid(); //获取货物编号 String[] good_sname = getGood_sname(); //获取货物名称 String[] good_sprice = getGood_sprice(); //获取货物价格 int len = good_sid.length; for(int i=0;i<span;i++){ int t = (page-1)*span+i; //获取货物编号 if(t >= len){ //如果当前货物编号大于数据库中已有编号,则跳出循环 break; } String[] good_temp=new String[3]; //定义一个长度为3的数组,用于存放一个物品的编号、名称、价格信息 good_temp[0]=good_sid[t]; good_temp[1]=good_sname[t]; good_temp[2]=good_sprice[t]; v.add(good_temp); //将1个物品的信息存入Vector集合中 } int size = v.size(); result=new String[size][]; //根据Vercotr大小,给result指定行数大小 for(int j=0;j<size;j++) { //返回Vector中一个值(其中即表示一个物品的sid,名称和价格),并赋值给result[j],即result二维数组一行表示一个物品具体信息 result[j]=(String[])v.elementAt(j); } return result; } //根据货物sid,返回其价格信息 public static double getPrice(String sid) { double price = 0; //定义返回物品的价格 String[] good_sprice = getGood_sprice(); //获取全部物品的价格 int i = Integer.parseInt(sid); //将String类型的物品编号sid转换为int型 String sprice = good_sprice[i]; //根据sid获取物品的价格 price = Double.parseDouble(sprice); //将String类型的价格信息转换为double型,并赋值给price return price; } //根据货物sid,返回货物的名称和价格,一一个长度为2的数组返回 public static String[] getDetail(String sid) { String[] good_detail=null; good_detail = new String[2]; String[] good_sname = getGood_sname(); //获取全部物品名称 String[] good_sprice = getGood_sprice(); //获取全部物品价格 int i = Integer.parseInt(sid); //将String类型的物品编号sid转换为int型 good_detail[0] = good_sname[i]; //根据物品编号sid,得到名称存入数组good_detail中 good_detail[1] = good_sprice[i]; //根据物品编号sid,得到物品价格存入数组good_detail中 return good_detail; } //通过查询数据库中货物种类数目,以5行为一页,返回现有货物页数 public static int getTotalPage() { int page = 0; String[] good_sid = getGood_sid(); int len = good_sid.length; page = len/span+((len%span==0)?0:1); //以span(span值为5)行为一页,计算货物具有的页数page return page; } //返回一个二维数组的行数大小 public static int getlength(String[][] a){ return a.length; } public static void main(String args[]){ // String[] good_sid = getGood_sid(); //定义一个存放货物编号的一维数组 // String[] good_sname = getGood_sname(); //定义一个存放货物名称的一维数组 // String[] good_sprice = getGood_sprice(); //定义一个存放货物价格的一维数组 // // for(int j=0;j<10;j++){ // System.out.println("货物sid:"+good_sid[j]); // System.out.println("货物sname:"+good_sname[j]); // System.out.println("货物是price:"+good_sprice[j]); // System.out.println("**************************"); // System.out.println(); // } System.out.println("分页数目(测试):"+MongodbBean.getTotalPage()); String[][] ss=MongodbBean.getGoodList(MongodbBean.getTotalPage()); for(int i=0;i<ss.length;i++) { System.out.println(ss[i][0]); System.out.println(ss[i][1]); System.out.println(ss[i][2]); System.out.println("***********"); } int n = ss.length; System.out.println("数组长度为:"+n); } }

2.6.4购买商品

实现购买商品,通过Buy.jsp业务处理脚本调用ShopCartBean.java类来实现。

ShopCartBean.java类代码如下:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wzjxds.html