├── README.md └── books ├── .gitignore ├── WebContent ├── META-INF │ └── MANIFEST.MF ├── WEB-INF │ ├── lib │ │ ├── jstl.jar │ │ ├── mysql-connector-java-5.1.8-bin.jar │ │ └── standard.jar │ └── web.xml ├── css │ └── style.css ├── image │ ├── bg01.jpg │ ├── book │ │ ├── book_01.gif │ │ ├── book_02.gif │ │ ├── book_03.gif │ │ ├── book_04.gif │ │ ├── book_05.gif │ │ ├── book_06.gif │ │ ├── book_07.gif │ │ ├── book_08.gif │ │ ├── book_09.gif │ │ └── book_10.gif │ ├── button_chart.png │ ├── button_gray_bg.gif │ ├── button_login.gif │ ├── button_login副本.gif │ ├── button_reg.gif │ ├── button_register.gif │ ├── button_search.gif │ ├── button_shop.gif │ ├── login_lock.gif │ ├── logo.gif │ ├── logo01.jpg │ ├── logo_ps03.png │ ├── step_arrow.gif │ ├── step_arrow_past.gif │ └── success_ico.gif ├── index.jsp ├── js │ └── valiadate.js ├── login.jsp ├── logout.jsp ├── main.jsp ├── main_head.jsp ├── orderlist.jsp ├── register.jsp ├── register_success.jsp ├── shopping.jsp └── shopping_success.jsp ├── bookstore.sql ├── image ├── img01.png ├── img02.png ├── img03.png ├── img04.png ├── img05.png ├── img06.png ├── img07.png ├── img08.png ├── img09.png ├── img10.png ├── img11.png ├── img12.png ├── img13.png ├── img14.png ├── img15.png ├── phone01.jpg ├── phone01.png ├── phone02.jpg └── phone03.jpg └── src └── com ├── biz ├── BookBiz.java ├── ItemBiz.java ├── OrderBiz.java ├── UserBiz.java └── impl │ ├── BookBizImpl.java │ ├── ItemBizImpl.java │ ├── OrderBizImpl.java │ └── UserBizImpl.java ├── dao ├── BaseDao.java ├── BookDao.java ├── ItemDao.java ├── OrderDao.java ├── UserDao.java └── impl │ ├── BookDaoImpl.java │ ├── ItemDaoImpl.java │ ├── OrderDaoImpl.java │ └── UserDaoImpl.java ├── entity ├── Book.java ├── Item.java ├── Order.java └── UserInfo.java └── servlet ├── AddOrderServlet.java ├── CartServlet.java ├── LoginServlet.java ├── ModifyCartServlet.java ├── RegisterServlet.java ├── SearchServlet.java └── ShowOrderServlet.java /README.md: -------------------------------------------------------------------------------- 1 | # 网上书城项目答辩 2 | 3 | [TOC] 4 | 5 | 6 | 7 | ## 主要技术 8 | 9 | **关键字**:JSP、servlet、Ajax、jstl、JavaScript、注册登录、分页、购物车、增删改查 10 | 11 | 开发环境:Eclipse、MySQL 5.7、Tomcat 8.0 12 | 13 | ## 数据库表结构设计 14 | 15 | ![image](https://github.com/comeCU/books/raw/master/image/img01.png) 16 | 17 | **books表结构:** 18 | 19 | ![image](https://github.com/comeCU/books/raw/master/image/img02.png) 20 | 21 | **items表结构:** 22 | 23 | ![image](https://github.com/comeCU/books/raw/master/image/img03.png) 24 | 25 | **orders表结构:** 26 | 27 | ![image](https://github.com/comeCU/books/raw/master/image/img04.png) 28 | 29 | **userinfo表结构:** 30 | 31 | ![image](https://github.com/comeCU/books/raw/master/image/img05.png) 32 | 33 | 34 | 35 | ## 项目包结构 36 | 37 | ![image](https://github.com/comeCU/books/raw/master/image/img06.png) 38 | 39 | 40 | 41 | ## MVC设计模式 42 | 43 | ### M(model层) 44 | 45 | 1. biz包:业务处理。 46 | 2. dao包:数据访问,对数据库的一些封装操作。 47 | 3. entity包:实体类,javabean构建,View层和数据库之间的桥梁作用。 48 | 49 | ### V(view层) 50 | 51 | 1. Jsp页面:与用户进行交互的界面。 52 | 53 | ![image](https://github.com/comeCU/books/raw/master/image/img07.png) 54 | 55 | ### C(controller 层) 56 | 57 | 1. servlet包:控制层,处理View层Jsp页面发来的请求。 58 | 59 | ## 注册登录模块 60 | 61 | ### Register 62 | 63 | ![image](https://github.com/codeYoke/my-picture/blob/master/bookStore/book2.png) 64 | 65 | **注册页面中form表单** 66 | 67 | 验证用户名、验证密码、验证邮箱 68 | 69 | ```jsp 70 |
71 |
72 |
用 户 名:
73 |
74 |
密  码:
75 |
密码至少8位
76 |
确认密码:
77 |
78 |
Email地址:
79 |
请输入正确格式的邮箱
80 |
81 |
82 |
83 |
84 | ``` 85 | 86 | **Ajax(异步 JavaScript 和 XML )**:验证用户名是否已被注册,页面部分数据刷新,而无需加载整个网页,提高网站的访问效率。 87 | 88 | ```javascript 89 | /** 90 | * Ajax检查用户是否已经被注册 91 | * 异步:发送请求时不等返回结果,由回调函数处理结果 92 | * @returns {Boolean} 93 | */ 94 | function isExists() { 95 | var xmlHttp; //定义 xmlHttp XmlHttpRequest对象从服务器异步获取数据后通过javascript修改页面局部信息 96 | try { 97 | //根据不同浏览器初始化不同的xmlHttp浏览器对象 98 | //IE6以上浏览器 99 | xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); 100 | } catch (e) { 101 | try { 102 | //FireFox 103 | xmlHttp = new XMLHttpRequest(); 104 | } catch (e) { 105 | try { 106 | //IE5.5+ 107 | xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); 108 | } catch (e) { 109 | alert("您的浏览器不支持Ajax"); 110 | return false; 111 | } 112 | } 113 | } 114 | var username = document.getElementById("username").value; //获取用户名 115 | /** 116 | * open(提交方式[get|post],url(servlet路径),同步或异步[false|true]) 117 | * RegisterServlet: servlet路径 118 | * 打开和后台服务器的链接 119 | */ 120 | xmlHttp.open("POST", "RegisterServlet?action=check&username="+username, true); //路径中不能有空格 121 | xmlHttp.send(null); //传送数据 122 | /** 123 | * onreadystatechange:调用回调函数 124 | * readyState:请求状态,代码是未初始化,1:初始化,2:发送请求,3:开始接受数据,4:接受结果完毕 125 | * status: http状态码 200成功,404路径错误,500后台代码错误, 126 | */ 127 | xmlHttp.onreadystatechange = function() { 128 | if(xmlHttp.readyState==4 && xmlHttp.status==200) { 129 | var usernull = document.getElementById("usernull"); 130 | var result = xmlHttp.responseText; //接受服务器端传过来的数据,写出来的数据都是String类型 131 | if(result == "true") { 132 | usernull.innerHTML = "当前用户名已被注册"; 133 | return false; 134 | } else if(result == "false") { 135 | usernull.innerHTML = "当前用户名可用"; 136 | return true; 137 | } 138 | } 139 | } 140 | } 141 | ``` 142 | 143 | 144 | 145 | ### Login 146 | 147 | ![image](https://github.com/codeYoke/my-picture/blob/master/bookStore/book1.png) 148 | 149 | **LoginServlet中doGet方法:** 150 | 151 | ```java 152 | protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 153 | String username = request.getParameter("username");//获取页面传过来的参数 154 | String password = request.getParameter("password"); 155 | //登录操作 156 | boolean flag = userBiz.checkLogin(username, password); 157 | 158 | if(flag) { 159 | request.getSession().setAttribute("loginuser", username); 160 | // response.sendRedirect("main.jsp"); 161 | response.sendRedirect("SearchServlet"); //首页直接显示 162 | } else {//登录失败 163 | response.setContentType("text/html;charset=utf-8"); 164 | // response.setCharacterEncoding("utf-8"); 165 | PrintWriter out = response.getWriter(); 166 | out.println(""); 170 | out.close(); 171 | } 172 | } 173 | ``` 174 | 175 | 176 | 177 | ## 网站首页及搜索图书模块 178 | 179 | ![image](https://github.com/codeYoke/my-picture/blob/master/bookStore/book3.png) 180 | 181 | 182 | 183 | ### **分页展示** 184 | 185 | Jstl标签库 c:forEach,循环遍历展示图书信息。 186 | 187 | ```jsp 188 | 189 | 190 | 191 | ${book.bookname } 192 | 193 | ¥${book.b_price } 194 | 195 | ${book.stock } 196 | 197 | 198 | 199 | 200 | 201 | ``` 202 | 203 | ```jsp 204 | 205 | <%if(request.getAttribute("current") != null) { %> 206 | 207 |
208 | 首页 209 | <%for(int i = 1; i <= totalPage; i++) { %> 210 | <%if(i==no) { %> 211 | <%=i %> 212 | <%continue;} %> 213 | <%=i %> 214 | <%} %> 215 | 尾页 216 |
217 | 218 | <%} %> 219 | 220 | ``` 221 | 222 | **servlet处理** 223 | 224 | ```java 225 | protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 226 | request.setCharacterEncoding("utf-8"); 227 | 228 | String currentPage = request.getParameter("currentPage");//获取当前页 229 | int no = currentPage == null?1:Integer.parseInt(currentPage);//如果当前页为空,则默认为1,否则转化为相应的int 230 | 231 | // String currentSearchPage = request.getParameter("currentSearchPage"); 232 | String bookname = request.getParameter("keywords");//获取你输入的书名 233 | List books = null; 234 | if(bookname==null || bookname.equals("")){ 235 | books=bookbiz.findAll(3, no);//查询所有。3:代表每页显示的条数,no:当前显示页面 236 | request.setAttribute("totalPage", (bookbiz.count()/3 + 1));//将总页面数存入request 237 | 238 | } else { 239 | books = bookbiz.findBookByName(bookname,3, no);//根据书名模糊查询出数据 240 | System.out.println(books.size()); 241 | request.setAttribute("totalPage", (books.size()/3 + 1));//将总页面数存入request 242 | } 243 | 244 | request.setAttribute("books", books);//将查询出的数据存入request 245 | request.setAttribute("current", no);//将当前页存入request 246 | request.getRequestDispatcher("main.jsp").forward(request, response);//页面转发 247 | } 248 | ``` 249 | 250 | ### 关键字匹配查询 251 | 252 | **输入“J”关键字,展示搜索结果** 253 | 254 | ![image](https://github.com/codeYoke/my-picture/blob/master/bookStore/book4.png) 255 | 256 | 257 | 258 | ## 购物车模块 259 | 260 | ### 增 261 | 262 | ![image](https://github.com/codeYoke/my-picture/blob/master/bookStore/book4.gif) 263 | 264 | **注意:** 265 | 266 | 当库存不足时,防止库存数被减到负值。 267 | 268 | ```java 269 | if(oneStock > 0) {//如果库存大于0 270 | //查看当前图书是否已经存在于购物车中 271 | //查看购物车列表是否已经存在该图书,对比两个图书id 272 | for (int j = 0; j < bookcart.size(); j++) { 273 | Book existBook = (Book) bookcart.get(j); 274 | if(existBook.getBid() == bid) {//如果购物车中的id==被选中的图书id,则购物车中的数量+1 275 | bookcart.remove(j); 276 | existBook.setCount(existBook.getCount()+1);//购物车中商品数量+1 277 | double totalPrice = existBook.getPrice(); 278 | bookcart.add(existBook);//商品总价 279 | System.out.println("总价"+totalPrice); 280 | isNotExists = false; 281 | 282 | //修改库存 283 | isOrNotChangeStock = bookbiz.changeStock(bid, "-1"); 284 | break; 285 | } 286 | } 287 | 288 | if(!isNotExists) { 289 | continue; 290 | } 291 | book.setBid(bid); 292 | 293 | //判断当前获取的图书信息是否为指定bid下的信息 294 | for (int j = 0; j < title.length; j++) { 295 | // request.setCharacterEncoding("utf-8"); 296 | //解决页面图书标题中文显示乱码 297 | String title_temp =new String(title[j].getBytes("ISO-8859-1"),"utf-8");//取标题 298 | 299 | if(title_temp.indexOf(bids[i]+":")<0) {//indexOf匹配bids是否包含在title_temp中,如果没有返回-1 300 | continue; 301 | } 302 | if(image[j].indexOf(bids[i]+":")<0) { 303 | continue; 304 | } 305 | if(price[j].indexOf(bids[i]+":")<0) { 306 | continue; 307 | } 308 | if(stock[j].indexOf(bids[i]+":")<0) { 309 | continue; 310 | } 311 | //添加指定bid下的图书信息 312 | book.setBookname(filter(title_temp, bids[i])); 313 | book.setImage(filter(image[j], bids[i])); 314 | book.setPrice(Double.parseDouble(filter(price[j], bids[i]))); 315 | // book.setPrice(Double.valueOf(filter(price[j], bids[i]))); 316 | book.setStock(filter(stock[j], bids[i])); 317 | book.setCount(1); 318 | 319 | //修改库存 320 | bookbiz.changeStock(bid, "-1"); 321 | bookcart.add(book); 322 | } 323 | 324 | } 325 | ``` 326 | 327 | 328 | 329 | ### 删 330 | 331 | 移除操作 332 | 333 | ```java 334 | protected void remove(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 335 | //1.获取图书id 336 | String bid = request.getParameter("bid"); 337 | //2.获取购物车 338 | List bookcart = (List) request.getSession().getAttribute("bookcart"); 339 | //3.循环查找购物车中相同的图书id 340 | for(int i = 0; i< bookcart.size(); i++) { 341 | Book book = bookcart.get(i); 342 | if(Integer.valueOf(bid) == book.getBid()) { 343 | //4.移除图书 344 | bookcart.remove(i); 345 | //5.修改库存 346 | bookbiz.changeStock(Integer.parseInt(bid), book.getCount()+""); 347 | break; 348 | } 349 | } 350 | 351 | //6.将购物车保存到session中 352 | // request.getSession().setAttribute("bookcart", bookcart); 353 | request.getSession().setAttribute("bookcart_count", bookcart.size()); 354 | System.out.println(request.getSession().getAttribute("bookcart_count")); 355 | } 356 | ``` 357 | 358 | 359 | 360 | ### 改 361 | 362 | 1. **前端shopping.jsp页面,添加onblur失去焦点事件,当鼠标离开输入框时执行JavaScript代码,调用update方法。** 363 | 364 | ```jsp 365 | 366 | 367 | 368 | 370 | 371 | ${book.bookname } 372 | 374 | 376 | ¥ 377 | 378 | 移除 379 | 380 | 381 | 382 | ``` 383 | 384 | 2. **异步更新图书购买量数据,并向控制层Servlet中返回一个“update”** 385 | 386 | ```javascript 387 | /** 388 | * Ajax修改购物车中的数量 389 | */ 390 | function update(size_str, i, bid_str) { 391 | //初始化XMLHttpRequest对象 392 | var xmlHttp; 393 | try { 394 | xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); 395 | } catch (e) { 396 | try { 397 | xmlHttp = new XMLHttpRequest(); 398 | } catch (e) { 399 | try { 400 | xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); 401 | } catch (e) { 402 | e.message(); 403 | alert("你的浏览器不支持Ajax"); 404 | } 405 | } 406 | } 407 | //2.获取修改后的数量 408 | var num_str = document.getElementById("nums_"+i); 409 | //3.打开服务器链接 410 | xmlHttp.open("POST", "ModifyCartServlet?action=update&bid="+bid_str+"&count="+num_str.value, true); 411 | //4.传值 ,无参传null值 412 | xmlHttp.send(null); 413 | //5.设置回调函数 414 | xmlHttp.onreadystatechange = function() { 415 | if(xmlHttp.readyState == 4 && xmlHttp.status == 200) { 416 | count(size_str);//调用Servlet中的方法 417 | } 418 | } 419 | } 420 | ``` 421 | 422 | 3. **servlet中通过前端返回的请求调用相应的方法。** 423 | 424 | ```java 425 | String action = request.getParameter("action"); 426 | if(action.equals("update")) { 427 | updateCart(request, response); 428 | } 429 | else if(action.equals("remove")) { 430 | remove(request, response); 431 | } 432 | ``` 433 | 434 | 4. **控制层Servlet更新购物车数据,并修改库存。** 435 | 436 | ```java 437 | protected void updateCart(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 438 | //1.获取图书id和修改后的数量 439 | String bid = request.getParameter("bid"); 440 | String count = request.getParameter("count"); 441 | //2.获取购物车 442 | List bookcart = (List) request.getSession().getAttribute("bookcart"); 443 | //3.查找购物车中修改过的图书,并修改相应的信息 444 | for (Book book : bookcart) { 445 | if(Integer.valueOf(bid) == book.getBid()) { 446 | //4.获取修改前的图书数量 447 | int old_count = book.getCount(); 448 | //5.设置当前图书的新数量 449 | book.setCount(Integer.valueOf(count)); 450 | System.out.println("当前图书新数量"+count); 451 | 452 | int nowStock = 0; 453 | try { 454 | nowStock = basedao.queryStock(Integer.parseInt(bid)); 455 | } catch (Exception e) { 456 | e.printStackTrace(); 457 | } 458 | 459 | //6.修改库存 460 | bookbiz.changeStock(Integer.parseInt(bid), (old_count-Integer.parseInt(count))+""); 461 | break; 462 | } 463 | } 464 | } 465 | ``` 466 | 467 | 468 | 469 | ### 查 470 | 471 | 与搜索图书模块类似 472 | 473 | 474 | 475 | ## 我的订单模块 476 | 477 | 1. 获取订单信息,存入List中 478 | 479 | ```java 480 | <% 481 | List orders = (List) request.getAttribute("orders"); 482 | %> 483 | ``` 484 | 485 | 2. 订单展示 486 | 487 | ```jsp 488 | 489 | 490 | ${order.oid } 491 | ${order.username } 492 | ${order.createdate } 493 | ${order.total_price } 494 | 495 | ${order.bookname } 496 | ${order.b_price } 497 | ${order.total_price/order.b_price } 498 | 499 | 500 | ``` 501 | 502 | 503 | 504 | ## 用户退出 505 | 506 | **当用户退出登录,清空session。** 507 | 508 | ```java 509 | <%--用户退出登录 --%> 510 | <% 511 | request.getSession().removeAttribute("loginuser"); 512 | request.getSession().removeAttribute("bookcart"); 513 | request.getSession().removeAttribute("bookcart_count"); 514 | response.sendRedirect("login.jsp"); 515 | %> 516 | ``` 517 | 518 | 519 | 520 | ## 一些注意细节 521 | 522 | 1. **为防止游客非法访问页面,在每一个前端页面body之后加上登录检测代码。** 523 | 524 | ```jsp 525 | <% 526 | String username = (String)session.getAttribute("loginuser"); 527 | if(username == null) { 528 | response.sendRedirect("login.jsp"); 529 | } 530 | %> 531 | ``` 532 | 533 | 2. **当库存不足时,防止库存数被减到负值。** 534 | 535 | ```java 536 | if(oneStock > 0) {//如果库存大于0 537 | //... 538 | } 539 | ``` 540 | 541 | 3. **href中加上空链接,否则该页面中的session不会刷新,必须手动刷新。** 542 | 543 | ```jsp 544 | 移除 545 | ``` 546 | 547 | 548 | ## 致谢 549 | 550 | 感谢何老师、Java web课程老师的指导,并感谢同学们的相互帮助! 551 | -------------------------------------------------------------------------------- /books/.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled class file 2 | *.class 3 | 4 | # Log file 5 | *.log 6 | 7 | # BlueJ files 8 | *.ctxt 9 | 10 | # Mobile Tools for Java (J2ME) 11 | .mtj.tmp/ 12 | 13 | # Package Files # 14 | #*.jar 15 | *.war 16 | *.nar 17 | *.ear 18 | *.zip 19 | *.tar.gz 20 | *.rar 21 | 22 | # test ignore this file 23 | .classpath 24 | .project 25 | .settings 26 | 27 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 28 | hs_err_pid* 29 | /bin/ 30 | -------------------------------------------------------------------------------- /books/WebContent/META-INF/MANIFEST.MF: -------------------------------------------------------------------------------- 1 | Manifest-Version: 1.0 2 | Class-Path: 3 | 4 | -------------------------------------------------------------------------------- /books/WebContent/WEB-INF/lib/jstl.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codeYoke/bookStore/8e14a77fe408bbe9b7a470353b1ad2932073a9f5/books/WebContent/WEB-INF/lib/jstl.jar -------------------------------------------------------------------------------- /books/WebContent/WEB-INF/lib/mysql-connector-java-5.1.8-bin.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codeYoke/bookStore/8e14a77fe408bbe9b7a470353b1ad2932073a9f5/books/WebContent/WEB-INF/lib/mysql-connector-java-5.1.8-bin.jar -------------------------------------------------------------------------------- /books/WebContent/WEB-INF/lib/standard.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codeYoke/bookStore/8e14a77fe408bbe9b7a470353b1ad2932073a9f5/books/WebContent/WEB-INF/lib/standard.jar -------------------------------------------------------------------------------- /books/WebContent/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | books 4 | 5 | index.jsp 6 | login.jsp 7 | 8 | 9 | 10 | LoginServlet 11 | com.servlet.LoginServlet 12 | 13 | 14 | LoginServlet 15 | /LoginServlet 16 | 17 | 18 | RegisterServlet 19 | com.servlet.RegisterServlet 20 | 21 | 22 | RegisterServlet 23 | /RegisterServlet 24 | 25 | 26 | SearchServlet 27 | com.servlet.SearchServlet 28 | 29 | 30 | SearchServlet 31 | /SearchServlet 32 | 33 | 34 | CartServlet 35 | com.servlet.CartServlet 36 | 37 | 38 | CartServlet 39 | /CartServlet 40 | 41 | 42 | ModifyCartServlet 43 | com.servlet.ModifyCartServlet 44 | 45 | 46 | ModifyCartServlet 47 | /ModifyCartServlet 48 | 49 | 50 | AddOrderServlet 51 | com.servlet.AddOrderServlet 52 | 53 | 54 | AddOrderServlet 55 | /AddOrderServlet 56 | 57 | 58 | ShowOrderServlet 59 | com.servlet.ShowOrderServlet 60 | 61 | 62 | ShowOrderServlet 63 | /ShowOrderServlet 64 | 65 | 66 | 67 | 68 | http://java.sun.com/jstl/core 69 | /WEB-INF/c.tld 70 | 71 | 72 | -------------------------------------------------------------------------------- /books/WebContent/css/style.css: -------------------------------------------------------------------------------- 1 | body { margin:0; padding:0; font-size:12px; line-height:20px; font-family:宋体; background: url(../image/bg01.jpg);} 2 | .wrap { margin:0 auto; width:960px; } 3 | h2,h4,p,form,input,ul,li,dl,dt,dd { margin:0; padding:0; } 4 | table { border-collapse:collapse; } 5 | ul { list-style:none; } 6 | a:link, a:visited { color:#039; text-decoration:none; } 7 | a:hover, a:active { color:#03F; text-decoration:underline; } 8 | /* clearFix */ 9 | .clearfix:after{content:".";display:block;height:0;clear:both;visibility:hidden} 10 | .clearfix{ display:block; display:inline-block; } 11 | 12 | .success { background:url(../image/success_ico.gif) 20px 20px no-repeat; } 13 | .success .information { margin-left:180px; } 14 | 15 | #header { } 16 | #header #logo { background:url(../image/logo_ps03.png)no-repeat; width:180px; height:148px; text-indent:-1000em; } 17 | #header #navbar { color:#fff; background:#fc883b; padding:2px; line-height:25px; text-align:right; } 18 | #header .userMenu { float:left; } 19 | #header .userMenu ul li { float:left; margin:0px 2px; display:inline; } 20 | #header .userMenu ul li a { display:block; padding:0px 15px; text-decoration:none; background:#ffe0a3; white-space:nowrap; } 21 | #header .userMenu ul li.current a { font-weight:bold; background:#fff; } 22 | #header #navbar input { margin-right:5px; vertical-align:middle; border:1px solid #ccc; } 23 | #header #navbar input.input-text { padding:2px 3px; width:120px; height:16px; } 24 | #header #navbar input.input-btn { border:none; background:url(../image/button_search.gif); height:22px; width:46px; } 25 | 26 | #footer { border-top:2px solid #ccc; line-height:40px; text-align:center; } 27 | 28 | #login { border:1px solid #666; margin:80px auto; padding:4px; width:360px; } 29 | #login h2 { font-size:14px; line-height:30px; background:url(../image/login_lock.gif) 5px center no-repeat; border-bottom:2px solid #ccc;; padding-left:30px; color:#9a0000; } 30 | #login dl { line-height:50px; padding:5px; } 31 | #login dl dt { float:left; clear:left; width:60px; text-align:right; } 32 | #login dl dd { margin-left:60px; } 33 | #login dl dd input { margin:10px; vertical-align:middle; } 34 | #login dl dd input.input-text { border:1px solid #999999; font-size:14px; height:18px; width:150px; padding:4px 4px; } 35 | #login dl dd.button input { height:26px; border:none; cursor:pointer; } 36 | #login dl dd input.input-btn { background:url(../image/button_login.gif); width:77px; } 37 | #login dl dd input.input-reg { background:url(../image/button_register.gif); width:143px; } 38 | #login dl dd span { color:#f00; } 39 | 40 | #register { border:1px solid #666666; margin:40px auto; padding:4px; width:750px; } 41 | #register .title { background:#e7f6e5; } 42 | #register .title h2 { font-size:20px; color:#666; padding:15px 100px; } 43 | #register .steps { line-height:25px; margin:1px 0; } 44 | #register .steps ul li { float:left; color:#663300; background-color:#ffe0a3; background-position:right; background-repeat:no-repeat; width:375px; text-align:center; } 45 | #register .steps ul li.current { font-weight:bold; background-image:url(../image/step_arrow.gif); } 46 | #register .steps ul li.unpass { color:#000; background-color:#dfdfdf; } 47 | #register .steps ul li.past { background-image:url(../image/step_arrow_past.gif); } 48 | #register .steps ul li.last { font-weight:bold; } 49 | #register form { margin:10px auto; width:480px; } 50 | #register form dl { line-height:50px; padding:20px 0; font-size:14px; color:#333; } 51 | #register form dl dt { float:left; clear:left; width:78px; text-align:right; } 52 | #register form dl dd { margin-left:78px; } 53 | #register form dl dd input { margin:10px; vertical-align:middle; } 54 | #register form dl dd input.input-text { border:1px solid #999999; font-size:14px; height:18px; width:200px; padding:4px 4px; } 55 | #register form dl dd.button input { height:39px; border:none; cursor:pointer; } 56 | #register form dl dd input.input-reg { background:url(../image/button_reg.gif); width:154px; } 57 | #register form dl dd span { color:#f00; } 58 | #register .success { margin:50px auto; width:350px; } 59 | #register .success .information { padding:60px 0; line-height:40px; color:#f00; } 60 | 61 | #content { padding:4px 0; } 62 | #content .list table { width:100%; text-align:center; font-size:14px; color:#3f413e; } 63 | #content .list table tr th { line-height:26px; background:#ffeec2; border-top:1px solid #d3d3d3; border-bottom:1px solid #d3d3d3; } 64 | #content .list th.checker { width:40px; } 65 | #content .list th.price { width:80px; } 66 | #content .list th.store { width:80px; } 67 | #content .list th.view { width:150px; } 68 | #content .list th.nums { width:150px; } 69 | #content .list th.store { width:80px; } 70 | #content .list th.orderId { width:80px; } 71 | #content .list th.userName { width:80px; } 72 | #content .list th.price { width:80px; } 73 | #content .list th.createTime { width:140px; } 74 | #content .list th.status { width:80px; } 75 | #content .bookList table td { padding:10px 0; border-bottom:1px solid #8b8b8b; } 76 | #content .bookList table td.title { text-align:left; font-weight:bold; } 77 | #content .bookList table td.thumb img { border:1px solid #999; } 78 | #content .bookList table tr.odd td { background:#f4faee; } 79 | #content .bookList .input-text { border:1px solid #999; width:30px; text-align:center; } 80 | #content .list .button { padding:10px 0; text-align:right; } 81 | #content .list .button h4 { float:left; color:#f00; font-size:14px; } 82 | #content .list .button .input-gray { font-size:12px; border:none; background:url(../image/button_gray_bg.gif); color:#222; width:125px; height:22px; margin-left:10px; } 83 | #content .bookList .button .input-btn { border:none; background:url(../image/button_shop.gif); width:100px; height:22px; } 84 | #content .bookList .button .input-chart { border:none; background:url(../image/button_chart.png); width:128px; height:36px; } 85 | #content .orderList td { font-size:12px; border:1px solid #e1e1e1; padding:10px 0; } 86 | #content .orderList td.thumb img { border:1px solid #e1e1e1; } 87 | 88 | #content .page-spliter { text-align:center; font-size:14px; padding-top:20px; } 89 | #content .page-spliter * { margin:0 5px; } 90 | #content .page-spliter .current { font-weight:bold; border-bottom:1px solid #000; } 91 | 92 | #content .success { margin:50px auto; width:350px; font-size:14px; } 93 | #content .success .information { padding:60px 0; line-height:40px; color:#f00; } -------------------------------------------------------------------------------- /books/WebContent/image/bg01.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codeYoke/bookStore/8e14a77fe408bbe9b7a470353b1ad2932073a9f5/books/WebContent/image/bg01.jpg -------------------------------------------------------------------------------- /books/WebContent/image/book/book_01.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codeYoke/bookStore/8e14a77fe408bbe9b7a470353b1ad2932073a9f5/books/WebContent/image/book/book_01.gif -------------------------------------------------------------------------------- /books/WebContent/image/book/book_02.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codeYoke/bookStore/8e14a77fe408bbe9b7a470353b1ad2932073a9f5/books/WebContent/image/book/book_02.gif -------------------------------------------------------------------------------- /books/WebContent/image/book/book_03.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codeYoke/bookStore/8e14a77fe408bbe9b7a470353b1ad2932073a9f5/books/WebContent/image/book/book_03.gif -------------------------------------------------------------------------------- /books/WebContent/image/book/book_04.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codeYoke/bookStore/8e14a77fe408bbe9b7a470353b1ad2932073a9f5/books/WebContent/image/book/book_04.gif -------------------------------------------------------------------------------- /books/WebContent/image/book/book_05.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codeYoke/bookStore/8e14a77fe408bbe9b7a470353b1ad2932073a9f5/books/WebContent/image/book/book_05.gif -------------------------------------------------------------------------------- /books/WebContent/image/book/book_06.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codeYoke/bookStore/8e14a77fe408bbe9b7a470353b1ad2932073a9f5/books/WebContent/image/book/book_06.gif -------------------------------------------------------------------------------- /books/WebContent/image/book/book_07.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codeYoke/bookStore/8e14a77fe408bbe9b7a470353b1ad2932073a9f5/books/WebContent/image/book/book_07.gif -------------------------------------------------------------------------------- /books/WebContent/image/book/book_08.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codeYoke/bookStore/8e14a77fe408bbe9b7a470353b1ad2932073a9f5/books/WebContent/image/book/book_08.gif -------------------------------------------------------------------------------- /books/WebContent/image/book/book_09.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codeYoke/bookStore/8e14a77fe408bbe9b7a470353b1ad2932073a9f5/books/WebContent/image/book/book_09.gif -------------------------------------------------------------------------------- /books/WebContent/image/book/book_10.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codeYoke/bookStore/8e14a77fe408bbe9b7a470353b1ad2932073a9f5/books/WebContent/image/book/book_10.gif -------------------------------------------------------------------------------- /books/WebContent/image/button_chart.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codeYoke/bookStore/8e14a77fe408bbe9b7a470353b1ad2932073a9f5/books/WebContent/image/button_chart.png -------------------------------------------------------------------------------- /books/WebContent/image/button_gray_bg.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codeYoke/bookStore/8e14a77fe408bbe9b7a470353b1ad2932073a9f5/books/WebContent/image/button_gray_bg.gif -------------------------------------------------------------------------------- /books/WebContent/image/button_login.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codeYoke/bookStore/8e14a77fe408bbe9b7a470353b1ad2932073a9f5/books/WebContent/image/button_login.gif -------------------------------------------------------------------------------- /books/WebContent/image/button_login副本.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codeYoke/bookStore/8e14a77fe408bbe9b7a470353b1ad2932073a9f5/books/WebContent/image/button_login副本.gif -------------------------------------------------------------------------------- /books/WebContent/image/button_reg.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codeYoke/bookStore/8e14a77fe408bbe9b7a470353b1ad2932073a9f5/books/WebContent/image/button_reg.gif -------------------------------------------------------------------------------- /books/WebContent/image/button_register.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codeYoke/bookStore/8e14a77fe408bbe9b7a470353b1ad2932073a9f5/books/WebContent/image/button_register.gif -------------------------------------------------------------------------------- /books/WebContent/image/button_search.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codeYoke/bookStore/8e14a77fe408bbe9b7a470353b1ad2932073a9f5/books/WebContent/image/button_search.gif -------------------------------------------------------------------------------- /books/WebContent/image/button_shop.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codeYoke/bookStore/8e14a77fe408bbe9b7a470353b1ad2932073a9f5/books/WebContent/image/button_shop.gif -------------------------------------------------------------------------------- /books/WebContent/image/login_lock.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codeYoke/bookStore/8e14a77fe408bbe9b7a470353b1ad2932073a9f5/books/WebContent/image/login_lock.gif -------------------------------------------------------------------------------- /books/WebContent/image/logo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codeYoke/bookStore/8e14a77fe408bbe9b7a470353b1ad2932073a9f5/books/WebContent/image/logo.gif -------------------------------------------------------------------------------- /books/WebContent/image/logo01.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codeYoke/bookStore/8e14a77fe408bbe9b7a470353b1ad2932073a9f5/books/WebContent/image/logo01.jpg -------------------------------------------------------------------------------- /books/WebContent/image/logo_ps03.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codeYoke/bookStore/8e14a77fe408bbe9b7a470353b1ad2932073a9f5/books/WebContent/image/logo_ps03.png -------------------------------------------------------------------------------- /books/WebContent/image/step_arrow.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codeYoke/bookStore/8e14a77fe408bbe9b7a470353b1ad2932073a9f5/books/WebContent/image/step_arrow.gif -------------------------------------------------------------------------------- /books/WebContent/image/step_arrow_past.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codeYoke/bookStore/8e14a77fe408bbe9b7a470353b1ad2932073a9f5/books/WebContent/image/step_arrow_past.gif -------------------------------------------------------------------------------- /books/WebContent/image/success_ico.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codeYoke/bookStore/8e14a77fe408bbe9b7a470353b1ad2932073a9f5/books/WebContent/image/success_ico.gif -------------------------------------------------------------------------------- /books/WebContent/index.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" contentType="text/html; charset=UTF-8" 2 | pageEncoding="UTF-8"%> 3 | 4 | 5 | 6 | 7 | Insert title here 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /books/WebContent/js/valiadate.js: -------------------------------------------------------------------------------- 1 | function ajax(method,url,data,callback,json){ 2 | //1.创建XMLHttpRequest对象 3 | console.log("进入ajax处理!") 4 | var xmlhttp = null; 5 | if(window.XMLHttpRequest){ 6 | // code for IE7+, Firefox, Chrome, Opera, Safari 7 | xmlhttp = new XMLHttpRequest(); 8 | }else{ 9 | // code for IE6, IE5 10 | xmlhttp = new ActiveXObject(); 11 | } 12 | 13 | //2.创建回调函数,在回调函数中处理返回值 14 | xmlhttp.onreadystatechange = function(){ 15 | if(xmlhttp.readyState == 4 && xmlhttp.status == 200){ 16 | //获取服务器的响应,获得字符串形式的响应数据,也可以获取responseXml形式的数据 17 | console.log("正在处理数据!") 18 | var result = xmlhttp.responseText; 19 | if(json !=undefined && json.toUpperCase() == "JSON"){ 20 | var objJson = null; 21 | //如果是ie 22 | if(window.ActiveXObject){ 23 | objJson = eval('('+ result +')') 24 | }else{ 25 | objJson = JSON.parse(result); 26 | } 27 | callback(objJson); 28 | }else{ 29 | callback(result); 30 | } 31 | }else{ 32 | console.log('error!') 33 | } 34 | } 35 | 36 | //3.发送请求(判断是get还是post请求) 37 | if(method.toUpperCase() == "GET"){ 38 | //为了避免缓存的结果情况,请向 URL 添加一个唯一的 ID: 39 | //xmlhttp.open("GET",url+"t=" + Math.random(),true); 40 | 41 | //希望通过 GET 方法发送信息,请向 URL 添加信息: 42 | //xmlhttp.open("GET","demo_get2.asp?fname=Bill&lname=Gates",true); 43 | xmlhttp.open("GET",url,true); 44 | 45 | xmlhttp.send(); 46 | }else{ 47 | //如果有 HTML 表单那样 POST 数据, 48 | //请使用 setRequestHeader() 来添加 HTTP 头。 49 | //然后在 send() 方法中规定您希望发送的数据: 50 | xmlhttp.open("POST",url,true); 51 | xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded"); 52 | xmlhttp.send(data); 53 | } 54 | 55 | } 56 | /** 57 | * 登录 58 | * 检查用户名不能为空 59 | */ 60 | function checkUserName() { 61 | var username = document.getElementById("username").value; 62 | var uname = document.getElementById("uname"); 63 | 64 | var password = document.getElementById("password").value; 65 | var pwd = document.getElementById("pwd"); 66 | 67 | if(username == null || username == "") { 68 | //alert(用户名不能为空); 69 | uname.innerHTML = "用户名不能为空"; 70 | return false; 71 | } 72 | if(password == null || password == "") { 73 | pwd.innerHTML = "密码不能为空"; 74 | return false; 75 | } 76 | return true; 77 | } 78 | 79 | /** 80 | * 注册 81 | * 用户名是否合法 82 | * @returns {Boolean} 83 | */ 84 | function isUsernameLegal() { 85 | /* 用户名是否填写 */ 86 | var username = document.getElementById("username").value; 87 | var usernull = document.getElementById("usernull"); 88 | if(username == null || username == "") { 89 | usernull.innerHTML = "用户名不能为空!"; 90 | return false; 91 | } 92 | 93 | /* 用户名是否已存在 alreadyExsits*/ 94 | isExists(); 95 | 96 | usernull.innerHTML = ""; //输入合法后消除提示 97 | return true; 98 | } 99 | 100 | /** 101 | * Ajax检查用户是否已经被注册 102 | * 异步:发送请求时不等返回结果,由回调函数处理结果 103 | * @returns {Boolean} 104 | */ 105 | function isExists() { 106 | var username = document.getElementById("username").value; //获取用户名 107 | ajax("post","RegisterServlet?action=check&username="+username,"",function(data){ 108 | if(data == "true"){ 109 | usernull.innerHTML = "当前用户名已被注册!"; 110 | return false; 111 | } else if(data == "false") { 112 | usernull.innerHTML = "当前用户名可用!"; 113 | return true; 114 | } 115 | 116 | }) 117 | } 118 | /*function isExists() { 119 | var xmlHttp; //定义 xmlHttp XmlHttpRequest对象从服务器异步获取数据后通过javascript修改页面局部信息 120 | try { 121 | //根据不同浏览器初始化不同的xmlHttp 122 | //IE6以上浏览器 123 | xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); 124 | } catch (e) { 125 | try { 126 | //FireFox 127 | xmlHttp = new XMLHttpRequest(); 128 | } catch (e) { 129 | try { 130 | //IE5.5+ 131 | xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); 132 | } catch (e) { 133 | alert("您的浏览器不支持Ajax"); 134 | return false; 135 | } 136 | } 137 | } 138 | var username = document.getElementById("username").value; //获取用户名 139 | *//** 140 | * open(提交方式[get|post],url(servlet路径),同步或异步[false|true]) 141 | * RegisterServlet: servlet路径 142 | * 打开和后台服务器的链接 143 | *//* 144 | xmlHttp.open("POST", "RegisterServlet?action=check&username="+username, true); //路径中不能有空格 145 | xmlHttp.send(null); //传送数据 146 | *//** 147 | * onreadystatechange:调用回调函数 148 | * readyState:请求状态,代码是未初始化,1:初始化,2:发送请求,3:开始接受数据,4:接受结果完毕 149 | * status: http状态码 200成功,404路径错误,500后台代码错误, 150 | *//* 151 | xmlHttp.onreadystatechange = function() { 152 | if(xmlHttp.readyState==4 && xmlHttp.status==200) { 153 | var usernull = document.getElementById("usernull"); 154 | var result = xmlHttp.responseText; //接受服务器端传过来的数据,写出来的数据都是String类型 155 | if(result == "true") { 156 | usernull.innerHTML = "当前用户名已被注册"; 157 | return false; 158 | } else if(result == "false") { 159 | usernull.innerHTML = "当前用户名可用"; 160 | return true; 161 | } 162 | } 163 | } 164 | } 165 | */ 166 | 167 | 168 | 169 | /** 170 | * 注册 171 | * 注册密码是否合法 172 | * @returns {Boolean} 173 | */ 174 | function isPasswordLegal() { 175 | var password = document.getElementById("password").value; 176 | var nullpassword = document.getElementById("nullpassword"); 177 | if(password == null || password == "") { 178 | nullpassword.innerHTML = "密码不能为空"; 179 | return false; 180 | } else if(password.length < 8) { 181 | nullpassword.innerHTML = "密码至少八个字符"; 182 | //document.getElementById("font").style.color= "red"; 183 | return false; 184 | } 185 | 186 | nullpassword.innerHTML = ""; //输入合法后消除提示 187 | return true; 188 | } 189 | 190 | /** 191 | * 注册 192 | * 重新确认注册密码 193 | * @returns {Boolean} 194 | */ 195 | function isRepasswordLegal() { 196 | var password = document.getElementById("password").value; 197 | 198 | var rePassword = document.getElementById("rePassword").value; 199 | var nullrePassword = document.getElementById("nullrePassword"); 200 | if(password != rePassword) { 201 | nullrePassword.innerHTML = "前后密码不唯一"; 202 | return false; 203 | } 204 | 205 | nullrePassword.innerHTML = ""; //输入合法后消除提示 206 | return true; 207 | } 208 | 209 | /** 210 | * 注册 211 | * 邮箱验证 212 | * @returns {Boolean} 213 | */ 214 | function isEmailLegal() { 215 | var email = document.getElementById("email").value; 216 | var emailnull = document.getElementById("nullemail"); 217 | var regEx = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z_-]+)+$/; 218 | if(email == null || email == "") { 219 | emailnull.innerHTML = "邮箱不能为空"; 220 | return false; 221 | }else if(!regEx.test(email)) { 222 | emailnull.innerHTML = "邮箱格式不正确"; 223 | return false; 224 | }else { 225 | emailnull.innerHTML=""; //将值清空 226 | } 227 | return true; 228 | } 229 | 230 | /** 231 | * 注册检验 232 | * @returns {Boolean} 233 | */ 234 | function checkRegister() { 235 | if(isUsernameLegal() && isPasswordLegal() && isRepasswordLegal() && isEmailLegal()) { 236 | return true; 237 | } 238 | return false; 239 | } 240 | 241 | 242 | 243 | 244 | 245 | 246 | -------------------------------------------------------------------------------- /books/WebContent/login.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" contentType="text/html; charset=UTF-8" 2 | pageEncoding="UTF-8"%> 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 网上书城 11 | 12 | 13 | 18 |
19 |

用户登陆

20 |
21 |
22 |
用户名:
23 |
24 |
密 码:
25 |
26 |
 
27 |
28 |
29 |
30 |
31 | 34 | <%-- --%> 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /books/WebContent/logout.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" contentType="text/html; charset=UTF-8" 2 | pageEncoding="UTF-8"%> 3 | <%--用户退出登录 --%> 4 | <% 5 | request.getSession().removeAttribute("loginuser"); 6 | request.getSession().removeAttribute("bookcart"); 7 | request.getSession().removeAttribute("bookcart_count"); 8 | response.sendRedirect("login.jsp"); 9 | %> -------------------------------------------------------------------------------- /books/WebContent/main.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" contentType="text/html; charset=UTF-8" 2 | pageEncoding="UTF-8"%> 3 | 4 | <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 5 | 6 | <%@ page language="java" import="java.util.*" %> 7 | 8 | 9 | 10 | 11 | 12 | 鲲鹏网上书城 13 | 14 | 15 | 16 | 17 | 35 | 36 | 37 | 38 | <% 39 | String bookname = (String)request.getAttribute("keywords"); 40 | String username = (String)session.getAttribute("loginuser"); 41 | if(username == null) { 42 | response.sendRedirect("login.jsp"); 43 | } 44 | 45 | int no = 0; 46 | int totalPage = 0; 47 | List books = (List)request.getAttribute("books"); 48 | if(request.getAttribute("current") != null) { 49 | no = (Integer)request.getAttribute("current");//获取当前页面数 50 | totalPage = (Integer)request.getAttribute("totalPage");//获取总页面数 51 | } 52 | %> 53 | 54 | 55 | 56 | 57 | 58 |
59 |
60 |
61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 |
书名价格库存图片预览
${book.bookname }¥${book.b_price } ${book.stock }
85 | <%-- <%out.println(request.getAttribute("current")); %> --%> 86 | 87 | <%if(request.getAttribute("current") != null) { %> 88 | 89 |
90 | 首页 91 | <%for(int i = 1; i <= totalPage; i++) { %> 92 | <%if(i==no) { %> 93 | <%=i %> 94 | <%continue;} %> 95 | <%=i %> 96 | <%} %> 97 | 尾页 98 |
99 | 100 | <%} %> 101 | 102 |
103 |
104 |
105 |
106 | 107 | 110 | 111 | 112 | <%-- 113 | 114 | 123 | 124 | 132 | 133 | 148 | 149 | --%> 150 | -------------------------------------------------------------------------------- /books/WebContent/main_head.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" contentType="text/html; charset=UTF-8" 2 | pageEncoding="UTF-8"%> 3 | 4 | 5 | 6 | 7 | Insert title here 8 | 9 | 10 | 11 | 12 | <% 13 | String bookname = request.getParameter("bookname"); 14 | String username = (String)session.getAttribute("loginuser"); 15 | if(username == null) { 16 | response.sendRedirect("login.jsp"); 17 | } 18 | %> 19 | 20 | 21 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /books/WebContent/orderlist.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" contentType="text/html; charset=UTF-8" 2 | pageEncoding="UTF-8"%> 3 | <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 4 | <%@page import="com.entity.Item" %> 5 | <%@page import="java.util.*" %> 6 | 7 | 8 | 9 | 10 | 11 | Insert title here 12 | 13 | 14 | 15 | 16 | <% 17 | String username = (String)session.getAttribute("loginuser"); 18 | if(username == null) { 19 | response.sendRedirect("login.jsp"); 20 | } 21 | %> 22 | 23 | 24 | 25 | <% 26 | List orders = (List) request.getAttribute("orders"); 27 | %> 28 | 29 |
30 |
31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 |
订单编号收货人下单时间总价订单商品商品名称商品单价商品数量
${order.oid }${order.username }${order.createdate }${order.total_price }${order.bookname }${order.b_price }${order.total_price/order.b_price }
56 |
57 |
58 | 61 | 62 | 63 | -------------------------------------------------------------------------------- /books/WebContent/register.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" contentType="text/html; charset=UTF-8" 2 | pageEncoding="UTF-8"%> 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 20 | 21 |
22 |
23 |

欢迎注册鲲鹏网上书城

24 |
25 |
26 |
    27 |
  • 1.填写注册信息
  • 28 |
  • 2.注册成功
  • 29 |
30 |
31 |
32 |
33 |
用 户 名:
34 |
35 |
密  码:
36 |
密码至少8位
37 |
确认密码:
38 |
39 |
Email地址:
40 |
请输入正确格式的邮箱
41 |
42 |
43 |
44 |
45 |
46 | 47 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /books/WebContent/register_success.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" contentType="text/html; charset=UTF-8" 2 | pageEncoding="UTF-8"%> 3 | 4 | 5 | 6 | 7 | 鲲鹏网上商城 8 | 9 | 10 | 11 | <% 12 | String username = (String)session.getAttribute("loginuser"); 13 | if(username == null) { 14 | response.sendRedirect("login.jsp"); 15 | } 16 | %> 17 | 18 | 26 |
27 |
28 |

欢迎注册鲲鹏网上商城

29 |
30 |
31 |
    32 |
  • 1.填写注册信息
  • 33 |
  • 2.注册成功
  • 34 |
35 |
36 |
37 |
38 |

恭喜:注册成功!

39 |

点此进入用户中心>>

40 |
41 |
42 |
43 | 46 | 47 | -------------------------------------------------------------------------------- /books/WebContent/shopping.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" contentType="text/html; charset=UTF-8" 2 | pageEncoding="UTF-8"%> 3 | 4 | <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 5 | <%@page language="java" import="java.util.*" %> 6 | 7 | <%@ page import="com.entity.Book" %> 8 | 9 | 10 | 11 | 12 | 13 | Insert title here 14 | 15 | 16 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | <%-- 125 | <% 126 | List bookcart = (List)request.getSession().getAttribute("bookcart"); 127 | 128 | double allPrice = 0; 129 | double oneAllPrice = 0; 130 | if(bookcart != null) { //购物车非空 131 | for(int i = 0; i< bookcart.size(); i++) { 132 | Book book = (Book)bookcart.get(i); 133 | oneAllPrice = book.getCount()*book.getPrice(); 134 | allPrice += oneAllPrice; 135 | } 136 | } 137 | request.setAttribute("allPrice", allPrice); 138 | %> 139 | --%> 140 | 141 | <% 142 | String username = (String)session.getAttribute("loginuser");//用户先登录才可访问购物车 143 | if(username == null) { 144 | response.sendRedirect("login.jsp"); 145 | } 146 | %> 147 | 148 |
149 |
150 |
151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 166 | 167 | 168 | 170 | 172 | 173 | 174 | 175 | 176 | 177 | 178 |
图片预览书名数量价格操作
${book.bookname }移除
179 | 180 |
181 |

总价:¥

<%--<%=request.getAttribute("allPrice") %> --%> 182 | 183 | 184 |
185 |
186 |
187 |
188 | 189 | 190 | -------------------------------------------------------------------------------- /books/WebContent/shopping_success.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" contentType="text/html; charset=UTF-8" 2 | pageEncoding="UTF-8"%> 3 | 4 | 5 | 6 | 7 | Insert title here 8 | 9 | 鲲鹏网上书城 10 | 11 | 12 | 13 | 14 | <% 15 | String username = (String)session.getAttribute("loginuser"); 16 | if(username == null) { 17 | response.sendRedirect("login.jsp"); 18 | } 19 | %> 20 | 21 | 22 | 23 | 24 |
25 |
26 |
27 |

恭喜:添加成功!

28 |

点此查看购物车详情>>

29 |
30 |
31 |
32 | 33 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /books/bookstore.sql: -------------------------------------------------------------------------------- 1 | # Host: localhost (Version: 5.5.27) 2 | # Date: 2018-05-02 17:01:03 3 | # Generator: MySQL-Front 5.3 (Build 4.9) 4 | 5 | /*!40101 SET NAMES utf8 */; 6 | 7 | # 8 | # Source for table "books" 9 | # 10 | create database books; 11 | use books; 12 | 13 | DROP TABLE IF EXISTS `books`; 14 | CREATE TABLE `books` ( 15 | `BID` int(11) NOT NULL DEFAULT '0' COMMENT '主键', 16 | 17 | `BOOKNAME` varchar(50) DEFAULT NULL, 18 | 19 | `B_PRICE` varchar(10) DEFAULT NULL, 20 | 21 | `IMAGE` varchar(100) DEFAULT NULL, 22 | 23 | `STOCK` int(11) DEFAULT NULL COMMENT '库存数', 24 | 25 | PRIMARY KEY (`BID`) 26 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8; 27 | 28 | # 29 | # Data for table "books" 30 | # 31 | 32 | INSERT INTO `books` VALUES (1,'php','35.00','image/book/book_02.gif',88), 33 | (2,'C++','36.00','image/book/book_03.gif',31), 34 | (3,'java','55.00','image/book/book_01.gif',32), 35 | (4,'python','80.00','image/book/book_04.gif',79), 36 | (5,'NET','28.00','image/book/book_05.gif',12), 37 | (6,'数据结构','55.50','image/book/book_06.gif',66), 38 | (7,'数据库','34.00','image/book/book_07.gif',34), 39 | (8,'疯狂Java','45.00','image/book/book_08.gif',22), 40 | (9,'java web','57.00','image/book/book_09.gif',24), 41 | (10,'tensorflow','99.9','image/book/book_10.gif',33), 42 | (11,'java 面向对象','54.00','image/book/book_02.gif',68); 43 | 44 | # 45 | # Source for table "items" 46 | # 47 | 48 | DROP TABLE IF EXISTS `items`; 49 | CREATE TABLE `items` ( 50 | `IID` int(11) NOT NULL DEFAULT '0', 51 | 52 | `OID` int(11) DEFAULT NULL, 53 | `BID` int(11) DEFAULT NULL, 54 | 55 | `CREATEDATE` datetime DEFAULT NULL, 56 | 57 | `COUNT` int(11) DEFAULT NULL COMMENT '购买数量', 58 | 59 | `PRICE` double DEFAULT NULL COMMENT '单价', 60 | 61 | `STATE` int(11) DEFAULT NULL, 62 | 63 | `TOTAL_PRICE` double DEFAULT NULL COMMENT '总价', 64 | 65 | PRIMARY KEY (`IID`) 66 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8; 67 | 68 | # 69 | # Data for table "items" 70 | # 71 | 72 | #INSERT INTO `items` VALUES (1,1,2,'2018-07-02 09:16:15',1,36,0,36),(2,2,2,'2018-07-02 09:16:25',1,36,0,36),(3,2,3,'2018-07-02 09:16:25',1,55,0,55),(4,3,2,'2018-07-02 10:21:14',2,36,0,72),(5,4,2,'2018-07-02 11:09:22',3,36,0,108),(6,4,3,'2018-07-02 11:09:22',1,55,0,55),(7,5,2,'2018-07-02 11:09:39',1,36,0,36),(8,6,2,'2018-07-02 16:01:36',1,36,0,36),(9,6,9,'2018-07-02 16:01:36',34,57,0,1938); 73 | 74 | # 75 | # Source for table "orders" 76 | # 77 | 78 | DROP TABLE IF EXISTS `orders`; 79 | CREATE TABLE `orders` ( 80 | `OID` int(11) DEFAULT NULL, 81 | 82 | `USERNAME` varchar(50) DEFAULT NULL, 83 | 84 | KEY `OID` (`OID`) 85 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8; 86 | 87 | # 88 | # Data for table "orders" 89 | # 90 | 91 | #INSERT INTO `orders` VALUES (1,'1'),(2,'1'),(3,'2'),(4,'1'),(5,'1'),(6,'1'); 92 | 93 | # 94 | # Source for table "userinfo" 95 | # 96 | 97 | DROP TABLE IF EXISTS `userinfo`; 98 | CREATE TABLE `userinfo` ( 99 | `username` varchar(50) NOT NULL DEFAULT '', 100 | 101 | `password` varchar(50) DEFAULT NULL, 102 | 103 | `email` varchar(50) DEFAULT NULL, 104 | 105 | PRIMARY KEY (`username`) 106 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8; 107 | 108 | # 109 | # Data for table "userinfo" 110 | # 111 | 112 | #INSERT INTO `userinfo` VALUES ('','',''),('1','1','1@qq.com'),('2','12345678','123@qq.com'),('3','12345678','123@qq.com'),('4','12345678','123@qq.com'),('5','123','123@qq.com'),('6','12345678','123@qq.com'),('7','12345678','123@qq.com'),('北','12345678','123@qq.com'),('东','12345678','123@qq.com'),('西','12345678','123@qq.com'),('kpit','123456','123@qq.com'),('南','12345678','123@qq.com'); 113 | -------------------------------------------------------------------------------- /books/image/img01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codeYoke/bookStore/8e14a77fe408bbe9b7a470353b1ad2932073a9f5/books/image/img01.png -------------------------------------------------------------------------------- /books/image/img02.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codeYoke/bookStore/8e14a77fe408bbe9b7a470353b1ad2932073a9f5/books/image/img02.png -------------------------------------------------------------------------------- /books/image/img03.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codeYoke/bookStore/8e14a77fe408bbe9b7a470353b1ad2932073a9f5/books/image/img03.png -------------------------------------------------------------------------------- /books/image/img04.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codeYoke/bookStore/8e14a77fe408bbe9b7a470353b1ad2932073a9f5/books/image/img04.png -------------------------------------------------------------------------------- /books/image/img05.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codeYoke/bookStore/8e14a77fe408bbe9b7a470353b1ad2932073a9f5/books/image/img05.png -------------------------------------------------------------------------------- /books/image/img06.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codeYoke/bookStore/8e14a77fe408bbe9b7a470353b1ad2932073a9f5/books/image/img06.png -------------------------------------------------------------------------------- /books/image/img07.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codeYoke/bookStore/8e14a77fe408bbe9b7a470353b1ad2932073a9f5/books/image/img07.png -------------------------------------------------------------------------------- /books/image/img08.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codeYoke/bookStore/8e14a77fe408bbe9b7a470353b1ad2932073a9f5/books/image/img08.png -------------------------------------------------------------------------------- /books/image/img09.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codeYoke/bookStore/8e14a77fe408bbe9b7a470353b1ad2932073a9f5/books/image/img09.png -------------------------------------------------------------------------------- /books/image/img10.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codeYoke/bookStore/8e14a77fe408bbe9b7a470353b1ad2932073a9f5/books/image/img10.png -------------------------------------------------------------------------------- /books/image/img11.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codeYoke/bookStore/8e14a77fe408bbe9b7a470353b1ad2932073a9f5/books/image/img11.png -------------------------------------------------------------------------------- /books/image/img12.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codeYoke/bookStore/8e14a77fe408bbe9b7a470353b1ad2932073a9f5/books/image/img12.png -------------------------------------------------------------------------------- /books/image/img13.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codeYoke/bookStore/8e14a77fe408bbe9b7a470353b1ad2932073a9f5/books/image/img13.png -------------------------------------------------------------------------------- /books/image/img14.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codeYoke/bookStore/8e14a77fe408bbe9b7a470353b1ad2932073a9f5/books/image/img14.png -------------------------------------------------------------------------------- /books/image/img15.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codeYoke/bookStore/8e14a77fe408bbe9b7a470353b1ad2932073a9f5/books/image/img15.png -------------------------------------------------------------------------------- /books/image/phone01.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codeYoke/bookStore/8e14a77fe408bbe9b7a470353b1ad2932073a9f5/books/image/phone01.jpg -------------------------------------------------------------------------------- /books/image/phone01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codeYoke/bookStore/8e14a77fe408bbe9b7a470353b1ad2932073a9f5/books/image/phone01.png -------------------------------------------------------------------------------- /books/image/phone02.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codeYoke/bookStore/8e14a77fe408bbe9b7a470353b1ad2932073a9f5/books/image/phone02.jpg -------------------------------------------------------------------------------- /books/image/phone03.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codeYoke/bookStore/8e14a77fe408bbe9b7a470353b1ad2932073a9f5/books/image/phone03.jpg -------------------------------------------------------------------------------- /books/src/com/biz/BookBiz.java: -------------------------------------------------------------------------------- 1 | package com.biz; 2 | 3 | import java.util.List; 4 | 5 | import com.dao.BookDao; 6 | import com.entity.Book; 7 | 8 | /** 9 | * 业务逻辑层 10 | * @author Administrator 11 | * 12 | */ 13 | public interface BookBiz { 14 | /** 15 | * 根据书名模糊查询 16 | * @param bookname 17 | * @return List集合 18 | */ 19 | List findBookByName(String bookname,int page_books, int page_No); 20 | 21 | //接口中所有方法默认为public 22 | 23 | /** 24 | * 25 | * @Title: findGoodsByName 26 | * @Description: 通过关键字模糊查询所有商品记录 27 | * @param goodsName 搜索框关键字 28 | * @return 返回记录条数 29 | */ 30 | public int findBookByName(String goodsName); 31 | /** 32 | * 查询所有书籍数 33 | * @return 34 | */ 35 | public int count(); 36 | 37 | public void setBookdao(BookDao bookdao); 38 | /** 39 | * 根据分页查询所有图书 40 | * @param page_books 每页显示条数 41 | * @param page_No 当前显示页面 42 | * @return 43 | */ 44 | List findAll(int page_books, int page_No); 45 | 46 | /** 47 | * 修改商品库存,防止库存为负数 48 | * @param bid 根据商品id修改 49 | * @param count 数量 50 | * @return 51 | */ 52 | public boolean changeStock(int bid, String count); 53 | } 54 | -------------------------------------------------------------------------------- /books/src/com/biz/ItemBiz.java: -------------------------------------------------------------------------------- 1 | package com.biz; 2 | 3 | import com.dao.ItemDao; 4 | import com.entity.Item; 5 | 6 | public interface ItemBiz { 7 | /** 8 | * 添加订单详细信息 9 | * @param item 10 | * @return 11 | */ 12 | public boolean addItemToOrder(Item item); 13 | /** 14 | * 初始化订单信息 15 | * @param itemdao 16 | */ 17 | public void setItemdao(ItemDao itemdao); 18 | 19 | public int count(); 20 | } 21 | -------------------------------------------------------------------------------- /books/src/com/biz/OrderBiz.java: -------------------------------------------------------------------------------- 1 | package com.biz; 2 | 3 | import java.util.List; 4 | 5 | import com.dao.OrderDao; 6 | import com.entity.Order; 7 | 8 | /** 9 | * 10 | * @author Administrator 11 | * 12 | */ 13 | public interface OrderBiz { 14 | /** 15 | * 添加新的订单 16 | * @param order 17 | * @return 18 | */ 19 | public boolean newOrder(Order order); 20 | /** 21 | * 根据用户名查询相应的订单 22 | * @param username 用户名 23 | * @param page_orders 每页显示条数 24 | * @param page_No 当前第几页 25 | * @return list 26 | */ 27 | public List findByUsername(String username, int page_orders, int page_No); 28 | /** 29 | * 初始化orderdao 30 | * @param orderdao 31 | * @return 32 | */ 33 | public void setOrderdao(OrderDao orderdao); 34 | /** 35 | * 根据用户名获取当前oid 36 | * @param username 37 | * @return 38 | */ 39 | public String getCurrentOid(String username); 40 | /** 41 | * 获取总记录数 42 | * @return 43 | */ 44 | public int count(); 45 | 46 | 47 | } 48 | -------------------------------------------------------------------------------- /books/src/com/biz/UserBiz.java: -------------------------------------------------------------------------------- 1 | package com.biz; 2 | 3 | import java.util.List; 4 | 5 | import com.dao.UserDao; 6 | import com.entity.UserInfo; 7 | 8 | public interface UserBiz { 9 | /** 10 | * 查询方法 11 | * @param sql 12 | * @return 13 | */ 14 | public boolean checkLogin(String username,String password); 15 | public void setUserDao(UserDao userdao); 16 | /** 17 | * 注册、添加用户 18 | * @param userinfo 19 | * @return 20 | */ 21 | public boolean addUser(UserInfo userinfo); 22 | 23 | /** 24 | * 判断用户是否已存在 25 | * @param username 26 | * @return 27 | */ 28 | public boolean userExist(String username); 29 | } 30 | -------------------------------------------------------------------------------- /books/src/com/biz/impl/BookBizImpl.java: -------------------------------------------------------------------------------- 1 | package com.biz.impl; 2 | 3 | import java.sql.SQLException; 4 | import java.util.List; 5 | 6 | import com.biz.BookBiz; 7 | import com.dao.BaseDao; 8 | import com.dao.BookDao; 9 | import com.entity.Book; 10 | 11 | public class BookBizImpl implements BookBiz { 12 | BookDao bookdao = null; 13 | BaseDao basedao = new BaseDao(); 14 | 15 | @Override 16 | public List findBookByName(String bookname,int page_books, int page_No) { 17 | String sql = "select * from books where bookname like '%"+bookname+"%' limit "+(page_No-1)*page_books+","+page_books; //模糊匹配查询 18 | return bookdao.query(sql); 19 | } 20 | 21 | @Override 22 | public int count() { 23 | return bookdao.count(); 24 | } 25 | 26 | @Override 27 | public void setBookdao(BookDao bookdao) { 28 | this.bookdao = bookdao; 29 | } 30 | 31 | @Override 32 | public List findAll(int page_books, int page_No) { 33 | //分页查询,MySQL中limit子句指定偏移量查询 34 | String sql = "select * from books where bid limit "+(page_No-1)*page_books+","+page_books; 35 | return bookdao.query(sql); 36 | } 37 | 38 | @Override 39 | public boolean changeStock(int bid, String count) { 40 | boolean flag = false; 41 | String sql = "update books set stock=stock+"+count+" where bid="+bid; 42 | try { 43 | if(basedao.queryStock(bid) > 0) { 44 | flag = bookdao.update(sql)>0?true:false; 45 | } 46 | } catch (SQLException e) { 47 | // TODO Auto-generated catch block 48 | e.printStackTrace(); 49 | } 50 | 51 | return flag; 52 | } 53 | 54 | @Override 55 | public int findBookByName(String booksName) { 56 | String sql = "select count(*) from books where bookname like '%"+booksName+"%'"; 57 | int countByName = bookdao.count(sql); 58 | return countByName; 59 | } 60 | 61 | 62 | } 63 | -------------------------------------------------------------------------------- /books/src/com/biz/impl/ItemBizImpl.java: -------------------------------------------------------------------------------- 1 | package com.biz.impl; 2 | 3 | import com.biz.ItemBiz; 4 | import com.dao.ItemDao; 5 | import com.entity.Item; 6 | 7 | public class ItemBizImpl implements ItemBiz { 8 | private ItemDao itemdao; 9 | 10 | 11 | public ItemDao getItemdao() { 12 | return itemdao; 13 | } 14 | 15 | @Override 16 | public boolean addItemToOrder(Item item) { 17 | 18 | return itemdao.insert(item)>0? true:false; 19 | } 20 | 21 | @Override 22 | public void setItemdao(ItemDao itemdao) { 23 | this.itemdao = itemdao; 24 | } 25 | 26 | @Override 27 | public int count() { 28 | return itemdao.count(); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /books/src/com/biz/impl/OrderBizImpl.java: -------------------------------------------------------------------------------- 1 | package com.biz.impl; 2 | 3 | import java.util.List; 4 | import java.util.Map; 5 | 6 | import com.biz.OrderBiz; 7 | import com.dao.OrderDao; 8 | import com.entity.Order; 9 | 10 | public class OrderBizImpl implements OrderBiz { 11 | private OrderDao orderdao = null; 12 | 13 | 14 | public OrderDao getOrderdao() { 15 | return orderdao; 16 | } 17 | 18 | @Override 19 | public boolean newOrder(Order order) { 20 | int i = orderdao.insert(order); 21 | 22 | return i>0?true:false; 23 | } 24 | 25 | @Override 26 | public List findByUsername(String username, int page_orders, int page_No) { 27 | String sql = "select * from orders o,items i,books b where o.oid=i.oid and b.bid=i.bid order by i.oid"; 28 | 29 | return orderdao.query(sql); 30 | } 31 | 32 | @Override 33 | public void setOrderdao(OrderDao orderdao) { 34 | this.orderdao = orderdao; 35 | } 36 | 37 | @Override 38 | public String getCurrentOid(String username) { 39 | String sql = "select * from orders where username='"+username+"' order by oid desc"; 40 | List orders = orderdao.queryByusername(sql); 41 | Map order = (Map) orders.get(0);//desc排序了,取当前最大的id 42 | 43 | return order.get("oid")+""; 44 | } 45 | 46 | @Override 47 | public int count() { 48 | return orderdao.count(); 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /books/src/com/biz/impl/UserBizImpl.java: -------------------------------------------------------------------------------- 1 | package com.biz.impl; 2 | 3 | import java.util.List; 4 | 5 | import com.biz.UserBiz; 6 | import com.dao.UserDao; 7 | import com.entity.UserInfo; 8 | 9 | public class UserBizImpl implements UserBiz { 10 | private UserDao userDao = null; 11 | 12 | public UserDao getUserDao() { 13 | return userDao; 14 | } 15 | 16 | 17 | 18 | 19 | @Override 20 | public boolean checkLogin(String username,String password) { 21 | 22 | String sql = "select * from userinfo where username='"+username+"'and password='"+password+"'"; 23 | List list = userDao.query(sql); 24 | 25 | return list.size()>0 ? true : false; 26 | } 27 | 28 | 29 | 30 | 31 | @Override 32 | public void setUserDao(UserDao userdao) { 33 | // TODO Auto-generated method stub 34 | this.userDao = userdao; 35 | } 36 | 37 | 38 | 39 | 40 | @Override 41 | public boolean addUser(UserInfo userinfo) { 42 | int i = userDao.insert(userinfo); 43 | 44 | return i > 0 ? true : false; 45 | } 46 | 47 | 48 | 49 | 50 | @Override 51 | public boolean userExist(String username) { 52 | String sql = "select * from userinfo where username='"+username+"'"; 53 | List list = userDao.query(sql); 54 | System.out.println(sql); 55 | return list.size()>0?true:false; //如果查询有数据则已经被注册 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /books/src/com/dao/BaseDao.java: -------------------------------------------------------------------------------- 1 | package com.dao; 2 | 3 | import java.sql.Connection; 4 | import java.sql.DriverManager; 5 | import java.sql.PreparedStatement; 6 | import java.sql.ResultSet; 7 | import java.sql.SQLException; 8 | import java.util.ArrayList; 9 | import java.util.HashMap; 10 | import java.util.List; 11 | import java.util.Map; 12 | 13 | public class BaseDao { 14 | 15 | /*public static void main(String[] args) { 16 | BaseDao b = new BaseDao(); 17 | b.openConnection(); 18 | }*/ 19 | 20 | protected Connection conn = null; 21 | protected PreparedStatement ps = null; 22 | protected ResultSet rs = null; 23 | 24 | /** 25 | * 鎵撳紑鏁版嵁搴撻摼鎺� 26 | */ 27 | public void openConnection() { 28 | String driver = "com.mysql.jdbc.Driver"; 29 | String url = "jdbc:mysql://localhost:3306/books?characterEncoding=utf-8"; 30 | String user = "root"; 31 | String password = "123456"; 32 | 33 | try { 34 | //鍔犺浇椹卞姩 35 | Class.forName(driver); 36 | //閾炬帴鏁版嵁搴� 37 | conn = DriverManager.getConnection(url, user, password); 38 | System.out.println("getConnection"); 39 | 40 | } catch (Exception e) { 41 | e.printStackTrace(); 42 | } 43 | } 44 | 45 | /** 46 | * 鍏抽棴鏁版嵁搴撻摼鎺� 47 | */ 48 | public void closeConnection() { 49 | 50 | try { 51 | if(rs != null) { 52 | rs.close(); 53 | } 54 | if(ps != null) { 55 | ps.close(); 56 | } 57 | if(conn != null) { 58 | conn.close(); 59 | } 60 | 61 | } catch (Exception e) { 62 | e.printStackTrace(); 63 | } 64 | 65 | } 66 | 67 | 68 | /** 69 | * 鎵ц鎵�鏈夎〃鐨勬彃鍏ユ搷浣� 70 | * String 71 | * StringBuffer 72 | * StringBuilder 73 | * @param table 74 | * @param list 75 | * @return 76 | */ 77 | public int insert(String table, List list) { 78 | int i = 0; 79 | //鎿嶄綔鐩稿搴旇〃鐨剆ql璇彞 80 | StringBuffer sql = null; //StringBuffer鏄嚎绋嬪畨鍏ㄧ殑 81 | if(table.equals("userinfo")) { 82 | sql = new StringBuffer("insert into "+table+" values("); 83 | } else { 84 | sql = new StringBuffer("insert into "+table+" values("+list.get(0)+","); 85 | list.remove(0); 86 | } 87 | for(int j = 0; j query(String sql); 19 | /** 20 | * 21 | * @param userinfo 22 | * @return 23 | */ 24 | public int insert(UserInfo userinfo); 25 | 26 | // public int deleteOrUpdate(String sql); 27 | // public int insert(String sql); 28 | 29 | } 30 | -------------------------------------------------------------------------------- /books/src/com/dao/impl/BookDaoImpl.java: -------------------------------------------------------------------------------- 1 | package com.dao.impl; 2 | 3 | import java.util.List; 4 | 5 | import com.dao.BaseDao; 6 | import com.dao.BookDao; 7 | 8 | public class BookDaoImpl extends BaseDao implements BookDao { 9 | 10 | @Override 11 | public List query(String sql) { 12 | String[] column = {"bid","bookname","b_price","image","stock"}; 13 | return query(sql, column); 14 | } 15 | 16 | @Override 17 | public int count() { 18 | String sql = "select count(*) from books"; //查询books的总条数 19 | openConnection(); 20 | int i = 0; 21 | try { 22 | ps = conn.prepareStatement(sql); 23 | rs = ps.executeQuery(); 24 | while(rs.next()) { 25 | i = rs.getInt(1); 26 | } 27 | 28 | } catch (Exception e) { 29 | e.printStackTrace(); 30 | } finally { 31 | closeConnection(); 32 | } 33 | 34 | return i; 35 | } 36 | 37 | @Override 38 | public int update(String sql) { 39 | return deleteOrUpdate(sql); 40 | } 41 | 42 | @Override 43 | public int count(String sql) { 44 | 45 | openConnection(); 46 | int i = 0; 47 | try { 48 | ps = conn.prepareStatement(sql); 49 | rs = ps.executeQuery(); 50 | while(rs.next()) { 51 | i = rs.getInt(1); 52 | } 53 | 54 | } catch (Exception e) { 55 | e.printStackTrace(); 56 | } finally { 57 | closeConnection(); 58 | } 59 | 60 | return i; 61 | } 62 | 63 | /** 64 | * 测试count方法 65 | * @param args 66 | */ 67 | /*public static void main(String[] args) { 68 | BookDaoImpl b = new BookDaoImpl(); 69 | int a = b.count(); 70 | System.out.println(a); 71 | }*/ 72 | 73 | } 74 | -------------------------------------------------------------------------------- /books/src/com/dao/impl/ItemDaoImpl.java: -------------------------------------------------------------------------------- 1 | package com.dao.impl; 2 | 3 | import java.sql.Timestamp; 4 | import java.text.SimpleDateFormat; 5 | import java.util.ArrayList; 6 | import java.util.Date; 7 | import java.util.List; 8 | 9 | import com.dao.BaseDao; 10 | import com.dao.ItemDao; 11 | import com.entity.Item; 12 | 13 | public class ItemDaoImpl extends BaseDao implements ItemDao { 14 | 15 | @Override 16 | public int insert(Item item) { 17 | String table = "items"; 18 | List list = new ArrayList(); 19 | list.add(item.getIid()); 20 | list.add(item.getOid()); 21 | list.add(item.getBid()); 22 | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 23 | Timestamp ts = new Timestamp(new java.util.Date().getTime()); 24 | list.add(new Date(new java.util.Date().getTime())); 25 | //数据库为varchar类型 26 | // list.add(sdf.format(ts.getTime())); 27 | list.add(item.getCount()); 28 | list.add(item.getPrice()); 29 | list.add(item.getState()); 30 | list.add(item.getTotal_price()); 31 | 32 | return insert(table, list); 33 | } 34 | 35 | @Override 36 | public int count() { 37 | // TODO Auto-generated method stub 38 | return 0; 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /books/src/com/dao/impl/OrderDaoImpl.java: -------------------------------------------------------------------------------- 1 | package com.dao.impl; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import com.dao.BaseDao; 7 | import com.dao.OrderDao; 8 | import com.entity.Order; 9 | 10 | public class OrderDaoImpl extends BaseDao implements OrderDao { 11 | 12 | @Override 13 | public List query(String sql) { 14 | String[] colums = {"oid","username","bid","createdate","count","price", 15 | "state","b_price","image","bookname","total_price"}; 16 | 17 | return query(sql, colums); 18 | } 19 | 20 | @Override 21 | public int insert(Order order) { 22 | String table = "orders"; 23 | List list = new ArrayList(); 24 | list.add(order.getOid()); 25 | list.add(order.getUsername()); 26 | 27 | return insert(table, list); 28 | } 29 | 30 | @Override 31 | public int count() { 32 | String sql = "select count(*) from items"; 33 | openConnection(); 34 | int i = 0; 35 | try { 36 | ps = conn.prepareStatement(sql); 37 | rs = ps.executeQuery(); 38 | while(rs.next()) { 39 | i = rs.getInt(1); 40 | } 41 | 42 | } catch (Exception e) { 43 | e.printStackTrace(); 44 | } finally { 45 | closeConnection(); 46 | } 47 | 48 | return i; 49 | } 50 | 51 | @Override 52 | public List queryByusername(String sql) { 53 | String[] colums = {"oid", "username"}; 54 | 55 | return query(sql, colums); 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /books/src/com/dao/impl/UserDaoImpl.java: -------------------------------------------------------------------------------- 1 | package com.dao.impl; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import com.dao.BaseDao; 7 | import com.dao.UserDao; 8 | import com.entity.UserInfo; 9 | 10 | /** 11 | * 实现类 12 | * @author Administrator 13 | * 14 | */ 15 | public class UserDaoImpl extends BaseDao implements UserDao{ 16 | 17 | @Override 18 | public List query(String sql) { 19 | String[] colums = {"username","password","email"}; 20 | 21 | return query(sql, colums); 22 | } 23 | 24 | @Override 25 | public int insert(UserInfo userinfo) { 26 | String table = "userinfo"; 27 | //List 有序有重复 28 | List list = new ArrayList(); 29 | list.add(userinfo.getUsername()); 30 | list.add(userinfo.getPassword()); 31 | list.add(userinfo.getEmail()); 32 | 33 | return insert(table, list); 34 | } 35 | 36 | 37 | 38 | 39 | } 40 | -------------------------------------------------------------------------------- /books/src/com/entity/Book.java: -------------------------------------------------------------------------------- 1 | package com.entity; 2 | 3 | import java.io.Serializable; 4 | 5 | public class Book implements Serializable { 6 | private final String bid_seq = "BS_BOOKID.nextVal"; //数据库,做自增长 7 | private int bid; 8 | private String bookname; 9 | private double price; 10 | private String image; 11 | private String stock;//库存 12 | private int count = 0; 13 | 14 | 15 | 16 | 17 | public int getBid() { 18 | return bid; 19 | } 20 | public void setBid(int bid) { 21 | this.bid = bid; 22 | } 23 | public String getBookname() { 24 | return bookname; 25 | } 26 | public void setBookname(String bookname) { 27 | this.bookname = bookname; 28 | } 29 | public double getPrice() { 30 | return price; 31 | } 32 | public void setPrice(double price) { 33 | this.price = price; 34 | } 35 | 36 | 37 | public String getImage() { 38 | return image; 39 | } 40 | public void setImage(String image) { 41 | this.image = image; 42 | } 43 | public String getStock() { 44 | return stock; 45 | } 46 | public void setStock(String stock) { 47 | this.stock = stock; 48 | } 49 | public int getCount() { 50 | return count; 51 | } 52 | public void setCount(int count) { 53 | this.count = count; 54 | } 55 | public String getBid_seq() { 56 | return bid_seq; 57 | } 58 | 59 | } 60 | -------------------------------------------------------------------------------- /books/src/com/entity/Item.java: -------------------------------------------------------------------------------- 1 | package com.entity; 2 | 3 | import java.io.Serializable; 4 | import java.sql.Date; 5 | 6 | import com.dao.BaseDao; 7 | 8 | public class Item extends BaseDao implements Serializable { 9 | private int iid = 1; 10 | private int oid; 11 | private int bid; 12 | private Date createDate; 13 | private double price; 14 | private double total_price; 15 | private int count; 16 | public double getTotal_price() { 17 | return total_price; 18 | } 19 | 20 | public void setTotal_price(double total_price) { 21 | this.total_price = total_price; 22 | } 23 | 24 | public int getCount() { 25 | return count; 26 | } 27 | 28 | public void setCount(int count) { 29 | this.count = count; 30 | } 31 | private int state;//订单状态 32 | 33 | /** 34 | * 由于表没有设置自动增长 35 | * 获取order表中最大id值 36 | * @return 37 | */ 38 | public int maxid() { 39 | String sql = "select * from items order by iid desc limit 0,1"; 40 | openConnection(); 41 | int i = 0; 42 | try { 43 | ps = conn.prepareStatement(sql); 44 | rs = ps.executeQuery(); 45 | if(rs.next()) { 46 | i = rs.getInt(1) + 1; 47 | } else { 48 | i = 1; 49 | } 50 | } catch (Exception e) { 51 | e.printStackTrace(); 52 | } finally { 53 | closeConnection(); 54 | } 55 | 56 | return i; 57 | } 58 | 59 | public int getIid() { 60 | iid = maxid(); 61 | return iid; 62 | } 63 | 64 | public void setIid(int iid) { 65 | this.iid = iid; 66 | } 67 | public int getOid() { 68 | return oid; 69 | } 70 | public void setOid(int oid) { 71 | this.oid = oid; 72 | } 73 | public int getBid() { 74 | return bid; 75 | } 76 | public void setBid(int bid) { 77 | this.bid = bid; 78 | } 79 | public Date getCreateDate() { 80 | return createDate; 81 | } 82 | public void setCreateDate(Date createDate) { 83 | this.createDate = createDate; 84 | } 85 | public double getPrice() { 86 | return price; 87 | } 88 | public void setPrice(double price) { 89 | this.price = price; 90 | } 91 | public int getState() { 92 | return state; 93 | } 94 | public void setState(int state) { 95 | this.state = state; 96 | } 97 | 98 | 99 | 100 | } 101 | -------------------------------------------------------------------------------- /books/src/com/entity/Order.java: -------------------------------------------------------------------------------- 1 | package com.entity; 2 | 3 | import java.io.Serializable; 4 | 5 | import com.dao.BaseDao; 6 | 7 | public class Order extends BaseDao implements Serializable { 8 | private int oid = 1; 9 | private String username; 10 | 11 | 12 | public int getOid() { 13 | oid = maxid(); //获取最大id值 14 | return oid; 15 | } 16 | public void setOid(int oid) { 17 | this.oid = oid; 18 | } 19 | public String getUsername() { 20 | return username; 21 | } 22 | public void setUsername(String username) { 23 | this.username = username; 24 | } 25 | 26 | /** 27 | * 由于表没有设置自动增长 28 | * 获取order表中最大id值 29 | * @return 30 | */ 31 | public int maxid() { 32 | String sql = "select * from orders order by oid desc limit 0,1"; 33 | openConnection(); 34 | int i = 0; 35 | try { 36 | ps = conn.prepareStatement(sql); 37 | rs = ps.executeQuery(); 38 | if(rs.next()) { 39 | i = rs.getInt(1) + 1; 40 | } else { 41 | i = 1; 42 | } 43 | } catch (Exception e) { 44 | e.printStackTrace(); 45 | } finally { 46 | closeConnection(); 47 | } 48 | 49 | return i; 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /books/src/com/entity/UserInfo.java: -------------------------------------------------------------------------------- 1 | package com.entity; 2 | 3 | public class UserInfo { 4 | 5 | private String username; 6 | private String password; 7 | private String email; 8 | 9 | public String getUsername() { 10 | return username; 11 | } 12 | public void setUsername(String username) { 13 | this.username = username; 14 | } 15 | public String getPassword() { 16 | return password; 17 | } 18 | public void setPassword(String password) { 19 | this.password = password; 20 | } 21 | public String getEmail() { 22 | return email; 23 | } 24 | public void setEmail(String email) { 25 | this.email = email; 26 | } 27 | 28 | 29 | } 30 | -------------------------------------------------------------------------------- /books/src/com/servlet/AddOrderServlet.java: -------------------------------------------------------------------------------- 1 | package com.servlet; 2 | 3 | import java.io.IOException; 4 | 5 | import javax.servlet.ServletConfig; 6 | import javax.servlet.ServletException; 7 | import javax.servlet.http.HttpServlet; 8 | import javax.servlet.http.HttpServletRequest; 9 | import javax.servlet.http.HttpServletResponse; 10 | 11 | import com.biz.ItemBiz; 12 | import com.biz.OrderBiz; 13 | import com.biz.impl.ItemBizImpl; 14 | import com.biz.impl.OrderBizImpl; 15 | import com.dao.ItemDao; 16 | import com.dao.OrderDao; 17 | import com.dao.impl.ItemDaoImpl; 18 | import com.dao.impl.OrderDaoImpl; 19 | import com.entity.Item; 20 | import com.entity.Order; 21 | 22 | 23 | public class AddOrderServlet extends HttpServlet { 24 | private static final long serialVersionUID = 1L; 25 | private ItemBiz itembiz; 26 | private ItemDao itemdao; 27 | private OrderBiz orderbiz; 28 | private OrderDao orderdao; 29 | 30 | 31 | 32 | public void init(ServletConfig config) throws ServletException { 33 | 34 | itembiz = new ItemBizImpl(); 35 | itemdao = new ItemDaoImpl(); 36 | itembiz.setItemdao(itemdao); 37 | 38 | orderbiz = new OrderBizImpl(); 39 | orderdao = new OrderDaoImpl(); 40 | orderbiz.setOrderdao(orderdao); 41 | 42 | } 43 | 44 | protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 45 | doPost(request, response); 46 | } 47 | 48 | 49 | protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 50 | //1.获取用户名 51 | String username = request.getSession().getAttribute("loginuser").toString(); 52 | 53 | Order order = new Order(); 54 | order.setUsername(username); 55 | //2.生成订单 56 | if(!orderbiz.newOrder(order)) { 57 | System.out.println("添加失败"); 58 | return; 59 | } 60 | //3.生成订单详细信息 61 | int count_str = Integer.parseInt(request.getParameter("count")); 62 | String oid_str = orderbiz.getCurrentOid(username); 63 | for(int i = 1; i< count_str; i++) { 64 | String bid = request.getParameter("hidden_bid_"+ i);//获取bid 65 | String book_count = request.getParameter("nums_"+ i);//获取图书数量 66 | String book_price = request.getParameter("hidden_"+ i); 67 | String totalPrice = request.getParameter("hidden_book_total_price_"+i);//获取单个图书总价格 68 | //String hidden_total_price = request.getParameter("hidden_total_price");//获取提交订单总价格 69 | 70 | Item item = new Item(); 71 | item.setOid(Integer.parseInt(oid_str)); 72 | item.setBid(Integer.parseInt(bid)); 73 | item.setCount(Integer.parseInt(book_count)); 74 | item.setPrice(Double.parseDouble(book_price)); 75 | item.setState(0); 76 | item.setTotal_price(Double.parseDouble(totalPrice)); 77 | 78 | itembiz.addItemToOrder(item); 79 | } 80 | //提交订单后清空购物信息 81 | request.getSession().removeAttribute("bookcart"); 82 | request.getSession().removeAttribute("bookcart_count"); 83 | response.sendRedirect("ShowOrderServlet");//我的订单 84 | 85 | } 86 | 87 | } 88 | -------------------------------------------------------------------------------- /books/src/com/servlet/CartServlet.java: -------------------------------------------------------------------------------- 1 | package com.servlet; 2 | 3 | import java.io.IOException; 4 | import java.util.ArrayList; 5 | import java.util.List; 6 | 7 | import javax.servlet.Servlet; 8 | import javax.servlet.ServletConfig; 9 | import javax.servlet.ServletException; 10 | import javax.servlet.http.HttpServlet; 11 | import javax.servlet.http.HttpServletRequest; 12 | import javax.servlet.http.HttpServletResponse; 13 | 14 | import com.biz.BookBiz; 15 | import com.biz.impl.BookBizImpl; 16 | import com.dao.BaseDao; 17 | import com.dao.BookDao; 18 | import com.dao.impl.BookDaoImpl; 19 | import com.entity.Book; 20 | 21 | /** 22 | * Servlet implementation class CartServlet 23 | */ 24 | public class CartServlet extends HttpServlet { 25 | private static final long serialVersionUID = 1L; 26 | private BookBiz bookbiz = null; 27 | private BookDao bookdao = null; 28 | 29 | private BaseDao basedao = null; 30 | 31 | 32 | /** 33 | * @see Servlet#init(ServletConfig) 34 | */ 35 | public void init(ServletConfig config) throws ServletException { 36 | bookdao = new BookDaoImpl(); 37 | bookbiz = new BookBizImpl(); 38 | bookbiz.setBookdao(bookdao); 39 | 40 | basedao = new BaseDao(); 41 | } 42 | 43 | 44 | protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 45 | String[] bids = request.getParameterValues("bookId");//获取页面所有名字为bookID的input值 46 | String[] title = request.getParameterValues("title"); 47 | String[] price = request.getParameterValues("price"); 48 | String[] stock = request.getParameterValues("stock"); 49 | String[] image = request.getParameterValues("image"); 50 | 51 | int oneStock = 0; 52 | boolean isOrNotChangeStock = true;//是否修改库存,保证库存不被修改为负数 53 | 54 | /*double allPrice;//总价 55 | double oneAllPrice;//书单价*购买数量 56 | */ List bookcart; //获取购物车中的内容,没有则初始化一个列表 57 | bookcart = (List) request.getSession().getAttribute("bookcart");//虚拟一个空表 58 | if(bookcart == null) { 59 | bookcart = new ArrayList(); 60 | } 61 | 62 | for(int i = 0; i< stock.length; i++) { 63 | System.out.println("stock"+stock[i]); 64 | } 65 | 66 | //获取复选框选中的值 67 | for (int i = 0; i < bids.length; i++) { 68 | boolean isNotExists = true;//定义购物车中的值 69 | Book book = new Book(); 70 | int bid = Integer.parseInt(bids[i]); 71 | 72 | try { 73 | oneStock = basedao.queryStock(bid); 74 | } catch (Exception e) { 75 | e.printStackTrace(); 76 | } 77 | 78 | if(oneStock > 0) {//如果库存大于0 79 | //查看当前图书是否已经存在于购物车中 80 | //查看购物车列表是否已经存在该图书,对比两个图书id 81 | for (int j = 0; j < bookcart.size(); j++) { 82 | Book existBook = (Book) bookcart.get(j); 83 | if(existBook.getBid() == bid) {//如果购物车中的id==被选中的图书id,则购物车中的数量+1 84 | bookcart.remove(j); 85 | existBook.setCount(existBook.getCount()+1);//购物车中商品数量+1 86 | double totalPrice = existBook.getPrice(); 87 | bookcart.add(existBook);//商品总价 88 | System.out.println("总价"+totalPrice); 89 | isNotExists = false; 90 | 91 | //修改库存 92 | isOrNotChangeStock = bookbiz.changeStock(bid, "-1"); 93 | break; 94 | } 95 | } 96 | 97 | if(!isNotExists) { 98 | continue; 99 | } 100 | book.setBid(bid); 101 | 102 | //判断当前获取的图书信息是否为指定bid下的信息 103 | for (int j = 0; j < title.length; j++) { 104 | // request.setCharacterEncoding("utf-8"); 105 | //解决页面图书标题中文显示乱码 106 | String title_temp =new String(title[j].getBytes("ISO-8859-1"),"utf-8");//取标题 107 | 108 | 109 | if(title_temp.indexOf(bids[i]+":")<0) {//indexOf匹配bids是否包含在title_temp中,如果没有返回-1 110 | continue; 111 | } 112 | if(image[j].indexOf(bids[i]+":")<0) {//indexOf匹配bids是否包含在title_temp中,如果没有返回-1 113 | continue; 114 | } 115 | if(price[j].indexOf(bids[i]+":")<0) {//indexOf匹配bids是否包含在title_temp中,如果没有返回-1 116 | continue; 117 | } 118 | if(stock[j].indexOf(bids[i]+":")<0) {//indexOf匹配bids是否包含在title_temp中,如果没有返回-1 119 | continue; 120 | } 121 | //添加指定bid下的图书信息 122 | book.setBookname(filter(title_temp, bids[i])); 123 | book.setImage(filter(image[j], bids[i])); 124 | book.setPrice(Double.parseDouble(filter(price[j], bids[i]))); 125 | // book.setPrice(Double.valueOf(filter(price[j], bids[i]))); 126 | book.setStock(filter(stock[j], bids[i])); 127 | book.setCount(1); 128 | 129 | //修改库存 130 | bookbiz.changeStock(bid, "-1"); 131 | bookcart.add(book); 132 | } 133 | 134 | } 135 | 136 | 137 | } 138 | request.getSession().setAttribute("bookcart", bookcart);//将购物车信息存入session 139 | request.getSession().setAttribute("bookcart_count", bookcart.size());//购物车的数量 140 | response.sendRedirect("shopping_success.jsp"); 141 | 142 | } 143 | 144 | /** 145 | * 截取图书信息 146 | * @param s:从哪里开始截取 147 | * @param t:信息 148 | * @return 149 | */ 150 | public String filter(String s, String t) { 151 | return s.substring(t.length()+1, s.length()); 152 | } 153 | 154 | 155 | protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 156 | doGet(request, response); 157 | } 158 | 159 | } 160 | -------------------------------------------------------------------------------- /books/src/com/servlet/LoginServlet.java: -------------------------------------------------------------------------------- 1 | package com.servlet; 2 | 3 | import java.io.IOException; 4 | import java.io.PrintWriter; 5 | 6 | import javax.servlet.ServletConfig; 7 | import javax.servlet.ServletException; 8 | import javax.servlet.annotation.WebServlet; 9 | import javax.servlet.http.HttpServlet; 10 | import javax.servlet.http.HttpServletRequest; 11 | import javax.servlet.http.HttpServletResponse; 12 | 13 | import com.biz.UserBiz; 14 | import com.biz.impl.UserBizImpl; 15 | import com.dao.UserDao; 16 | import com.dao.impl.UserDaoImpl; 17 | 18 | public class LoginServlet extends HttpServlet { 19 | private static final long serialVersionUID = 1L; 20 | private UserDao userDao = null; 21 | private UserBiz userBiz = null; 22 | 23 | /** 24 | * @see Servlet#init(ServletConfig) 25 | */ 26 | public void init() throws ServletException { 27 | userDao = new UserDaoImpl(); 28 | userBiz = new UserBizImpl(); 29 | userBiz.setUserDao(userDao); 30 | } 31 | 32 | /** 33 | * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 34 | */ 35 | protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 36 | String username = request.getParameter("username");//获取页面传过来的参数 37 | String password = request.getParameter("password"); 38 | // System.out.print("fdsafdsfsa"); 39 | //登录操作 40 | boolean flag = userBiz.checkLogin(username, password); 41 | 42 | if(flag) { 43 | request.getSession().setAttribute("loginuser", username); 44 | // response.sendRedirect("main.jsp"); 45 | response.sendRedirect("SearchServlet"); //首页直接显示 46 | } else {//登录失败 47 | response.setContentType("text/html;charset=utf-8"); 48 | // response.setCharacterEncoding("utf-8"); 49 | PrintWriter out = response.getWriter(); 50 | out.println(""); 54 | out.close(); 55 | } 56 | } 57 | 58 | /** 59 | * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 60 | */ 61 | protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 62 | // request.setAttribute("list", "list"); 63 | doGet(request, response); 64 | 65 | } 66 | 67 | } 68 | -------------------------------------------------------------------------------- /books/src/com/servlet/ModifyCartServlet.java: -------------------------------------------------------------------------------- 1 | package com.servlet; 2 | 3 | import java.io.IOException; 4 | import java.sql.SQLException; 5 | import java.util.List; 6 | 7 | import javax.servlet.ServletException; 8 | import javax.servlet.http.HttpServlet; 9 | import javax.servlet.http.HttpServletRequest; 10 | import javax.servlet.http.HttpServletResponse; 11 | 12 | import com.biz.BookBiz; 13 | import com.biz.impl.BookBizImpl; 14 | import com.dao.BaseDao; 15 | import com.dao.BookDao; 16 | import com.dao.impl.BookDaoImpl; 17 | import com.entity.Book; 18 | 19 | public class ModifyCartServlet extends HttpServlet { 20 | 21 | private static final long serialVersionUID = 1L; 22 | private BookBiz bookbiz = null; 23 | private BookDao bookdao = null; 24 | private BaseDao basedao = null; 25 | 26 | @Override 27 | public void init() throws ServletException { 28 | bookdao = new BookDaoImpl(); 29 | bookbiz = new BookBizImpl(); 30 | bookbiz.setBookdao(bookdao); 31 | 32 | basedao = new BaseDao(); 33 | 34 | } 35 | 36 | protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 37 | String action = request.getParameter("action"); 38 | if(action.equals("update")) { 39 | updateCart(request, response); 40 | } 41 | else if(action.equals("remove")) { 42 | remove(request, response); 43 | } 44 | 45 | } 46 | 47 | 48 | protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 49 | doGet(request, response); 50 | } 51 | 52 | 53 | protected void updateCart(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 54 | //1.获取图书id和修改后的数量 55 | String bid = request.getParameter("bid"); 56 | String count = request.getParameter("count"); 57 | //2.获取购物车 58 | List bookcart = (List) request.getSession().getAttribute("bookcart"); 59 | //3.查找购物车中修改过的图书,并修改相应的信息 60 | for (Book book : bookcart) { 61 | if(Integer.valueOf(bid) == book.getBid()) { 62 | //4.获取修改前的图书数量 63 | int old_count = book.getCount(); 64 | //5.设置当前图书的新数量 65 | book.setCount(Integer.valueOf(count)); 66 | System.out.println("当前图书新数量"+count); 67 | 68 | int nowStock = 0; 69 | try { 70 | nowStock = basedao.queryStock(Integer.parseInt(bid)); 71 | } catch (Exception e) { 72 | e.printStackTrace(); 73 | } 74 | 75 | //6.修改库存 76 | bookbiz.changeStock(Integer.parseInt(bid), (old_count-Integer.parseInt(count))+""); 77 | break; 78 | } 79 | } 80 | } 81 | 82 | protected void remove(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 83 | //1.获取图书 84 | String bid = request.getParameter("bid"); 85 | //2.获取购物车 86 | List bookcart = (List) request.getSession().getAttribute("bookcart"); 87 | //3.循环查找购物车中相同的图书id 88 | for(int i = 0; i< bookcart.size(); i++) { 89 | Book book = bookcart.get(i); 90 | if(Integer.valueOf(bid) == book.getBid()) { 91 | //4.移除图书 92 | bookcart.remove(i); 93 | //5.修改库存 94 | bookbiz.changeStock(Integer.parseInt(bid), book.getCount()+""); 95 | break; 96 | } 97 | } 98 | 99 | //6.将购物车保存到session中 100 | // request.getSession().setAttribute("bookcart", bookcart); 101 | request.getSession().setAttribute("bookcart_count", bookcart.size()); 102 | System.out.println(request.getSession().getAttribute("bookcart_count")); 103 | } 104 | 105 | } 106 | -------------------------------------------------------------------------------- /books/src/com/servlet/RegisterServlet.java: -------------------------------------------------------------------------------- 1 | package com.servlet; 2 | 3 | import java.io.IOException; 4 | import java.io.PrintWriter; 5 | 6 | import javax.servlet.ServletException; 7 | import javax.servlet.http.HttpServlet; 8 | import javax.servlet.http.HttpServletRequest; 9 | import javax.servlet.http.HttpServletResponse; 10 | 11 | import com.biz.UserBiz; 12 | import com.biz.impl.UserBizImpl; 13 | import com.dao.UserDao; 14 | import com.dao.impl.UserDaoImpl; 15 | import com.entity.UserInfo; 16 | 17 | /** 18 | * Servlet implementation class RegisterServlet 19 | */ 20 | 21 | public class RegisterServlet extends HttpServlet { 22 | private static final long serialVersionUID = 1L; 23 | private UserDao userDao = null; 24 | private UserBiz userBiz = null; 25 | 26 | @Override 27 | public void init() throws ServletException { 28 | // TODO Auto-generated method stub 29 | userDao = new UserDaoImpl(); 30 | userBiz = new UserBizImpl(); 31 | userBiz.setUserDao(userDao); 32 | } 33 | 34 | /** 35 | * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 36 | */ 37 | protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 38 | String action = request.getParameter("action"); //根据查看当前执行的动作时什么,(action有值,则检查用户名是否存在,null则注册) 39 | if(action == null) { 40 | register(request, response); 41 | } else { 42 | check(request, response); 43 | } 44 | 45 | // register(request, response); 46 | 47 | } 48 | 49 | /** 50 | * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 51 | */ 52 | protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 53 | doGet(request, response); 54 | } 55 | 56 | 57 | protected void register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 58 | request.setCharacterEncoding("utf-8"); //防止中文注册名乱码 59 | String username = request.getParameter("username"); 60 | String password = request.getParameter("password"); 61 | String email = request.getParameter("email"); 62 | 63 | UserInfo userinfo = new UserInfo(); 64 | userinfo.setUsername(username); 65 | userinfo.setPassword(password); 66 | userinfo.setEmail(email); 67 | 68 | if(!userBiz.addUser(userinfo)) { 69 | response.setContentType("text/html; charset=utf-8"); 70 | PrintWriter out = response.getWriter(); 71 | out.println(""); 75 | out.close(); 76 | } 77 | response.sendRedirect("register_success.jsp");//注册成功 78 | 79 | } 80 | 81 | protected void check(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 82 | String username = request.getParameter("username"); 83 | boolean isExist = userBiz.userExist(username); 84 | if(isExist) { 85 | response.setCharacterEncoding("utf-8"); 86 | response.getWriter().write("true"); 87 | System.out.println("true"); 88 | response.getWriter().close(); 89 | } else { 90 | response.setCharacterEncoding("utf-8"); 91 | response.getWriter().write("false"); 92 | System.out.println("false"); 93 | response.getWriter().close(); 94 | } 95 | } 96 | 97 | } 98 | -------------------------------------------------------------------------------- /books/src/com/servlet/SearchServlet.java: -------------------------------------------------------------------------------- 1 | package com.servlet; 2 | 3 | import java.io.IOException; 4 | import java.util.List; 5 | 6 | import javax.servlet.Servlet; 7 | import javax.servlet.ServletConfig; 8 | import javax.servlet.ServletException; 9 | import javax.servlet.annotation.WebServlet; 10 | import javax.servlet.http.HttpServlet; 11 | import javax.servlet.http.HttpServletRequest; 12 | import javax.servlet.http.HttpServletResponse; 13 | import javax.websocket.Session; 14 | 15 | import com.biz.BookBiz; 16 | import com.biz.impl.BookBizImpl; 17 | import com.dao.BookDao; 18 | import com.dao.impl.BookDaoImpl; 19 | import com.entity.Book; 20 | 21 | /** 22 | * Servlet implementation class SearchServlet 23 | */ 24 | 25 | public class SearchServlet extends HttpServlet { 26 | private static final long serialVersionUID = 1L; 27 | private BookBiz bookbiz = null; 28 | private BookDao bookdao = null; 29 | 30 | 31 | /** 32 | * @see Servlet#init(ServletConfig) 33 | */ 34 | public void init(ServletConfig config) throws ServletException { 35 | bookdao = new BookDaoImpl(); 36 | bookbiz = new BookBizImpl(); 37 | bookbiz.setBookdao(bookdao); 38 | } 39 | 40 | /** 41 | * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 42 | */ 43 | protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 44 | doPost(request, response); 45 | } 46 | 47 | /** 48 | * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 49 | */ 50 | protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 51 | request.setCharacterEncoding("utf-8"); 52 | int countByName = 0; 53 | String currentPage = request.getParameter("currentPage");//获取当前页 54 | int no = currentPage == null?1:Integer.parseInt(currentPage);//如果当前页为空,则默认为1,否则转化为相应的int 55 | 56 | String bookname = request.getParameter("keywords");//获取你输入的书名 57 | String keywords = bookname; 58 | List books = null; 59 | if(bookname==null || bookname.equals("")|| bookname.equals("null")){ 60 | books=bookbiz.findAll(3, no);//查询所有。3:代表每页显示的条数,no:当前显示页面 61 | request.setAttribute("totalPage", (bookbiz.count()/3 + 1));//将总页面数存入request 62 | 63 | } else { 64 | countByName = bookbiz.findBookByName(bookname);//记录模糊查询的总记录数 65 | books = bookbiz.findBookByName(bookname,3, no);//根据书名模糊查询出数据 66 | //System.out.println("bookname不为0:"+books.size()); 67 | request.setAttribute("totalPage", (books.size()/3 + 1));//将总页面数存入request 68 | } 69 | 70 | request.setAttribute("keywords", keywords); 71 | System.out.println(keywords); 72 | request.setAttribute("books", books);//将查询出的数据存入request 73 | request.setAttribute("current", no);//将当前页存入request 74 | request.getRequestDispatcher("main.jsp").forward(request, response);//页面转发 75 | 76 | } 77 | 78 | } 79 | -------------------------------------------------------------------------------- /books/src/com/servlet/ShowOrderServlet.java: -------------------------------------------------------------------------------- 1 | package com.servlet; 2 | 3 | import java.io.IOException; 4 | import java.util.List; 5 | 6 | import javax.servlet.ServletConfig; 7 | import javax.servlet.ServletException; 8 | import javax.servlet.http.HttpServlet; 9 | import javax.servlet.http.HttpServletRequest; 10 | import javax.servlet.http.HttpServletResponse; 11 | 12 | import com.biz.OrderBiz; 13 | import com.biz.impl.OrderBizImpl; 14 | import com.dao.OrderDao; 15 | import com.dao.impl.OrderDaoImpl; 16 | 17 | 18 | public class ShowOrderServlet extends HttpServlet { 19 | private static final long serialVersionUID = 1L; 20 | private OrderBiz orderbiz; 21 | private OrderDao orderdao; 22 | 23 | 24 | public void init(ServletConfig config) throws ServletException { 25 | 26 | orderbiz=new OrderBizImpl(); 27 | orderdao=new OrderDaoImpl(); 28 | orderbiz.setOrderdao(orderdao); 29 | } 30 | 31 | 32 | protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 33 | doPost(request, response); 34 | 35 | } 36 | 37 | protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 38 | 39 | String username=request.getSession().getAttribute("loginuser").toString(); 40 | List orders=orderbiz.findByUsername(username, 3, 0); 41 | request.setAttribute("orders", orders); 42 | request.getRequestDispatcher("orderlist.jsp").forward(request, response); 43 | 44 | } 45 | 46 | } 47 | --------------------------------------------------------------------------------