⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shopcenter.jsp

📁 《JSP时尚编程百例》一例源代码81-100
💻 JSP
字号:
<%@ page language="java" %>
<%@ page import="java.lang.*" %>
<%@ page contentType="text/html; charset=gb2312" %>
<%@ page import="java.util.*" %>
<%@ page import="xbook.goods.*" %>
<%
    Vector shoppingCart=(Vector)session.getValue( "ShoppingCart" ) ;
    Hashtable gamesList=(Hashtable)session.getValue( "GamesList" ) ;
    boolean payBill=false ;
    
    if( shoppingCart==null ) {
      shoppingCart=new Vector() ;
      session.putValue( "ShoppingCart", shoppingCart ) ;
    }
    else {
      String userRequest=(String)request.getParameter( "request" ) ;
      String itemKey=(String)request.getParameter( "itemKey" ) ;
      if( userRequest!=null && itemKey!=null ) {
        if( userRequest.equals( "buy" ) )
          shoppingCart.addElement( gamesList.get( itemKey ) ) ;
        else if( userRequest.equals( "cancel" ) )
          try {
            shoppingCart.removeElementAt( Integer.parseInt( itemKey ) ) ;
          }
          catch( NumberFormatException ex ) {
          }
        else if( userRequest.equals( "pay" ) )
          payBill=true ;
        session.putValue( "ShoppingCart", shoppingCart ) ;
      }
    }
%>
<html>
<head>
<title>游戏软件购物中心</title>
</head>
<script language="JavaScript" >
  function selectRequest( r, i ) {
    switch(r) {
      case 1: document.form1.request.value="buy";
              break;
      case 2: document.form1.request.value="cancel";
      	      break;
      case 3: document.form1.request.value="pay";
      	      break;
    }
    document.form1.itemKey.value=i;
    document.form1.submit();
  }
</script>
<body bgcolor="#FFFFCC">
<%
    if( !payBill ) {
%>
<h1 align="center"><b><font color="#FF3333">游戏软件购物中心</font></b></h1>
<table width="100%" border="0" cellpadding="0">
  <tr>
    <td>
      <div align="center"><a href="http://localhost:8100/games/RoleGame.jsp">角色扮演类</a></div>
    </td>
    <td>
      <div align="center"><a href="http://localhost:8100/games/StrategyGame.jsp">即时战略类</a></div>
    </td>
    <td>
      <div align="center"><a href="http://localhost:8100/games/NetGame.jsp">网络游戏类</a></div>
    </td>
  </tr>
</table>
<hr>
<form name="form1" method="get" action="/games/ShopCenter.jsp">
<%
      if( gamesList!=null && gamesList.size()>0 ) {
        String gameType=(String)request.getAttribute( "GameType" ) ;
        if( gameType==null )
          gameType=(String)session.getValue( "CurrentGameType" ) ;
        else
          session.putValue( "CurrentGameType", gameType ) ;
%>
  <input type="hidden" name="request" value="" >
  <input type="hidden" name="itemKey" value="" >
  <table width="60%" border="1" cellpadding="2" align="center">
    <tr bgcolor="#CCCCFF"> 
      <td colspan="3">软件橱窗<%=gameType %></td>
    </tr>
    <tr> 
      <td width="15%" bgcolor="#9999FF"> 
        <div align="center">&nbsp;</div>
      </td>
      <td width="65%" bgcolor="#9999FF"> 
        <div align="center">货品名称</div>
      </td>
      <td width="20%" bgcolor="#9999FF"> 
        <div align="center">价格</div>
      </td>
    </tr>
<%
        for( Enumeration e=gamesList.keys() ; e.hasMoreElements() ; ) {
          String key=(String)e.nextElement() ;
          Goods item=(Goods)gamesList.get( key ) ;
%>
    <tr> 
      <td width="15%" bgcolor="#FFFFFF"> 
        <div align="center"> 
          <input type="button" name="buyBtn" value="购买" onClick="selectRequest(1,'<%=key %>')" >
        </div>
      </td>
      <td width="65%" bgcolor="#FFFFFF"><%=item.name %></td>
      <td width="20%" bgcolor="#FFFFFF"> 
        <div align="center"><%=item.price %></div>
      </td>
    </tr>
<%
        }
%>
  </table>
  <hr>
<%
      }
%>
  <table width="60%" border="1" cellpadding="2" align="center">
    <tr bgcolor="#CCFFCC"> 
      <td colspan="3"> 
        <div align="left">购物车</div>
      </td>
    </tr>
    <tr> 
      <td width="15%" bgcolor="#99FF99">&nbsp;</td>
      <td width="65%" bgcolor="#99FF99"> 
        <div align="center">货品名称</div>
      </td>
      <td bgcolor="#99FF99"> 
        <div align="center">价格</div>
      </td>
    </tr>
<%
      int i=0 ;
      for( Enumeration e=shoppingCart.elements() ; e.hasMoreElements() ; ) {
        Goods item=(Goods)e.nextElement() ;
%>
    <tr> 
      <td width="15%" bgcolor="#FFFFFF"> 
        <div align="center"> 
          <input type="button" name="cancelBtn" value="取消" onClick="selectRequest(2,'<%=i %>')" >
        </div>
      </td>
      <td width="65%" bgcolor="#FFFFFF"><%=item.name %></td>
      <td bgcolor="#FFFFFF"> 
        <div align="center"><%=item.price %></div>
      </td>
    </tr>
<%
	i++ ;
      }
%>
    <tr bgcolor="#006600"> 
      <td colspan="3"> 
        <div align="center"> 
          <input type="button" name="payBtn" value="结账" onClick="selectRequest(3,'')" >
        </div>
      </td>
    </tr>
  </table>
</form>
<%
    }
    else {
%>
<h1 align="center"><font color="#3333FF">谢谢光临</font></h1>
<table width="50%" border="1" cellpadding="2" align="center">
  <tr bgcolor="#CCFFCC"> 
    <td bgcolor="#FFCCCC"> 
      <div align="center">货品名称</div>
    </td>
    <td bgcolor="#FFCCCC"> 
      <div align="center">价格</div>
    </td>
  </tr>
<%
      int totalPrice=0 ;
      for( Enumeration e=shoppingCart.elements() ; e.hasMoreElements() ; ) {
        Goods item=(Goods)e.nextElement() ;
        totalPrice+=item.price ;
%>
  <tr> 
    <td width="70%" bgcolor="#FFFFFF"><%=item.name %></td>
    <td bgcolor="#FFFFFF"> 
      <div align="center"><%=item.price %></div>
    </td>
  </tr>
<%
      }
%>
  <tr bgcolor="#FF9999"> 
    <td colspan="2" height="21">总价:<%=totalPrice %></td>
  </tr>
</table>
<%
    }
%>
</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -