zoukankan      html  css  js  c++  java
  • JSP之购物车

    摘要:用jsp和servlet制作一个简单的购物车

     

    1,首先需要一个用来选择商品的JSP页面

    程序如下:

     1 <%@ page language="java" import="java.util.*" pageEncoding="GBK" contentType="text/html; charset=GBK"%>
     2 <%
     3 String path = request.getContextPath();
     4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
     5 %>
     6 
     7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
     8 <html>
     9   <head>
    10     <base href="<%=basePath%>">
    11     
    12     <title>My JSP 'Buy.jsp' starting page</title>
    13     
    14     <meta http-equiv="pragma" content="no-cache">
    15     <meta http-equiv="cache-control" content="no-cache">
    16     <meta http-equiv="expires" content="0">    
    17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    18     <meta http-equiv="description" content="This is my page">
    19     <!--
    20     <link rel="stylesheet" type="text/css" href="styles.css">
    21     -->
    22 
    23   </head>
    24   
    25   <body>
    26     <form action="buycar" method="post">
    27     电脑:<input type="checkbox" name="goods" value="computer"><br/>
    28     手机:<input type="checkbox" name="goods" value="phone"><br/>
    29   手表:<input type="checkbox" name="goods" value="watch"><br/>
    30   <input type="submit" value="购买"/>
    31     </form>
    32     
    33     
    34   </body>
    35 </html>

    2,需要一个servlet服务端

    程序如下:

     1 package servlet;
     2 
     3 import java.io.IOException;
     4 import java.util.HashMap;
     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 import javax.servlet.http.HttpSession;
    12 
    13 @WebServlet(urlPatterns={"/buycar"})
    14 public class ShopCar extends HttpServlet{
    15     private HttpSession hts;
    16     HashMap<String, Integer> hs=new HashMap<String, Integer>();
    17     static int sumcom=0;
    18     static int sumphone=0;
    19     static int sumwatch=0;
    20     @Override
    21     protected void service(HttpServletRequest req, HttpServletResponse res)
    22             throws ServletException, IOException {
    23         req.setCharacterEncoding("GBK");
    24         String[] goodshop=req.getParameterValues("goods");
    25         for(String goods:goodshop){
    26             if("computer".equals(goods)){
    27                 hs.put("电脑", ++sumcom);
    28             }
    29             if("phone".equals(goods)){
    30                 hs.put("手机", ++sumphone);
    31             }
    32             if("watch".equals(goods)){
    33                 hs.put("手表", ++sumwatch);
    34             }
    35         }
    36         hts=req.getSession();
    37         hts.setAttribute("shoppingCar", hs);
    38         req.getRequestDispatcher("shoppcar.jsp").forward(req, res);
    39     }
    40     
    41 }

    3,需要一个购买到商品的显示页面

    程序如下:

     1 <%@ page language="java" import="java.util.*" pageEncoding="GBK" contentType="text/html; charset=GBK"%>
     2 <%
     3 String path = request.getContextPath();
     4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
     5 %>
     6 
     7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
     8 <html>
     9   <head>
    10     <base href="<%=basePath%>">
    11     
    12     <title>My JSP 'shoppcar.jsp' starting page</title>
    13     
    14     <meta http-equiv="pragma" content="no-cache">
    15     <meta http-equiv="cache-control" content="no-cache">
    16     <meta http-equiv="expires" content="0">    
    17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    18     <meta http-equiv="description" content="This is my page">
    19     <!--
    20     <link rel="stylesheet" type="text/css" href="styles.css">
    21     -->
    22 
    23   </head>
    24   
    25   <body>
    26    <%
    27        request.setCharacterEncoding("GBK");
    28        HashMap<String,Integer> hs=(HashMap<String,Integer>)session.getAttribute("shoppingCar");
    29        if(hs!=null){
    30            Set set=hs.keySet();
    31            Iterator<String> it=set.iterator();
    32            while(it.hasNext()){
    33                String key=it.next();
    34                if(key.equals("电脑")){
    35                    out.print("购买电脑"+hs.get(key)+"台");
    36                }
    37                if(key.equals("手机")){
    38                    out.print("购买手机"+hs.get(key)+"台");
    39                }
    40                if(key.equals("手表")){
    41                    out.print("购买手表"+hs.get(key)+"台");
    42                }
    43            }
    44        }
    45        
    46        
    47        
    48        
    49     %>
    50    
    51    <a hres="Buy.jsp">继续购买</a>
    52   </body>
    53 </html>
  • 相关阅读:
    iOS Simulator功能介绍关于Xamarin IOS开发
    Unity中制作游戏的快照游戏支持玩家拍快照
    手机数据抓包入门教程
    Swift语言中为外部参数设置默认值可变参数常量参数变量参数输入输出参数
    Hierarchy视图里的Transform和Camera组件
    用JAVA编写MP3解码器——GUI(FFT)(转)
    功率W与dBm的对照表及关系(转)
    单鞭天线的长度计算方法(转)
    STM32F10X SPI操作flash MX25L64读写数据(转)
    利用STM32F唯一96bit序列号实现反拷贝加密的源代码公开(转)
  • 原文地址:https://www.cnblogs.com/ztyy04126/p/4960190.html
Copyright © 2011-2022 走看看