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

由上表可知,在我们熟悉的关系型数据库中,具体的实体表对应MongoDB中的集合,表中的行对应MongoDB集合中的文档,表中的列对应MongoDB文档中的域,最关键的主键在MongoDB中是系统自动生成,MongoDB自动的生成的主键是按照特定的方法来生成,具体有12个字节,12字节按照如下方式生成: 

0|1|2|3 | 4|5|6 | 7|8 | 9|10|11 

时间戳 | 机器  | PID | 计数器

以上是个人初步学习MongoDB的介绍,如有错误,欢迎各位圆友指正。

说完了概念,就要到具体运用,MongoDB中存入和读取的数据格式均为BSON格式,BSON格式是一种类似JSON格式的数据,其具体样式如下所示:

/* 7 createdAt:2016/11/22 下午3:52:51*/ { "_id" : ObjectId("5833f953e9d60125601a8c8b"), "sid" : "7", "sname" : "红米Note4", "sprice" : "899" }, /* 8 createdAt:2016/11/22 下午3:53:19*/ { "_id" : ObjectId("5833f96fe9d60125601a8c8c"), "sid" : "8", "sname" : "平凡的世界", "sprice" : "99" }, /* 9 createdAt:2016/11/22 下午3:53:43*/ { "_id" : ObjectId("5833f987e9d60125601a8c8d"), "sid" : "9", "sname" : "斗罗大陆", "sprice" : "199" },

当我们从MongoDB数据库查询获取数据后,其格式为BSON格式,不能直接与客户端获取的数据进行匹配。在这里,我在获取数据库中BSON格式数据后,调用MongoDB驱动包中BSON.toMap()方法,把BSON格式转换为Map键值对格式的字符串,然后调用Map中Map.get(“name”)方法,获取其中具体键值对的值,从而实现与客户端页面中数据的匹配。

最后,最关键的一点就是,在新建项目中导入MongoDB驱动包,方便客户端和业务层操作数据库。这里我使用的是mongo-java-driver-3.3.0.jar包,其各版本驱动包下载链接:

2.5  核心功能代码讲解

(1)用户登录功能

实现用户登录,主要是由login_action.jsp脚本中代码来实现,代码中已给出具体注释,具体如下:

<%@ page language="java" import="java.util.*,com.mongodb.*" 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 'login_action.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"> <!-- <link type="text/css" href="https://www.jb51.net/styles.css"> --> </head> <body> <% response.setContentType("text/html;charset=utf-8"); //确保显示的汉字信息以正确编码方式显示 request.setCharacterEncoding("utf-8"); //确保获取的汉字信息以正确编码方法获取 String userName=(String)request.getParameter("username"); //获取登录页面用户名 String passWord=(String)request.getParameter("password");//获取登陆页面密码 String checkBox = request.getParameter("save_password");//获取登陆页面记住密码选择框属性值 boolean login_test = false; //设定登陆布尔值,若用户名和密码成功匹配,则为true try{ // 连接到 mongodb 服务 MongoClient mongoClient = new MongoClient( "localhost" , 27017 ); //此处采用无用户名和密码验证方式登陆 @SuppressWarnings("deprecation") DB db = mongoClient.getDB( "library" ); //连接到数据库library DBCollection coll = db.getCollection("userInfo"); //获取library数据库中集合userInfo System.out.println("Collection userInfo selected successfully"); DBCursor cursor = coll.find(); //查询集合userInfo中文档信息 int i=1; while (cursor.hasNext()) { //检索集合userInfo中所有文档信息 System.out.println("userInfo Document: "+i); DBObject show = cursor.next(); System.out.println(show); @SuppressWarnings("rawtypes") Map show1 = show.toMap(); //将检索结果show(Bson类型)转换为Map类型 String toname = (String)show1.get("username"); //提取Map中字段名为username的属性值 String topassword = (String)show1.get("password"); //提取Map中字段名为password的属性值 if(toname.equals(userName) && topassword.equals(passWord)){ //将从数据库中获取的用户名和密码与表单中获取的数据进行验证,匹配成功则使login_test值为true System.out.println("登陆成功!!!!!"+"username:"+toname+" password:"+topassword); //request.getRequestDispatcher("welcome.jsp").forward(request, response); login_test = true; } System.out.println(show1.get("username")); i++; } }catch(Exception e){ System.err.println( e.getClass().getName() + ": " + e.getMessage() ); } if(login_test) { if ("save".equals(checkBox)) { //Cookie存取时用URLEncoder.encode进行编码(PS:读取时URLDecoder.decode进行解码) String name1 = java.net.URLEncoder.encode(userName,"UTF-8"); //创建两个Cookie对象 Cookie nameCookie = new Cookie("username", name1); //设置Cookie的有效期为3天 nameCookie.setMaxAge(60 * 60 * 24 * 3); String pwd = java.net.URLEncoder.encode(passWord,"UTF-8"); Cookie pwdCookie = new Cookie("password", pwd); pwdCookie.setMaxAge(60 * 60 * 24 * 3); response.addCookie(nameCookie); response.addCookie(pwdCookie); } // request.getRequestDispatcher("welcome.jsp").forward(request, response); response.sendRedirect("welcome.jsp"); } else{ response.sendRedirect("login_Fail.jsp"); // request.getRequestDispatcher("loginFail.jsp").forward(request, response); } %> </body> </html>

(2)用户注册功能

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

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