2.我的負責模塊或任務說明
將數據庫中已經存在的商品取出,用表格顯示到頁面中。
實如今商品頁面的購買,直接彈出消息框,輸出價格,實現購買。
實如今商品頁面進行添加購物車,並轉到購物車
實如今購物車頁面顯示數據
實如今購物車頁面進行刪除表格的某一行數據
實如今購物車頁面進行購買,並從新返回商品頁面
3.本身的代碼提交記錄截圖

4.本身負責模塊或任務詳細說明
(1)將數據庫中已經存在的商品取出,用表格顯示到頁面中。
主要代碼:
<table border="1">
<tr>
<th>ID</th>
<th>NAME</th>
<th>PRICE</th>
<th>INVENTORY</th>
</tr>
<%
DButil util=new DButil();
List<ProductDB>list=util.readFirstProduct();
for(ProductDB e:list){ %>
<tr>
<td><%=e.getId() %></td>
<td><%=e.getName() %></td>
<td><%=e.getPrice() %></td>
<td><%=new Random().nextInt(100) %></td>
</tr>
<%} %>
</table>
public List<ProductDB> readFirstProduct() {
List<ProductDB> list = new ArrayList<ProductDB>();
Connection con = null;
PreparedStatement psmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/shopping", "root", "kimheechul");
String sql = "select * from product";
psmt = con.prepareStatement(sql);
rs = psmt.executeQuery();
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
double price = rs.getDouble("price");
ProductDB tl = new ProductDB(id, name, price);
list.add(tl);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
}
if (psmt != null) {
psmt.close();
}
if (con != null) {
con.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return list;
}
在DButil中定義好方法,使得其可以鏈接數據庫,獲取數據庫中的數據,並將數據輸入到List列表中,並返回列表。
在JSP中使用<table>、<tr>、<td>和<th>標籤製做表格,而且同時使用for循環進行遍歷list列表,將列表中的數據顯示到表格中,使用<%= %>就能夠實現。

(2)實如今商品頁面的購買,直接彈出消息框,輸出價格,實現購買。
主要代碼:
<form id="product" method="post" action="productList.jsp">
<select id="ID" name="NAME">
<%for(ProductDB e:list){ %>
<option value=<%=e.getName() %>><%=e.getName() %></option>
<%} %>
</select>
數量:<input id='count' type="text"/>
單價:<input id='price' type='text'>
<input type="submit" value="購買" onclick="fun()"/>
</form>
<script type="text/javascript">
function fun()
{
var obj=document.getElementById("ID");
for (i=0;i<obj.length;i++) {//下拉框的長度就是它的選項數.
if (obj[i].selected== true ) {
var text=obj[i].value;//獲取當前選擇項的 值 .
}
}
var price = document.getElementById('price').value;
var count = document.getElementById('count').value;
var total = (count*price).toFixed(2);
if(confirm("肯定購買"+text+"支付"+total+"元嗎?")){
window.location.href = "productList.jsp";
}
}
</script>
經過使用<form id="product" method="post" action="productList.jsp">
form表單來提交選中和輸入數據,同時使用action屬性來規定當咱們使用表單來提交數據時,數據提交到哪裏。使用<select> 標籤建立一個菜單,將 <option> 標籤中的內容做爲一個元素顯示,其值能夠用value=<%=e.getName() %>來實現輸入。
再經過<input>標籤和其type屬性定義一個「submit」提交按鈕,並使用onclick事件屬性使得咱們能夠在點擊購買按鈕時可以執行已經編寫好的JavaScript代碼。
在fun()中,咱們使用document.getElementById(id)來獲取<select>菜單中的值,及使用document.getElementById(id).value來獲取「text」字段中的值。且使用confirm()來顯示對話框,而且當有對話框出現後點擊的是肯定按鈕時,跳轉頁面。

(3)實如今商品頁面進行添加購物車,並轉到購物車
主要代碼:
</table>
<form id="product" method="post" action="productList.jsp">
<select id="ID" name="NAME">
<%for(ProductDB e:list){ %>
<option value=<%=e.getName() %>><%=e.getName() %></option>
<%} %>
</select>
數量:<input id='count' type="text"/>
單價:<input id='price' type='text'>
<input type="button" value="添加" onclick="add()"/>
</form>
<script type="text/javascript" language="javaScript">
function add(){
var obj=document.getElementById("ID");
for (i=0;i<obj.length;i++) {//下拉框的長度就是它的選項數.
if (obj[i].selected== true ) {
var text=obj[i].value;//獲取當前選擇項的 值 .
}
}
var price = document.getElementById('price').value;
var count = document.getElementById('count').value;
if(confirm("肯定要加入購物車?")){
window.location.href = "cart.jsp?count="+count+"&price="+price+"&name="+text;
}
}
</script>
此功能和上述功能類似,且使用的代碼相似。
經過使用<form id="product" method="post" action="productList.jsp">
form表單來提交選中和輸入數據,同時使用action屬性來規定當咱們使用表單來提交數據時,數據提交到哪裏。使用<select> 標籤建立一個菜單,將 <option> 標籤中的內容做爲一個元素顯示,其值能夠用value=<%=e.getName() %>來實現輸入。
再經過<input>標籤和其type屬性定義兩個「text」字段和一個「button」提交按鈕,並在其中使用onclick事件屬性使得咱們能夠在點擊購買按鈕時可以執行已經編寫好的JavaScript代碼。
在add()中,咱們使用document.getElementById(id)來獲取<select>菜單中的值,及使用document.getElementById(id).value來獲取「text」字段中的值。且使用confirm()來顯示對話框,而且當有對話框出現後點擊的是肯定按鈕時,跳轉頁面,並在其後添上參數,將咱們得到的值傳送到購物車頁面中。

(4)實如今購物車頁面顯示數據
主要代碼:
<table border="1">
<tr>
<th>ID</th>
<th>NAME</th>
<th>PRICE</th>
<th>NUMBER</th>
<th>操做</th>
<th>操做</th>
</tr>
<%int count = Integer.parseInt(request.getParameter("count"));%>
<%BigDecimal price = new BigDecimal(request.getParameter("price"));
price=price.setScale(2, BigDecimal.ROUND_HALF_UP);%>
<%String name = request.getParameter("name");%>
<%Connection con = null;
PreparedStatement psmt = null;
ResultSet rs = null;
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "kimheechul");
String strSql = "insert into cart(name,price,num) values(?,?,?)";
psmt = con.prepareStatement(strSql);
psmt.setString(1, name);
psmt.setBigDecimal(2,price);
psmt.setInt(3, count);
psmt.executeUpdate();
psmt.close();//當即釋放資源
DButil util=new DButil();
double total=0.0;
List<Product>list=util.readProduct();
for(Product e:list){ %>
<tr>
<td><%=e.getId() %></td>
<td><%=e.getName() %></td>
<td><%=e.getPrice() %></td>
<td><%=e.getNum() %></td>
</tr>
<%} %>
</table>
此方法和上述的在商品頁面中顯示數據的代碼類似,差異在於咱們須要使用request.getParameter()來得到商品頁面中表單使用post方式傳送過來的值,且由於其類型爲String型,須要將其轉換成對應的類型。再將數據插入數據庫中。其他與商品頁面的代碼大體同樣。

(5)實如今購物車頁面進行刪除表格的某一行數據
主要代碼:
<td align="center"> <a href="#" onclick="this.parentNode.parentNode.parentNode.removeChild(this.parentNode.parentNode)">刪除</a> </td>
刪除功能是經過使用<a></a>標籤,使用鏈接,使其匹配到this所指向的元素的父容器的父容器的父容器,(parentNode指的是父節點)也就是在這個元素外面第三層包裹他的那個元素。而後刪除這個節點,而且是刪除的是這個this元素指向的元素的父容器的父容器,也就是這個元素第二層包裹它的那個元素。

(6)實如今購物車頁面進行購買,並從新返回商品頁面
主要代碼:
<%
DButil util=new DButil();
double total=0.0;
List<Product>list=util.readProduct();
for(Product e:list){ %>
<tr>
<td><%=e.getId() %></td>
<td><%=e.getName() %></td>
<td><%=e.getPrice() %></td>
<td><%=e.getNum() %></td>
<% total=e.getPrice()*e.getNum();%>
<td><input type="button" name="calculate" onclick="setvalue(<%=total%>)" value="購買"></td>
</tr>
<%} %>
</table>
總價:<input type="text" name="result" id="result">
<script>
function setvalue(v)
{
document.getElementById("result").value=v.toFixed(2);
if(confirm("肯定要支付"+document.getElementById("result").value+"元嗎?")){
window.location.href = "productList.jsp";
}
}
</script>
一樣是經過List儲存數據庫數據,使用for循環遍歷列表,獲取數據庫的值,計算總價。
再經過<input>標籤和其type屬性定義一個「button」按鈕,並使用onclick事件屬性使得咱們能夠在點擊購買按鈕時可以執行已經編寫好的JavaScript代碼。
在setvalue()中,咱們使用document.getElementById("result").value=v.toFixed(2);來將得到的值顯示在「text」字段中。且使用confirm()來顯示對話框,而且當有對話框出現後點擊的是肯定按鈕時,跳轉頁面,支付成功。

5.課程設計感想
一、在課程設計的過程當中對於一些知識點的掌握更加鞏固了,尤爲是關於JSP的用法,比原來稍微瞭解了些
二、在課設的過程發現本身的編程知識儲備仍是比較薄弱的,對於在編寫代碼的過程當中發現的問題,還不是能很好地解決
三、在編寫代碼的過程當中,雖然有碰到不少的困難,可是也有不少的收穫,至少可以編寫WEB界面了。雖然是比較基礎的界面,可是也是一種進步和收穫
四、由於本週有兩場考試,因此作課程設計時間就變得很緊張了,並且開始作課設的時間又較遲,致使WEB界面沒有時間弄得很美觀了。但願下次可以作的更好吧。