session实现购物车功能实例

本项目设计了5个文件

(1)login.html为登录页面

(2)index.jsp为主页面,取出表单内容的方法为request.getParameter();设置session属性的方法为session.setAttribute();通过判断若发现用户没有登录,则会使用response的sendRedirect()方法重定向到登录页面login.html

(3)fruit.jsp为第一个购物页面,此项目在每个页面中都使用session.getAttribute()方法取出登录用户的用户名,并正确显示该登录用户。

(4)drink.jsp为第二个购物页面,使用session对象的setAttribute()方法,将第一次购物选择的商品和商品数量设置到会话范围内。

(5)result.jsp列出前两次购物所选购的商品和数量。

login.html的源代码如下:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>购物功能实例</title>
 
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
   
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

  </head>
 
  <body>
    <center>
    <h1>购物中心--欢迎登录</h1>
    <form action="index.jsp" method="post">
               姓名:<input type="text" name="name" size="12"><br>
        密码:<input type="password" name="pwd" size="12"><br>
        <input type="submit" name="submit" value="登录">
        <input type="reset" name="reset" value="重置"><br>
    </form>
    </center>
  </body>
</html>

index.jsp的源代码如下:

<%@ 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>购物功能实例</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>
 
  <body>
    <center>
    <%
     request.setCharacterEncoding("UTF-8");
     String name = request.getParameter("name");
     String pwd = request.getParameter("pwd");
     if(name != null && pwd != null && name.equals("guanlin") && pwd.equals("123"))
     {
      session.setAttribute("sessionname",name);
      out.println(name+",欢迎您的进入!");
     }
     else
     {
      response.sendRedirect("login.html");
     }
     %>
     <a href="session/Cart/fruit.jsp"><h1>单击开始购物</h1></a>
    </center>
  </body>
</html>
fruit.jsp的源代码如下:

<%@ 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>购物功能实例</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>
 
  <body>
    <%=session.getAttribute("sessionname") %>,欢迎您选购水果!
    <center>
    <h1>购买水果页面</h1>
    <form action="session/Cart/drink.jsp" method="post">
    水果:
    <select name="fruit">
       <option value="梨">梨</option>
       <option value="苹果">苹果</option>
       <option value="香蕉">香蕉</option>
       <option value="橘子">橘子</option>
       <option value="柚子">柚子</option>
       <option value="西红柿" selected>西红柿</option>
    </select><br>
    购买斤数:<input type="text" name="jin">斤<br>
    <input type="submit" name="submit" value="下一页"><br>
    </form>
    </center>
  </body>
</html>
drink.jsp的源代码如下:

<%@ 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>购物功能实例</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>
 
  <body>
    <%=session.getAttribute("sessionname") %>,欢迎您选购饮品!
    <%
     request.setCharacterEncoding("UTF-8");
     String fruit = request.getParameter("fruit");
     String num = request.getParameter("jin");
     if(fruit != null && num != null)
     {
      session.setAttribute("sessionfruit",fruit);
      session.setAttribute("sessionnum",num);
      out.println("您暂时购买了"+fruit+",购买了"+ num+"斤");
     }
     else
     {
      response.sendRedirect("fruit.jsp");
     }
     %>
     <center>
     <h1>购买饮料页面</h1>
    <form action="session/Cart/result.jsp" method="post">
    饮料:
    <select name="drink">
       <option value="啤酒">啤酒</option>
       <option value="汽水">汽水</option>
       <option value="可口可乐">可口可乐</option>
       <option value="牛奶">牛奶</option>
       <option value="奶茶">奶茶</option>
       <option value="矿泉水" selected>矿泉水</option>
    </select><br>
    购买瓶数:<input type="text" name="ping">瓶<br>
    <input type="submit" name="submit" value="确认购买">
    </form>
     </center>
  </body>
</html>
result.jsp的源代码如下:

<%@ 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>购物功能实例</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>
 
  <body>
    <%=session.getAttribute("sessionname") %>,欢迎您的光临!
    <center>
    <%
      request.setCharacterEncoding("UTF-8");
      String drink = request.getParameter("drink");
      String ping = request.getParameter("ping"); 
     %>
     您购买的物品有:
     <h3>
     <%=session.getAttribute("sessionfruit")%>,
     <%=session.getAttribute("sessionnum")%>斤
      <br>
      <%=drink %>,
      <%=ping %>瓶
      </h3>
    </center>
  </body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值