list.jsp
上传用户:sanhemotor
上传日期:2010-01-25
资源大小:381k
文件大小:8k
源码类别:

电子政务应用

开发平台:

Java

  1. <%@ page session="true" %>
  2. <%@ page import="java.sql.*,java.util.*" %>
  3. <html>
  4. <head>
  5. <title>JSP在线书店</title>
  6. <script language="JavaScript" type="text/JavaScript">
  7. <!--
  8. function MM_swapImgRestore() { //v3.0
  9.   var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
  10. }
  11. function MM_preloadImages() { //v3.0
  12.   var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
  13.     var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
  14.     if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
  15. }
  16. function MM_findObj(n, d) { //v4.01
  17.   var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
  18.     d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  19.   if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  20.   for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  21.   if(!x && d.getElementById) x=d.getElementById(n); return x;
  22. }
  23. function MM_swapImage() { //v3.0
  24.   var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
  25.    if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
  26. }
  27. //-->
  28. </script>
  29. <meta http-equiv="Content-Type" content="text/html; charset=gb2312"></head>
  30. <%
  31. String loginSign=(String)session.getValue("loginSign");
  32. if(loginSign.equals("OK"))
  33. {
  34. %>
  35. <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')">
  36. <font face="Times New Roman,Times" size="+3"> </font> 
  37. <table width="78%" border="0" align="center" cellpadding="0" cellspacing="0">
  38.   <tr>
  39.     <td width="20%"><img src="images/logo.jpg" width="230" height="120"></td>
  40.     <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">
  41.   <param name="movie" value="images/8.swf">
  42.   <param name="quality" value="high">
  43.   <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>
  44.   </tr>
  45. </table>
  46. <hr>
  47. <p>
  48. <center>
  49. <b><font color="#3300CC" size="+3" face="华文行楷">JSP在线书店</font></b>
  50. <table border="1">
  51. <tr>
  52.       <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>
  53.       <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>
  54.       <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>
  55.       <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>
  56.       <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>
  57. </tr>
  58. <%! String item;%>
  59. <%! String[] title,author,pubhouse;%>
  60. <%! float[] price;%>
  61. <%! int[] id;%>
  62. <%! float   totalAmount; %>
  63. <%! int count,removeNo,i; %>
  64. <%! boolean isExist;%>
  65. <%
  66. try
  67. {
  68. String  sDBDriver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
  69.       Class.forName(sDBDriver);
  70.  String sConnStr="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=Web";
  71. String username="sa";
  72. String password="";
  73. Connection connWeb=DriverManager.getConnection(sConnStr,username,password);
  74. Statement stmt=connWeb.createStatement(); 
  75. String query="select bookname,author,price,publichouse,bookid from book_info";
  76. ResultSet rs=stmt.executeQuery(query);
  77. i=0;
  78. while(rs.next())
  79. {
  80. i++;
  81. }
  82. title=new String[i];
  83. author=new String[i];
  84. pubhouse=new String[i];
  85. price=new float[i];
  86. id=new int[i];
  87. i=0;
  88. rs=stmt.executeQuery(query);
  89. while(rs.next())
  90. {
  91. title[i]=rs.getString(1);
  92. author[i]=rs.getString(2);
  93. price[i]=rs.getFloat(3);
  94. pubhouse[i]=rs.getString(4);
  95. id[i]=rs.getInt(5);
  96. %>
  97. <tr>
  98. <td><%= title[i] %></td>
  99. <td><%= author[i] %></td>
  100. <td><%= price[i] %></td>
  101. <td><%= pubhouse[i] %></td>
  102.       <td><A href="list.jsp?action=add&item=<%= i%>"><img src="images/wing_20.jpg" width="160" height="46"></a></td>
  103. </tr>
  104. <%
  105. i++;
  106. }
  107. rs.close();
  108. stmt.close();
  109. connWeb.close();
  110. }
  111. catch(Exception e)
  112. {
  113.       out.println(e.getMessage());
  114. }
  115. %>
  116. </table>
  117. <%
  118.   String action=request.getParameter("action");
  119.   if(action!=null)
  120.   {
  121. %>
  122.     <form action="purchase.jsp" mothod="post">
  123.     您的购物车中有以下物品:
  124.    <table border="1">
  125.    <tr>
  126.     <td>booktitle</td>
  127.     <td>bookauthor</td>
  128.     <td>price</td>
  129.     <td>publichouse</td>
  130.     <td>Quantity</td>
  131.     <td>--------</td>
  132.    </tr>
  133. <% 
  134.   isExist=false;
  135.   Vector buyList=(Vector)session.getValue("shoppingcart");
  136.   item=request.getParameter("item");
  137.   int bookID;
  138.   shop.Bookinfo cart=new shop.Bookinfo();
  139.  try
  140.   {
  141.    bookID=Integer.parseInt(item);
  142.    if(action.equals("add"))
  143.    {
  144.     cart.setId(id[bookID]);
  145.     cart.setBookname(title[bookID]);
  146.     cart.setAuthor(author[bookID]);
  147.     cart.setPrice(price[bookID]);
  148.     cart.setQuantity(1);
  149.     cart.setPublichouse(pubhouse[bookID]);
  150.     if (buyList==null)
  151.     {
  152.     buyList = new Vector();
  153.     buyList.addElement(cart);
  154.     }
  155.    
  156.     else
  157.     {
  158.       for (int i=0;i<buyList.size();i++)
  159.       {
  160.        int tempid=((shop.Bookinfo)buyList.elementAt(i)).getId();
  161.        int tmp=((shop.Bookinfo)buyList.elementAt(i)).getQuantity();
  162.            if(tempid==cart.getId())
  163.            {
  164.            isExist=true;
  165.       ((shop.Bookinfo)buyList.elementAt(i)).setQuantity(tmp+1);
  166.            }
  167.         }
  168.       if(!isExist)
  169.       buyList.addElement(cart);
  170.      }
  171.  }
  172.   if(action.equals("del"))
  173.   {
  174.     for (int i=0;i<buyList.size();i++)
  175.      {
  176.       int tempid=((shop.Bookinfo)buyList.elementAt(i)).getId();
  177.        if (tempid==bookID)
  178.        removeNo=i;
  179.       }
  180.       buyList.removeElementAt(removeNo);
  181.    }
  182.   session.putValue("shoppingcart",buyList);
  183.   
  184.   totalAmount=0;
  185.      for(int i=0;i<buyList.size();i++)
  186.      {
  187.      out.println("<tr>");
  188.      out.println("<td>");
  189. out.println(((shop.Bookinfo)buyList.elementAt(i)).getBookname());
  190.      out.println("</td>");
  191.      out.println("<td>");
  192.  out.println(((shop.Bookinfo)buyList.elementAt(i)).getAuthor()); 
  193.     
  194.      out.println("</td>");
  195.      out.println("<td>");
  196.      out.println(((shop.Bookinfo)buyList.elementAt(i)).getPrice());
  197.      out.println("</td>");
  198.      out.println("<td>");
  199. out.println(((shop.Bookinfo)buyList.elementAt(i)).getPublichouse());
  200.     out.println("</td>");
  201.      out.println("<td>");
  202.   
  203. out.println(((shop.Bookinfo)buyList.elementAt(i)).getQuantity());
  204.      out.println("</td>");
  205.      out.println("<td>");
  206.      int tmp=((shop.Bookinfo)buyList.elementAt(i)).getId();
  207.     
  208. int tmpQuantity=((shop.Bookinfo)buyList.elementAt(i)).getQuantity();
  209.     float price=((shop.Bookinfo)buyList.elementAt(i)).getPrice();
  210.     float booktotal=price*tmpQuantity;
  211.     totalAmount=totalAmount+booktotal;
  212. %>
  213.      <A href="list.jsp?action=del&item=<%= tmp%>">暂时不购买:)</a>
  214. <%
  215.      out.println("</td>");
  216.      out.println("</tr>");
  217.      }
  218.      out.println("</table>");
  219.      out.println("total:"+totalAmount);
  220.     out.println("<input type="submit" value="submit">");
  221.    }
  222. catch(Exception e)
  223. {
  224. out.println(e.getMessage());
  225. }    
  226. }
  227. }
  228.  %> 
  229. </form>
  230. </center>
  231. </body>
  232. </html>