/*生成博客目录的CSS*/ #uprightsideBar{ font-size:12px; font-family:Arial, Helvetica, sans-serif; text-align:left; position:fixed;/*将div的位置固定到距离top:50px,right:0px的位置,这样div就会处在最右边的位置,距离顶部50px*/ top:50px; right:0px; width: auto; height: auto; } #sideBarTab{ /* float:left; */ width:100px; border:1px solid #e5e5e5; border-right:none; text-align:center; background:#ffffff; cursor:pointer; } #sideBarContents{ float:left; overflow:auto; overflow-x:hidden;!important; width:300px; min-height:108px; max-height:460px; border:1px solid #e5e5e5; border-right:none; background:#ffffff; } #sideBarContents dl{ margin:0; padding:0; } #sideBarContents dt{ margin-top:5px; margin-left:5px; } #sideBarContents dd, dt { cursor: pointer; } #sideBarContents dd:hover, dt:hover { color:#A7995A; } #sideBarContents dd{ margin-left:20px; } #sideBarContents dd.indent3{ margin-left:40px; } #sideBarContents dd.indent4{ margin-left:60px; } #sideBarContents dd.indent5{ margin-left:80px; } #sideBarContents dd.indent6{ margin-left:100px; }

JSP技术

JSP入门

第1关:搭建你的第一个Web服务器

  • 任务描述
    认识和使用服务器是学习JavaWeb必须要掌握的知识,本关需要你根据文中步骤,搭建你的第一个Tomcat服务器。
  • 相关知识
  • 编程要求
    每次你进入实训我们会在后台帮你搭建好服务器运行所需的环境,请在右侧编辑器中输入hello educoder,点击评测-->查看效果,就可以看到你刚刚编写代码实现的效果哦。
  • 测试说明
    平台会对你的代码进行运行测试,如果实际输出结果与预期结果相同,则通关;反之,则 GameOver。
<body>
	<!-- 请在此 添加代码 -->
	<!-- begin -->
	hello educoder
	<!-- end -->
</body>

第2关:JSP基础(一)

  • 任务描述
    本小节需要完成:创建你的第一个动态网页。
  • 相关知识
  • 编程要求
    通过上面的例子,你应该可以发现在JSP页面中如果我们想要添加java代码,只需要在<%和%>这对标签中间填充代码即可。
    在右侧编辑器中的begin-end之间添加代码,请使用刚刚所学知识和for循环在网页中显示九九乘法表。
<body>
    <!-- 请在此处添加代码 -->
    <!-- begin -->
    <%
        for(int i=1;i<=9;i++){
            for(int j=1;j<=i;j++){
                out.println(i + "*" + j "=" + i*j);
                out.println("</br>");
            }
            out.println("<p></p>");
        }
    %>
    <!-- end -->
</body>

第3关:JSP基础测试题(一)

1-2 AB

第4关:JSP基础(二)

  • 任务描述
    本关需要你:使用三种JSP脚本元素创建动态网页。
  • 相关知识
<body>
	<!-- 创建一个公有的整形全局变量count 初始值为0-->
	<!-- start -->
	<%! int count = 0; %>
	<!-- end -->
	
	<!-- 使用JSP脚本程序将count变量+1之后输出 -->
	<!-- start -->
	<%
        count++;
        out.println(count);
	%>
	<!-- end -->
	
	<!-- 使用JSP表达式将count的值输出 -->
	<!-- start -->
	使用表达式输出的count值为:
	<%=count%>
	<!-- end -->

	<table width="800" cellpadding="0"  border = 1>
	<tr><td>i</td><td>i的平方</sup></td></tr>
	<!-- 在这里使用JSP脚本程序输出表格的行和列,循环的变量请使用 "i" 效果图请看编程要求 -->
	<!-- start -->
	<%
        for(int i=0;i<=5;i++){
            out.println("<tr><td>"+i+"</td><td>"+i*i+"</sup></td></tr>");
        }
	%>
	<!-- end -->
	</table>
</body>

第5关:JSP基础测试题(二)

1-2 CD

JSP技术基础

第1关:JSP脚本元素

1-5 DABBB
6-8 BCB

第2关:JSP指令

1-5 AABBB

第3关:JSP隐式对象

1-2 BD

第4关:EL表达式和JSTL标签库

1-5 ADCCD
6-8 DAD

JSP进阶

第1关:动作标签—请求转发

<jsp:forward page="WEB-INF/welcome.jsp"/>

第2关:EL表达式

id:${user.id}
userName:${user.userName}
host:${user.host}

第3关:JSTL标签库

...
<%-- 导入jstl核心标签库 --%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
...
<c:forEach var="stu" items="${students}">
  <tr>
    <td>${stu.id}</td>
    <td>${stu.username}</td>
    <td>${stu.wechat}</td>
    <td>
      <c:if test="${stu.sex==0}">
        女
      </c:if>
      <c:if test="${stu.sex==1}">
        男
      </c:if>
    </td>
  </tr>
</c:forEach>

基于JSP的网上商店

1-3关

package com.educoder.service.impl;

import java.util.List;

import com.educoder.entity.Goods;
import com.educoder.service.GoodsService;
import com.educoder.dao.impl.BaseDao;

import java.sql.*;
import java.lang.Exception;
import java.util.*;

public class GoodsServiceImpl implements GoodsService {
  public static final String url = "jdbc:mysql://127.0.0.1:3306/online_shop?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true";
  public static final String user = "root";
  public static final String password = "123123";

  private static Connection conn = null;
  private static Statement stmt = null;
  private static ResultSet rs = null;

  /**
   * 商品详情接口
   */
  public Goods getGoodsByGoodsId(String goodsId) {
    /********* Begin *********/
    String sql = "select * from t_goods where goodsId=?";
    List < Object > parameters = new ArrayList < Object > ();
    parameters.add(goodsId);

    List < Goods > goodsList = null;
    try {
      goodsList = BaseDao.operQuery(sql, parameters, Goods.class);
    } catch (Exception e) {
      e.printStackTrace();
    }

    return goodsList.get(0);
    /********* End *********/
  }

  /**
   * 商品搜索接口
   */
  public List < Goods > searchGoods(String condition) {
    /********* Begin *********/
    condition = "%" + condition + "%";
    String sql = "select * from t_goods where goodsName like ? or goodsClass like ?";
    List < Object > parameters = new ArrayList < Object > ();
    parameters.add(condition);
    parameters.add(condition);

    List < Goods > goodsList = null;
    try {
      goodsList = BaseDao.operQuery(sql, parameters, Goods.class);
    } catch (Exception e) {
      e.printStackTrace();
    }

    return goodsList;
    /********* End *********/
  }

  /**
   * 商品列表接口
   */
  public List < Goods > getGoodsList() {
    /********* Begin *********/
    List < Goods > goodsList = new ArrayList < Goods > ();

    try {
      //1.加载驱动程序
      Class.forName("com.mysql.jdbc.Driver");
      //2. 获得数据库连接
      conn = DriverManager.getConnection(url, user, password);
      stmt = conn.createStatement();
      rs = stmt.executeQuery("select * from t_goods order by salesNum desc limit 4");

      while (rs.next()) {
        Goods goods = new Goods();

        goods.setGoodsId(rs.getString("goodsId"));
        goods.setGoodsName(rs.getString("goodsName"));
        goods.setGoodsImg(rs.getString("goodsImg"));
        goods.setGoodsPrice(rs.getBigDecimal("goodsPrice"));
        goods.setGoodsNum(rs.getInt("goodsNum"));
        goods.setSalesNum(rs.getInt("salesNum"));
        goods.setGoodsSize(rs.getString("goodsSize"));
        goods.setGoodsFrom(rs.getString("goodsFrom"));
        goods.setGoodsTime(rs.getString("goodsTime"));
        goods.setGoodsSaveCondition(rs.getString("goodsSaveCondition"));
        goods.setGoodsDescribe(rs.getString("goodsDescribe"));
        goods.setGoodsExplain(rs.getString("goodsExplain"));
        goods.setGoodsClass(rs.getString("goodsClass"));
        goods.setGoodsDiscount(rs.getBigDecimal("goodsDiscount"));
        goods.setDiscountStartTime(rs.getDate("discountStartTime"));
        goods.setDiscountEndTime(rs.getDate("discountEndTime"));

        goodsList.add(goods);
      }
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
    } catch (SQLException e) {
      e.printStackTrace();
    }

    return goodsList;
    /********* End *********/
  }
}

4-5关

package com.educoder.service.impl;

import java.util.*;

import javax.servlet.http.HttpServletRequest;
import com.educoder.dao.impl.BaseDao;
import com.educoder.entity.Cart;
import com.educoder.entity.User;
import com.educoder.service.CartService;

public class CartServiceImpl implements CartService {

  // 查询购物车列表
  public List < Cart > getGoodsByUserId(HttpServletRequest request) {
    /********* Begin *********/
    List < Cart > cartList = null;

    // 获取user对象
    User user = (User) request.getSession().getAttribute("user");
    String userId = user.getUserId();

    String sql = "select * from t_goods, t_cart where t_goods.goodsId=t_cart.goodsId and t_cart.userId=?";
    List < Object > parameters = new ArrayList < Object > ();
    parameters.add(userId);

    try {
      cartList = BaseDao.operQuery(sql, parameters, Cart.class);
    } catch (Exception e) {
      e.printStackTrace();
    }

    return cartList;
    /********* End *********/
  }

  // 购物车操作接口
  @Override
  public void doCartHandle(String userId, String goodsId, String buyNum, String oper) {
    /********* Begin *********/
    String updateSql = "update t_cart set buyNum=? where userId=? and goodsId=?";
    String deleteSql = "delete from t_cart where userId=? and goodsId=?";
    String addSql = "insert into t_cart(userId, goodsId, buyNum, addTime) values(?,?,?,?)";

    List < Object > parameters = new ArrayList < Object > ();

    if (buyNum == null) {
      // 删除
      parameters.add(userId);
      parameters.add(goodsId);

      BaseDao.operUpdate(deleteSql, parameters);
    } else {
      List < Cart > cartList = null;
      String querySql = "select * from t_cart where userId=? and goodsId=?";
      List < Object > p = new ArrayList < Object > ();
      p.add(userId);
      p.add(goodsId);

      try {
        cartList = BaseDao.operQuery(querySql, p, Cart.class);
      } catch (Exception e) {
        e.printStackTrace();
      }

      if (cartList.isEmpty()) {
        // 不存在,添加进购物车
        parameters.add(userId);
        parameters.add(goodsId);
        parameters.add(buyNum);
        parameters.add(new Date());

        BaseDao.operUpdate(addSql, parameters);
      } else {
        // 存在,添加buyNum
        if (oper != null) {
          int oldNum = cartList.get(0).getBuyNum(); // 原本的数量

          parameters.add(oldNum + buyNum);
          parameters.add(userId);
          parameters.add(goodsId);

          BaseDao.operUpdate(updateSql, parameters);
        }
      }
    }

    /********* End *********/
  }
}

6-7关

package com.educoder.service.impl;

import java.util.*;

import com.educoder.dao.impl.BaseDao;
import com.educoder.entity.Order;
import com.educoder.service.OrderService;

public class OrderServiceImpl implements OrderService {
  private CartServiceImpl cartService = new CartServiceImpl();

  // 下单
  public Order submitOrder(String userId, String addressId, String[] goodsBuyNum,
    String[] chooseGoodId) {
    /********* Begin *********/
    Order order = new Order();
    String orderSql = "insert into t_order(orderId, userId, orderTime, addressId) values(?,?,?,?)";
    String orderChildSql = "insert into t_order_child(orderId, goodsId, buyNum) values(?,?,?)";
    // 添加Order
    String orderId = UUID.randomUUID().toString();

    List < Object > parameters = new ArrayList < Object > ();
    parameters.add(orderId);
    parameters.add(userId);
    parameters.add(new Date());
    parameters.add(addressId);

    BaseDao.operUpdate(orderSql, parameters);

    // 添加OrderChild
    for (int i = 0; i < chooseGoodId.length; i++) {
      parameters.clear();
      parameters.add(orderId);
      parameters.add(chooseGoodId[i]);
      parameters.add(goodsBuyNum[i]);

      BaseDao.operUpdate(orderChildSql, parameters);

      // 清空购物车
      cartService.doCartHandle(userId, chooseGoodId[i], null, null);
    }

    order.setOrderId(orderId);
    order.setUserId(userId);
    order.setOrderTime(new Date());
    order.setAddressId(addressId);

    return order;
    /********* End *********/
  }

  // 订单查询
  public List < Order > getOrderByUserId(String userId) {
    /********* Begin *********/
    // a: t_goods
    // b: t_order
    // c: t_order_child
    // d: address
    // e: user
    String sql = "select d.address, c.buyNum, a.goodsId, a.goodsName, a.goodsPrice, b.orderId, b.orderTime, b.userId, e.userName from t_goods as a,t_order as b,t_order_child as c,t_address as d,t_user as e where b.userId=? and b.orderId=c.orderId and b.userId=e.userId and b.addressId=d.addressId and c.goodsId=a.goodsId order by orderTime desc";

    List < Object > parameters = new ArrayList < Object > ();
    parameters.add(userId);

    List < Order > orderList = null;

    try {
      orderList = BaseDao.operQuery(sql, parameters, Order.class);
    } catch (Exception e) {
      e.printStackTrace();
    }

    return orderList;
    /********* End *********/
  }

}
posted @ 2023-10-01 22:48  你的脑子能压几个栈  阅读(213)  评论(0)    收藏  举报