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>

 

posted @ 2015-11-12 20:25  逍遥鸣  阅读(2008)  评论(0编辑  收藏  举报