隨着這個學期的javaweb課程結束,接着就是每學期的重頭戲--課程設計。今年老師給的課設題目是在線商城,給了一個黑馬程序員的書店模板。本着想練練後臺的能力,本身動手寫了一個簡單的購物商城。實現了基本的登陸、註冊、購買、提交訂單等基本功能。css
GoodsServlethtml
1 package servlet; 2 3 4 import java.io.IOException; 5 import java.sql.SQLException; 6 import java.util.List; 7 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 14 import domain.Goods; 15 import service.GoodsService; 16 17 /** 18 * Servlet implementation class GoodsServlet 19 */ 20 @WebServlet("/GoodsServlet") 21 public class GoodsServlet extends HttpServlet { 22 private static final long serialVersionUID = 1L; 23 24 25 public GoodsServlet() { 26 super(); 27 // TODO Auto-generated constructor stub 28 } 29 30 31 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 32 GoodsService goodsService=new GoodsService(); 33 try { 34 List<Goods> list1=goodsService.findAllRecommand(); 35 List<Goods> list2=goodsService.findAllRest(); 36 List<Goods> list3=goodsService.findAllMilk(); 37 List<Goods> list4=goodsService.findAllFirst(); 38 List<Goods> list5=goodsService.findAllDrink(); 39 request.setAttribute("list1", list1); 40 request.setAttribute("list2", list2); 41 request.setAttribute("list3", list3); 42 request.setAttribute("list4", list4); 43 request.setAttribute("list5", list5); 44 request.getRequestDispatcher("/index.jsp").forward(request, response); 45 } catch (SQLException e) { 46 // TODO Auto-generated catch block 47 e.printStackTrace(); 48 } 49 } 50 51 52 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 53 54 doGet(request, response); 55 } 56 57 }
LoginServletjava
1 package servlet; 2 3 import java.io.IOException; 4 import java.sql.SQLException; 5 import java.util.ArrayList; 6 import java.util.List; 7 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 14 import domain.User; 15 import service.UserService; 16 17 /** 18 * Servlet implementation class LoginServlet 19 */ 20 @WebServlet("/LoginServlet") 21 public class LoginServlet extends HttpServlet { 22 private static final long serialVersionUID = 1L; 23 24 25 public LoginServlet() { 26 super(); 27 } 28 29 30 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 31 UserService userService=new UserService(); 32 try { 33 List<User> list= userService.login(); 34 String name=request.getParameter("username"); 35 String password=request.getParameter("password"); 36 boolean flag=false; 37 38 for(User user:list){ 39 if(name.equals(user.getName())&&password.equals(user.getPassword())){ 40 List<User> user1=(List<User>) request.getSession().getAttribute("user1"); 41 if(user1==null){ 42 user1=new ArrayList<User>(); 43 request.getSession().setAttribute("user1",user1); 44 user1.add(user); 45 flag=true; 46 break; 47 } 48 } 49 } 50 if(flag){ 51 request.getRequestDispatcher("GoodsServlet").forward(request, response); 52 } 53 else{ 54 request.getRequestDispatcher("/fail.jsp").forward(request, response); 55 } 56 } catch (SQLException e) { 57 e.printStackTrace(); 58 } 59 60 } 61 62 /** 63 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 64 */ 65 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 66 // TODO Auto-generated method stub 67 doGet(request, response); 68 } 69 70 }
RegisterServletjquery
1 package servlet; 2 3 import java.io.IOException; 4 import java.sql.SQLException; 5 6 import javax.servlet.ServletException; 7 import javax.servlet.annotation.WebServlet; 8 import javax.servlet.http.HttpServlet; 9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletResponse; 11 12 import domain.User; 13 import service.UserService; 14 15 /** 16 * Servlet implementation class RegisterServlet 17 */ 18 @WebServlet("/RegisterServlet") 19 public class RegisterServlet extends HttpServlet { 20 private static final long serialVersionUID = 1L; 21 22 /** 23 * @see HttpServlet#HttpServlet() 24 */ 25 public RegisterServlet() { 26 super(); 27 // TODO Auto-generated constructor stub 28 } 29 30 /** 31 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 32 */ 33 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 34 35 UserService userService=new UserService(); 36 response.getWriter().println(request.getParameter("telephone")); 37 User user=new User((request.getParameter("gender")),request.getParameter("username"),request.getParameter("password"),request.getParameter("email"), 38 request.getParameter("telephone"),request.getParameter("address")); 39 try { 40 userService.insert(user); 41 request.getRequestDispatcher("/login.jsp").forward(request, response); 42 } catch (SQLException e) { 43 // TODO Auto-generated catch block 44 e.printStackTrace(); 45 } 46 } 47 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 48 // TODO Auto-generated method stub 49 doGet(request, response); 50 } 51 52 }
index.jsp程序員
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 3 <html> 4 <head> 5 <title>淘一淘</title> 6 <%-- 導入css --%> 7 <link 8 href="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/css/bootstrap.min.css" 9 rel="stylesheet"> 10 <script 11 src="http://cdn.static.runoob.com/libs/jquery/2.1.1/jquery.min.js"> 12 13 </script> 14 <script 15 src="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/js/bootstrap.min.js"> 16 </script> 17 <style> 18 body { 19 /*navbar-fixed-top 固定導航欄時也會覆蓋body,解決此問題*/ 20 padding-top: 50px; 21 padding-bottom: 50px; 22 background-image:url("image/background1.jpg"); 23 } 24 </style> 25 </head> 26 <body> 27 <%@include file="head.jsp"%> 28 <%@include file="auto.jsp"%> 29 <hr class="divider"> 30 <!--推薦區域 --> 31 <div class="container"> 32 <img src="images/recommand.jpg" width="100%"> 33 <hr class="divider"> 34 <c:forEach items="${requestScope.list1}" var="goods"> 35 <div class="col-md-3" align="center"> 36 <img class="img-rounded" src="${goods.url}" alt="error" 37 width="100%"> 38 <p> 39 <a class="btn-lg btn-danger" href="DetailsServlet?id=${goods.id}" role="button">查看詳情</a> 40 </p> 41 </div> 42 </c:forEach> 43 </div> 44 <hr class="divider"> 45 <div class="container"> 46 <img src="images/remai.jpg" width="100%"> 47 <!-- 休閒食品區 --> 48 <hr class="divider"> 49 <div class="tab-content"> 50 <div role="tabpanel" class="tab-pane active" id="tab-rest"> 51 <div class="col-md-4" > 52 <img class="img-rounded " src="image/rest.jpg" 53 width="100%" height="100%"> 54 </div> 55 <c:forEach items="${requestScope.list2}" var="goods"> 56 <div class="col-md-4" > 57 <a href="DetailsServlet?id=${goods.id}"><img class="img-rounded " src="${goods.url}" 58 width="100%" height="50%"></a> 59 </div> 60 </c:forEach> 61 </div> 62 </div> 63 </div> 64 65 <!-- 奶製品區 --> 66 <hr class="divider"> 67 <div class="container"> 68 <div class="tab-content"> 69 <div role="tabpanel" class="tab-pane active" id="tab-milk"> 70 <div class="col-md-4" > 71 <img class="img-rounded " src="image/milk.jpg" 72 width="100%" height="100%"> 73 </div> 74 <c:forEach items="${requestScope.list3}" var="goods"> 75 <div class="col-md-4" > 76 <a href="DetailsServlet?id=${goods.id}"><img class="img-rounded " src="${goods.url}" 77 width="100%" height="50%"> </a> 78 </div> 79 </c:forEach> 80 </div> 81 </div> 82 </div> 83 84 <!-- 速食品區 --> 85 <hr class="divider"> 86 <div class="container"> 87 <div class="tab-content"> 88 <div role="tabpanel" class="tab-pane active" id="tab-first"> 89 <div class="col-md-4" > 90 <img class="img-rounded " src="image/first.jpg" 91 width="100%" height="100%"> 92 </div> 93 <c:forEach items="${requestScope.list4}" var="goods"> 94 <div class="col-md-4" > 95 <a href="DetailsServlet?id=${goods.id}"><img class="img-rounded " src="${goods.url}" 96 width="100%" height="50%"></a> 97 </div> 98 </c:forEach> 99 </div> 100 </div> 101 </div> 102 <!-- 酒水飲料區 --> 103 <hr class="divider"> 104 <div class="container"> 105 <div class="tab-content"> 106 <div role="tabpanel" class="tab-pane active" id="tab-drink"> 107 <div class="col-md-4" > 108 <img class="img-rounded " src="image/drink.jpg" 109 width="100%" height="100%"> 110 </div> 111 <c:forEach items="${requestScope.list5}" var="goods"> 112 <div class="col-md-4" > 113 <a href="DetailsServlet?id=${goods.id}"><img class="img-rounded " src="${goods.url}" 114 width="100%" height="50%"></a> 115 </div> 116 </c:forEach> 117 </div> 118 </div> 119 </div> 120 <%@ include file="foot.jsp"%> 121 </body> 122 </html>
auto.jspweb
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <div id="myCarousel" class="carousel slide"> <!-- 輪播(Carousel)指標 --> <ol class="carousel-indicators"> <li data-target="#myCarousel" data-slide-to="0" class="active"></li> <li data-target="#myCarousel" data-slide-to="1"></li> <li data-target="#myCarousel" data-slide-to="2"></li> <li data-target="#myCarousel" data-slide-to="3"></li> </ol> <!-- 輪播(Carousel)項目 --> <div class="carousel-inner"> <div class="item active" align="center"> <img src="ad/big1.jpg" alt="First slide" width="100%"> </div> <div class="item" align="center"> <img src="ad/big2.jpg" alt="Second slide" width="100%"> </div> <div class="item" align="center"> <img src="ad/big3.jpg" alt="Third slide" width="100%"> </div> <div class="item" align="center"> <img src="ad/big4.jpg" alt="Third slide" width="100%"> </div> </div> <!-- 輪播(Carousel)導航 --> <a class="left carousel-control" href="#myCarousel" role="button" data-slide="prev"></a> <a class="right carousel-control" href="#myCarousel" role="button" data-slide="next"></a> </div> </div> </body> </html>
head.jspsql
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <nav class="navbar navbar-default navbar-fixed-top navbar-inverse"> <div class="container"> <div class="navbar-header"> <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#demo-navbar" aria-expanded="false"> <span class="sr-only">Toggle navigation</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <a class="navbar-brand" href="#">淘一淘</a> </div> <div class="collapse navbar-collapse" id="demo-navbar"> <ul class="nav navbar-nav"> <li><a href="#">電腦<span class="sr-only">(current)</span></a></li> <li><a href="#">手機</a></li> <li><a href="#">運動</a></li> <li><a href="#">女裝</a></li> <!--下拉菜單--> <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown">食品<span class="caret"></span></a> <ul class="dropdown-menu" role="menu"> <li><a href="#tab-milk">奶製品</a></li> <li><a href="#tab-rest">休閒食品</a></li> <li><a href="#tab-first">進口速食</a></li> <li><a href="#tab-drink">酒水飲料</a></li> </ul> </li> <li><a href="ListServlet">小零食</a></li> <li> <form class="navbar-form navbar-left" role="search"> <div class="form-group"> <input type="text" class="form-control" placeholder="搜索寶貝" /></div> <a href="#"><button type="submit" class="btn btn-default">搜索</button></a> </form> </li> </ul> <div style="margin-top:10px"> <form class="navbar-form navbar-right"> <a href="register.jsp" style="color:#71C671"><span class="glyphicon glyphicon-flash">註冊</span></a> <a href="login.jsp" style="color:#71C671"><span class="glyphicon glyphicon-log-out">登陸</span></a> </form> </div> </div> </nav> </body> </html>
foot.jspbootstrap
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <div id="divfoot"> <table width="100%" border="0" cellspacing="0"> <tr> <td rowspan="2" style="width:10%"> <img src="images/logo.jpg" width="100" height="40" style="margin-left:175px" /> </td> <td style="padding-top:5px; padding-left:20px"> <a href="#"> <font color="#747556"><b>CONTACT US</b></font> </a> </td> </tr> <tr> <td style="padding-left:20px"> <font color="#CCCCCC"> <b>COPYRIGHT 2016 © ItcastStore All Rights RESERVED.</b> </font> </td> </tr> </table> </div>
第一次寫博,感受有些累贅,就先到這裏吧dom