list.jsp
上传用户:sanhemotor
上传日期:2010-01-25
资源大小:381k
文件大小:8k
- <%@ page session="true" %>
- <%@ page import="java.sql.*,java.util.*" %>
- <html>
- <head>
- <title>JSP在线书店</title>
- <script language="JavaScript" type="text/JavaScript">
- <!--
- function MM_swapImgRestore() { //v3.0
- var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
- }
- function MM_preloadImages() { //v3.0
- var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
- var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
- if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
- }
- function MM_findObj(n, d) { //v4.01
- var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
- d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
- if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
- for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
- if(!x && d.getElementById) x=d.getElementById(n); return x;
- }
- function MM_swapImage() { //v3.0
- var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
- if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
- }
- //-->
- </script>
- <meta http-equiv="Content-Type" content="text/html; charset=gb2312"></head>
- <%
- String loginSign=(String)session.getValue("loginSign");
- if(loginSign.equals("OK"))
- {
- %>
- <body bgcolor="#F4F5FF" onLoad="MM_preloadImages('images/bb1_1.jpg','images/bb2_1.jpg','images/bb3_1.jpg','images/bb4_1.jpg','images/bb5_1.jpg')">
- <font face="Times New Roman,Times" size="+3"> </font>
- <table width="78%" border="0" align="center" cellpadding="0" cellspacing="0">
- <tr>
- <td width="20%"><img src="images/logo.jpg" width="230" height="120"></td>
- <td width="80%"><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="540" height="120">
- <param name="movie" value="images/8.swf">
- <param name="quality" value="high">
- <embed src="images/8.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="540" height="120"></embed></object></td>
- </tr>
- </table>
- <hr>
- <p>
- <center>
- <b><font color="#3300CC" size="+3" face="华文行楷">JSP在线书店</font></b>
- <table border="1">
- <tr>
- <td width="72"><a href="#" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image1','','images/bb1_1.jpg',1)"><img src="images/bb1.jpg" alt="欢迎光临恽奚皓的JSP书店" name="Image1" width="160" height="46" border="0"></a></td>
- <td width="80"><a href="#" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image2','','images/bb2_1.jpg',1)"><img src="images/bb2.jpg" alt="欢迎光临恽奚皓的JSP书店" name="Image2" width="160" height="45" border="0"></a></td>
- <td width="40"><a href="#" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image3','','images/bb3_1.jpg',1)"><img src="images/bb3.jpg" alt="欢迎光临恽奚皓的JSP书店" name="Image3" width="160" height="45" border="0"></a></td>
- <td width="88"><a href="#" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image4','','images/bb4_1.jpg',1)"><img src="images/bb4.jpg" alt="欢迎光临恽奚皓的JSP书店" name="Image4" width="160" height="45" border="0"></a></td>
- <td width="68"><a href="#" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image5','','images/bb5_1.jpg',1)"><img src="images/bb5.jpg" alt="欢迎光临恽奚皓的JSP书店" name="Image5" width="160" height="45" border="0"></a></td>
- </tr>
- <%! String item;%>
- <%! String[] title,author,pubhouse;%>
- <%! float[] price;%>
- <%! int[] id;%>
- <%! float totalAmount; %>
- <%! int count,removeNo,i; %>
- <%! boolean isExist;%>
- <%
- try
- {
- String sDBDriver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
- Class.forName(sDBDriver);
- String sConnStr="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=Web";
- String username="sa";
- String password="";
- Connection connWeb=DriverManager.getConnection(sConnStr,username,password);
- Statement stmt=connWeb.createStatement();
- String query="select bookname,author,price,publichouse,bookid from book_info";
- ResultSet rs=stmt.executeQuery(query);
- i=0;
- while(rs.next())
- {
- i++;
- }
- title=new String[i];
- author=new String[i];
- pubhouse=new String[i];
- price=new float[i];
- id=new int[i];
- i=0;
- rs=stmt.executeQuery(query);
- while(rs.next())
- {
- title[i]=rs.getString(1);
- author[i]=rs.getString(2);
- price[i]=rs.getFloat(3);
- pubhouse[i]=rs.getString(4);
- id[i]=rs.getInt(5);
- %>
- <tr>
- <td><%= title[i] %></td>
- <td><%= author[i] %></td>
- <td><%= price[i] %></td>
- <td><%= pubhouse[i] %></td>
- <td><A href="list.jsp?action=add&item=<%= i%>"><img src="images/wing_20.jpg" width="160" height="46"></a></td>
- </tr>
- <%
- i++;
- }
- rs.close();
- stmt.close();
- connWeb.close();
- }
- catch(Exception e)
- {
- out.println(e.getMessage());
- }
- %>
- </table>
- <%
- String action=request.getParameter("action");
- if(action!=null)
- {
- %>
- <form action="purchase.jsp" mothod="post">
- 您的购物车中有以下物品:
- <table border="1">
- <tr>
- <td>booktitle</td>
- <td>bookauthor</td>
- <td>price</td>
- <td>publichouse</td>
- <td>Quantity</td>
- <td>--------</td>
- </tr>
- <%
- isExist=false;
- Vector buyList=(Vector)session.getValue("shoppingcart");
- item=request.getParameter("item");
- int bookID;
- shop.Bookinfo cart=new shop.Bookinfo();
- try
- {
- bookID=Integer.parseInt(item);
- if(action.equals("add"))
- {
- cart.setId(id[bookID]);
- cart.setBookname(title[bookID]);
- cart.setAuthor(author[bookID]);
- cart.setPrice(price[bookID]);
- cart.setQuantity(1);
- cart.setPublichouse(pubhouse[bookID]);
- if (buyList==null)
- {
- buyList = new Vector();
- buyList.addElement(cart);
- }
-
- else
- {
- for (int i=0;i<buyList.size();i++)
- {
- int tempid=((shop.Bookinfo)buyList.elementAt(i)).getId();
- int tmp=((shop.Bookinfo)buyList.elementAt(i)).getQuantity();
- if(tempid==cart.getId())
- {
- isExist=true;
- ((shop.Bookinfo)buyList.elementAt(i)).setQuantity(tmp+1);
- }
- }
- if(!isExist)
- buyList.addElement(cart);
- }
- }
- if(action.equals("del"))
- {
- for (int i=0;i<buyList.size();i++)
- {
- int tempid=((shop.Bookinfo)buyList.elementAt(i)).getId();
- if (tempid==bookID)
- removeNo=i;
- }
- buyList.removeElementAt(removeNo);
- }
- session.putValue("shoppingcart",buyList);
-
- totalAmount=0;
- for(int i=0;i<buyList.size();i++)
- {
- out.println("<tr>");
- out.println("<td>");
- out.println(((shop.Bookinfo)buyList.elementAt(i)).getBookname());
- out.println("</td>");
- out.println("<td>");
- out.println(((shop.Bookinfo)buyList.elementAt(i)).getAuthor());
-
- out.println("</td>");
- out.println("<td>");
- out.println(((shop.Bookinfo)buyList.elementAt(i)).getPrice());
- out.println("</td>");
- out.println("<td>");
- out.println(((shop.Bookinfo)buyList.elementAt(i)).getPublichouse());
- out.println("</td>");
- out.println("<td>");
-
- out.println(((shop.Bookinfo)buyList.elementAt(i)).getQuantity());
- out.println("</td>");
- out.println("<td>");
- int tmp=((shop.Bookinfo)buyList.elementAt(i)).getId();
-
- int tmpQuantity=((shop.Bookinfo)buyList.elementAt(i)).getQuantity();
- float price=((shop.Bookinfo)buyList.elementAt(i)).getPrice();
- float booktotal=price*tmpQuantity;
- totalAmount=totalAmount+booktotal;
- %>
- <A href="list.jsp?action=del&item=<%= tmp%>">暂时不购买:)</a>
- <%
- out.println("</td>");
- out.println("</tr>");
- }
- out.println("</table>");
- out.println("total:"+totalAmount);
- out.println("<input type="submit" value="submit">");
- }
- catch(Exception e)
- {
- out.println(e.getMessage());
- }
- }
- }
- %>
- </form>
- </center>
- </body>
- </html>