Javaweb_21过滤器

一.login 登录
 

<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
 
</head>
<body>
 
<!-- 显示数据时,判断非空 -->
    <c:if test="${empty listGoods }">
        <jsp:forward page="load.do"></jsp:forward>
    </c:if>
 
    <h1 style="text-align: center;">我的购物商城首页</h1>
    <div style="text-align: center;">
         <form action="load.do" method="post">
              名称<input type="text" name="strName">
              <input type="submit" value ="搜索">
         </form>
    </div>
 
    <table border="1" width="70%" align = "center">
        <tr>
            <th>编号</th>
            <th>名称</th>
            <th>价格</th>
            <th>操作</th>
        </tr>
        
        <c:forEach items="${listGoods }" var="goods">
        
        <tr>
            <td>${goods.gid}</td>
            <td>${goods.gname}</td>
            <td>${goods.gprice}</td>
            <td>
                <button οnclick="addCart(${goods.gid})">加入购物车</button>
            </td>
        </tr>
</c:forEach>
        
    </table>
    
    <div style="text-align: center;">
      [${pageIndex }/${pageMax }]
      <a href="load.do?pageIndex=1">首页</a>
      <a href="load.do?pageIndex=${pageIndex-1<0?1:pageIndex-1 }">上一页</a>
      <a href="load.do?pageIndex=${pageIndex+1>pageMax?pageMax:pageIndex+1 }&strName=${strName}">下一页</a>
      <a href="load.do?pageIndex=${pageMax }">尾页</a>
    </div>
    
    
    <script type="text/javascript">
    //加入购物车的点击事件
    function addCart(cid){
        //alert(cid)
        location.href = "/shopping.do?cid="+cid;
    }
 
</script>
    
</body>
</html>

二.register 注册

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<!-- <link type="text/css" rel="stylesheet" href="css/style2.css" /> -->
<style type="text/css">
body { margin:0; padding:0; font-size:12px; line-height:20px; font-family:宋体; }
.wrap { margin:0 auto; width:960px; }
h2,h4,p,form,input,ul,li,dl,dt,dd { margin:0; padding:0; }
table { border-collapse:collapse; }
ul { list-style:none; }
a:link, a:visited { color:#039; text-decoration:none; }
a:hover, a:active { color:#03F; text-decoration:underline; }
/* clearFix */
.clearfix:after{content:".";display:block;height:0;clear:both;visibility:hidden}
.clearfix{ display:block; display:inline-block; }
 
.success { background:url(images/success_ico.gif) 20px 20px no-repeat; }
.success .information { margin-left:180px; }
 
#header { }
#header #logo { background:url(images/logo.jpg); width:263px; height:56px; text-indent:-1000em; }
#header #navbar { color:#fff; background:#fc883b; padding:2px; line-height:25px; text-align:right; }
#header .userMenu { float:left; }
#header .userMenu ul li { float:left; margin:0px 2px; display:inline; }
#header .userMenu ul li a { display:block; padding:0px 15px; text-decoration:none; background:#ffe0a3; white-space:nowrap; }
#header .userMenu ul li.current a { font-weight:bold; background:#fff; }
#header #navbar input { margin-right:5px; vertical-align:middle; border:1px solid #ccc; }
#header #navbar input.input-text { padding:2px 3px; width:120px; height:16px; }
#header #navbar input.input-btn { border:none; background:url(images/button_search.gif); height:22px; width:46px; }
 
#footer { border-top:2px solid #ccc; line-height:40px; text-align:center; }
 
#login { border:1px solid #666; margin:80px auto; padding:4px; width:360px; }
#login h2 { font-size:14px; line-height:30px; background:url(images/login_lock.gif) 5px center no-repeat; border-bottom:2px solid #ccc;; padding-left:30px; color:#9a0000; }
#login dl { line-height:50px; padding:5px; }
#login dl dt { float:left; clear:left; width:60px; text-align:right; }
#login dl dd { margin-left:60px; }
#login dl dd input { margin:10px; vertical-align:middle; }
#login dl dd input.input-text { border:1px solid #999999; font-size:14px; height:18px; width:150px; padding:4px 4px; }
#login dl dd.button input { height:26px; border:none; cursor:pointer; }
#login dl dd input.input-btn { background:url(images/button_login.gif); width:77px; }
#login dl dd input.input-reg { background:url(images/button_register.gif); width:143px; }
#login dl dd span { color:#f00; }
 
#register { border:1px solid #666666; margin:40px auto; padding:4px; width:750px; }
#register .title { background:#e7f6e5; }
#register .title h2 { font-size:20px; color:#666; padding:15px 100px; }
#register .steps { line-height:25px; margin:1px 0; }
#register .steps ul li { float:left; color:#663300; background-color:#ffe0a3; background-position:right; background-repeat:no-repeat; width:375px; text-align:center; }
#register .steps ul li.current { font-weight:bold; background-image:url(images/step_arrow.gif); }
#register .steps ul li.unpass { color:#000; background-color:#dfdfdf; }
#register .steps ul li.past { background-image:url(images/step_arrow_past.gif); }
#register .steps ul li.last { font-weight:bold; }
#register form { margin:10px auto; width:480px; }
#register form dl { line-height:50px; padding:20px 0; font-size:14px; color:#333; }
#register form dl dt { float:left; clear:left; width:78px; text-align:right; }
#register form dl dd { margin-left:78px; }
#register form dl dd input { margin:10px; vertical-align:middle; }
#register form dl dd input.input-text { border:1px solid #999999; font-size:14px; height:18px; width:200px; padding:4px 4px; }
#register form dl dd.button input { height:39px; border:none; cursor:pointer; }
#register form dl dd input.input-reg { background:url(images/button_reg.gif); width:154px; }
#register form dl dd span { color:#f00; }
#register .success { margin:50px auto; width:350px; }
#register .success .information { padding:60px 0; line-height:40px; color:#f00; }
 
#content { padding:4px 0; }
#content .list table { width:100%; text-align:center; font-size:14px; color:#3f413e; }
#content .list table tr th { line-height:26px; background:#ffeec2; border-top:1px solid #d3d3d3; border-bottom:1px solid #d3d3d3; }
#content .list th.checker { width:40px; }
#content .list th.price { width:80px; }
#content .list th.store { width:80px; }
#content .list th.view { width:150px; }
#content .list th.nums { width:150px; }
#content .list th.store { width:80px; }
#content .list th.orderId { width:80px; }
#content .list th.userName { width:80px; }
#content .list th.price { width:80px; }
#content .list th.createTime { width:140px; }
#content .list th.status { width:80px; }
#content .bookList table td { padding:10px 0; border-bottom:1px solid #8b8b8b; }
#content .bookList table td.title { text-align:left; font-weight:bold; }
#content .bookList table td.thumb img { border:1px solid #999; }
#content .bookList table tr.odd td { background:#f4faee; }
#content .bookList .input-text { border:1px solid #999; width:30px; text-align:center; }
#content .list .button { padding:10px 0; text-align:right; }
#content .list .button h4 { float:left; color:#f00; font-size:14px; }
#content .list .button .input-gray { font-size:12px; border:none; background:url(images/button_gray_bg.gif); color:#222; width:125px; height:22px; margin-left:10px; }
#content .bookList .button .input-btn { border:none; background:url(images/button_shop.gif); width:100px; height:22px; }
#content .bookList .button .input-chart { border:none; background:url(images/button_chart.png); width:128px; height:36px; }
#content .orderList td { font-size:12px; border:1px solid #e1e1e1; padding:10px 0; }
#content .orderList td.thumb img { border:1px solid #e1e1e1; }
 
#content .page-spliter { text-align:center; font-size:14px; padding-top:20px; }
#content .page-spliter * { margin:0 5px; }
#content .page-spliter .current { font-weight:bold; border-bottom:1px solid #000; }
 
#content .success { margin:50px auto; width:350px; font-size:14px; }
#content .success .information { padding:60px 0; line-height:40px; color:#f00; }
</style>
</head>
<body>
<div id="header" class="wrap">
    <div id="logo">卓京信息网上书城</div>
    <div id="navbar">
        <form method="get" name="search" action="">
            搜索:<input class="input-text" type="text" name="keywords" /><input class="input-btn" type="submit" name="submit" value="" />
        </form>
    </div>
</div>
<div id="register">
    <div class="title">
        <h2>欢迎注册卓京信息网上书城</h2>
    </div>
    <div class="steps">
        <ul class="clearfix">
            <li class="current">1.填写注册信息</li>
            <li class="unpass">2.进入首页</li>
        </ul>
    </div>
    <form method="post" action="Register.do">
        <dl>
            <dt>用户编号:</dt>
            <dd><input class="input-text" type="text" name="uid" /></dd>
            <dt>用  户 名:</dt>
            <dd><input class="input-text" type="text" name="uname" /></dd>
            <dt>密      码:</dt>
            <dd><input class="input-text" type="password" name="pwd" /></dd>
            <dt></dt>
            <dd class="button"><input class="input-reg" type="submit" name="register" value="" /></dd>
        </dl>
    </form>
</div>
<div id="footer" class="wrap">
    卓京信息网上书城 © 版权所有</div>
</body>
</html> 

三.index 主页

<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
 
</head>
<body>
 
<!-- 显示数据时,判断非空 -->
    <c:if test="${empty listGoods }">
        <jsp:forward page="load.do"></jsp:forward>
    </c:if>
 
    <h1 style="text-align: center;">我的购物商城首页</h1>
    <div style="text-align: center;">
         <form action="load.do" method="post">
              名称<input type="text" name="strName">
              <input type="submit" value ="搜索">
         </form>
    </div>
 
    <table border="1" width="70%" align = "center">
        <tr>
            <th>编号</th>
            <th>名称</th>
            <th>价格</th>
            <th>操作</th>
        </tr>
        
        <c:forEach items="${listGoods }" var="goods">
        
        <tr>
            <td>${goods.gid}</td>
            <td>${goods.gname}</td>
            <td>${goods.gprice}</td>
            <td>
                <button οnclick="addCart(${goods.gid})">加入购物车</button>
            </td>
        </tr>
</c:forEach>
        
    </table>
    
    <div style="text-align: center;">
      [${pageIndex }/${pageMax }]
      <a href="load.do?pageIndex=1">首页</a>
      <a href="load.do?pageIndex=${pageIndex-1<0?1:pageIndex-1 }">上一页</a>
      <a href="load.do?pageIndex=${pageIndex+1>pageMax?pageMax:pageIndex+1 }&strName=${strName}">下一页</a>
      <a href="load.do?pageIndex=${pageMax }">尾页</a>
    </div>
    
    
    <script type="text/javascript">
    //加入购物车的点击事件
    function addCart(cid){
        //alert(cid)
        location.href = "/shopping.do?cid="+cid;
    }
 
</script>
    
</body>
</html> 

四.购物车


<%@page import="javaWeb_MVC_Cart.entity.Cart"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
    
    //封装id
    function $(id){
        return document.getElementById(id);
    }
    
    //购物车每个商品数量的递增或递减
    function editGoodsCount(type,gid){
        //根据参数gid获取到tr对象【在遍历数据时,为每个tr标签上设置了id属性对应的就是商品编号】
        var tr = $(gid);
        //获取到指定的单元格(数量的那一列)
        var tdCount = tr.cells[3].children[1].value;
        //根据参数type判断递增还是递减
        if(type === 'minus'){//递减
            
            if(tdCount > 1){
                tdCount--;    
            }else{
                alert('商品件数不能少于1件')
            }
        }else if(type === 'add'){
            tdCount++;
        }
        //将更新后的数量重新设置到input输入项中
        tr.cells[3].children[1].value = tdCount;
        //调用计算价格的方法
        cartCalc();
    }
    
    //计算每件商品的总价
    function cartCalc(){
        //获取到表格对象
        var oTab = $("oTab");
        //获取到所有的行
        var trs = oTab.rows;
        //遍历
        for(var i = 1 ; i < trs.length ; i++){
            //获取商品的单价
            var gprice = trs[i].cells[2].innerHTML;
            //获取商品数量
            var gcount = trs[i].cells[3].children[1].value;
            //计算总价格
            var goodsPrice = parseInt(gprice) * parseInt(gcount);
            //将计算的总价格赋值到每个商品的总价格区域
            trs[i].cells[4].innerHTML = goodsPrice;
        }
    }
 
    //删除单个商品
    function delCart(gid){
        var flag = window.confirm("你确定要删除吗?");
        if(flag){
            window.location.href = "del.do?gid="+gid
        }
    }
    
    //修改单个商品
    function updateCart(gid){
        //根据id获取数量
        var gcount = $(id).cells[3].children[1].value;
        var flag = window.confirm("你确定要修改吗?");
        if(flag){
            window.location.href = "update.do?gid="+gid+"&gcount="+gcount;
        }
    }
    
</script>
 
</head>
<body>
    <h2 style="text-align: center;">我的购物车</h2>
    <hr/>
    <table id = "oTab" border ="1" width = "70%" align = "center">
    <tr>
        <th>编号</th>
        <th>名称</th>
        <th>价格</th>
        <th>数量</th>
        <th>总价格</th>
        <th>操作</th>
    </tr>
    <%
        //获取购物车
        List<Cart> listCarts = (List<Cart>)session.getAttribute("listCarts");
        
        //定义一个页码
        int pageIndex = 1;
        //定义一个变量存储每页显示的条数
        int pageSize = 4;
        //当用户点击了下一页   获取到下一页超链接上的参数
        String pIndex= request.getParameter("pageIndex");
        if(pIndex!=null){
            pageIndex = Integer.valueOf(pIndex);
        }
        
        //定义一个变量存储总记录数
        int pageCount = listCarts.size();
        /* System.out.println("总记录数: "+pageCount); */
        int start = (pageIndex-1)*pageSize;
        //sublist  参数end  没有等于
        int end = pageIndex*pageSize;
        if(end > pageCount){
            end = pageCount;
        }
        int pageMax = pageCount/pageSize;
        if(pageCount%pageSize!=0){
            pageMax++;
        }
        
        //调用伪分页的方法  subList(start,end);
        listCarts = listCarts.subList(start,end);
        
        for(Cart cart : listCarts){
    %>
        <c:forEach items="${sessionScope.list }" var="cart">
        <tr id = "<%=cart.getCid()%>">
            <td><%=cart.getGood().getGid() %></td>
            <td><%=cart.getGood().getGname() %></td>
            <td><%=cart.getGood().getGprice() %></td>
            <td>
                <button οnclick="add('a',<%=cart.getGood().getGid()%>)">-</button>
                <input type = "text" style ="width:40px" value = "<%=cart.getCcount() %>"/>
                <button οnclick="add('b',<%=cart.getGood().getGid()%>)">+</button>
            </td>
            <td><%=cart.getCtotal() %></td>
            <td>
                <button οnclick="delCart(<%=cart.getGood().getGid()%>)">删除</button>
                <button οnclick="updateCart(<%=cart.getGood().getGid()%>)">修改</button>
            </td>
        
        </tr>
        </c:forEach>
    
    <%        
        }
    %>
    
</table>
<!-- 购物车页面实现分页 -->
<!-- 每一页现实的商品条数4条 -->
<p style="text-align: center;">
    [<%=pageIndex %>/<%=pageMax %>]
    <a href = "cart.jsp?pageIndex=1">首页</a>
    <a href = "cart.jsp?pageIndex=<%=pageIndex-1<0?1:pageIndex-1%>">上一页</a>
    <a href = "cart.jsp?pageIndex=<%=pageIndex+1>pageMax?pageMax:pageIndex+1%>">下一页</a>
    <a href="cart.jsp?pageIndex=<%=pageMax%>">尾页</a>
    <a href = "index.jsp" style="text-align: center;">返回首页</a>
 
</p>
    
    <button οnclick="buy()">结算</button>
    
    <script type="text/javascript">
    
        function delCart(id){
            if(window.confirm("您确定要删除吗?")){
                location.href="del.do?gid="+id;
            }
        }
        
        
        function add(type,id){
            //根据参数id获取tr标签
            var tr = document.getElementById(id);
            //console.log(tr)
            //根据tr获取数量
            var ccount = tr.cells[4].children[1].value;
            console.log(ccount);
            if(type === 'a'){//减法
                ccount--;
            }else if(type==='b'){//加法
                ccount++;
            }
            tr.cells[4].children[1].value = ccount;
        }
        
        function updateCart(id){
            //根据id获取到数量
            var tr = document.getElementById(id);
            var ccount = tr.cells[4].children[1].value;
            //console.log(ccount);
            location.href="update.do?gid="+id+"&ccount="+ccount;
            
        }
    
        function buy() {
            location.href="submitBuy.do";
        }
        
    </script>
</body>
</html>


//--------------------------------SubmitBuyServlet--------------------------------------
 
 
//设置编码
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        
        //获取session
        HttpSession session = request.getSession();
        //通过session获取登录用户
        Users users = (Users) session.getAttribute("users");
        //根据用户获取该用户的购物车
        ICartBiz icb=new CartBizImpl();
        List<Cart> cart = icb.getCartByUserId(users.getUuid());
        Integer num=0;
        Integer price=0;
 
        for (Cart cart2 : cart) {
            num+=cart2.getCcount();
            price+=cart2.getCtotal();
        }
        
        Map<String,Object> maps = new HashMap<String,Object>();
        maps.put("cart", cart);
        maps.put("num", num);
        maps.put("price", price);
        
        session.setAttribute("maps", maps);
        
        response.sendRedirect(this.getServletContext().getContextPath()+"/confirmOrder.jsp");
        
    }


//------------------------------------SubmitOrderServlet---------------------------------
 
// 设置编码
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
 
        HttpSession session = request.getSession();
        // 通过session获取登录用户
        Users users = (Users) session.getAttribute("users");
        //数量和总价格
        int num=0;
        int price=0;
        ICartBiz icb=new CartBizImpl();
        List<Cart> cart=icb.getCartByUserId(users.getUuid());
        
        for (Cart cart2 : cart) {
            num+=cart2.getCcount();
            price+=cart2.getCtotal();
        }
        //收货地址
        String oaddress=request.getParameter("oaddress");
        Integer ostate=0; //0--未发货 1--已发货
        
        Orders order=new Orders();
        order.setOrderid(new BaseDao().getMaxID("orders", "orderid"));
        order.setUsers(users);
        order.setNum(num);
        order.setPrice(price);
        order.setOaddress(oaddress);
        order.setOstate(ostate);
        order.setListCart(cart);
        
        IOrdersBiz iob=new OrderBizImpl();
        iob.addOrder(order);
        
    }
————————————————
版权声明:本文为CSDN博主「t276tangqiqi」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/m0_67785100/article/details/124451166

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值