ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

JSP购物车

2022-04-26 20:34:42  阅读:172  来源: 互联网

标签:String list 购物车 shipin JSP jsp page


在main.jsp中做一个购物车,里面显示3个商品名和价格 每一个后面有一个加入购物车按钮,main.jsp中有一个按钮(或者超链接)可以显示购物车。(选作:在购物车中加删除按钮删除商品)

d1

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<html>
  <head>
    
    <title>My JSP 'd1.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">

  </head>
  
  <body>
    This is my JSP page. <br>
    <%
     %>
     
     牛奶价钱:20
     <form action="d2.jsp" method="post">
     <input type="hidden" value="niunai" name="shipin"/>
     <input type="hidden" value="20" name="jiaqian"/>
     <input type="submit" value="购买" />
     </form>
       面包价钱:30
     <form action="d2.jsp" method="post">
     <input type="hidden" value="mianbao" name="shipin"/>
     <input type="hidden" value="30" name="jiaqian"/>
     <input type="submit" value="购买" />
     </form>
       水价钱:40
     <form action="d2.jsp" method="post">
     <input type="hidden" value="shui" name="shipin"/>
     <input type="hidden" value="40" name="jiaqian"/>
     <input type="submit" value="购买" />
     </form>
     
     <form action="d3.jsp" >
     <input type="submit" value="查看购物车"/>
     </form>
     
  </form>
  <a href="d4.jsp">刷新</a>
  </body>
</html>

d2

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'd2.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
   <%@ page import="Demo.demo" %>
  <%
  List<demo> list=(ArrayList<demo>)session.getAttribute("list");
  if(list==null){
  list=new ArrayList<demo>();
  session.setAttribute("list", list);
  }
  String shipin=request.getParameter("shipin");
  String jiaqian=request.getParameter("jiaqian");
  demo d=new demo();
  d.setName(shipin);
  d.setMoney(jiaqian);
  
  list.add(d);
  
  request.getRequestDispatcher("d1.jsp").forward(request, response);
  
  
   %>
  <body>
    This is my JSP page. <br>
  </body>
</html>

d3

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<html>
  <head>
    
    <title>My JSP 'd3.jsp' starting page</title>
    
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">

  </head>
  <%@ page import="Demo.demo" %>
  
  <body>
  
  <%
  List<demo> list=(ArrayList<demo>)session.getAttribute("list");
  if(list==null){
  out.print("购物车为空,准备跳转..");
  response.setHeader("refresh", "3;url=d1.jsp");
  }else{
  
  for(int i=0;i<list.size();i++){
    
    out.print("<form action='d5.jsp'>");
    out.print("<input type='hidden' name='shipin' value='"+list.get(i).getName()+"'/>");
    out.print("<input type='hidden' name='jiaqian' value='"+list.get(i).getMoney()+"'/>");
    out.print("商品名:"+list.get(i).getName()+"&nbsp价格:"+list.get(i).getMoney()+"&nbsp<input type='submit' value='删除'/>");
    out.print("</form>");
    out.println("<br>");
  }
  }
   %>
  
    <a href="d1.jsp">返回</a>
  </body>
</html>

d4

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'd4.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  <%
  session.invalidate();
  out.print("准备跳转..");
  response.setHeader("refresh", "2;url=d1.jsp");
   %>
  <body>
    
  </body>
</html>

d5

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'd5.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  <%@ page import="Demo.demo" %>
  <body>
   <%!public demo search(List<demo> list, String shipin) {
        for (demo d : list) {
            if (d.getName().equals(shipin)) {
                return d;
            }
        }
        return null;
    }%>
  <%
  String shipin=request.getParameter("shipin");
  String jiaqian=request.getParameter("jiaqian");
  List<demo> list=(ArrayList<demo>)session.getAttribute("list");
  demo d=new demo();
  d.setName(shipin);
  d.setMoney(jiaqian);
  list.remove(search(list, shipin));
  request.getRequestDispatcher("d3.jsp").forward(request, response);
   %>
  </body>
</html>

class

package Demo;

public class demo {
    String name;
    String money;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getMoney() {
        return money;
    }
    public void setMoney(String money) {
        this.money = money;
    }
    

}

 

 

 

 

 

标签:String,list,购物车,shipin,JSP,jsp,page
来源: https://www.cnblogs.com/liweimingbk/p/16196315.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有