实验报告

这是一个关于Web应用开发技术的实验报告,涵盖了基于MVC的JPetStore实现,包括商品展示、用户管理和订单管理,以及通过AJAX和jQuery提升用户体验的实践。实验扩展了验证码、购物车持久化、日志记录等功能,并在账号管理、查询商品、购物车操作等方面实现了AJAX交互,提高了用户界面的响应速度。
摘要由CSDN通过智能技术生成

Web应用开发技术实验报告

实验一 基于MVC用JSP/Servlet实现JPetStore

一、实验内容——基本任务

1. 商品展示业务模块,包括大类Category、小类Product和具体商品Item的展示和搜索功能。
1)运行界面
  • 大类Category展示
    在这里插入图片描述

  • 小类Product展示
    在这里插入图片描述

  • 商品Item展示
    在这里插入图片描述

  • 商品搜索功能(输入P进行搜索)

在这里插入图片描述

2)核心代码
  • 展示功能 ViewCatagory servlet &jsp
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
   
        categoryId = request.getParameter("categoryId");

        CatalogService service = new CatalogService();
        Category category = service.getCategory(categoryId);
        List<Product> productList = service.getProductListByCategory(categoryId);

        HttpSession session = request.getSession();
        session.setAttribute("category",category);
        session.setAttribute("productList",productList);

        request.getRequestDispatcher(VIEW_CATEGORY).forward(request,response);

    }


<c:forEach var="product" items="${sessionScope.productList}">
		<tr>
			<td>
				<a href="viewProduct?productId=${product.productId}">
					${
   product.productId}
				</a>
			</td>
			<td>
					${
   product.name}
			</td>
		</tr>
	</c:forEach>
  • ViewProduct servlet
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
   
        productId = request.getParameter("productId");
        CatalogService service = new CatalogService();

        Product product = service.getProduct(productId);

        List<Item> itemList = service.getItemListByProduct(productId);

        HttpSession session = request.getSession();
        session.setAttribute("product",product);
        session.setAttribute("itemList",itemList);

        request.getRequestDispatcher(VIEW_PRODUCT).forward(request,response);
    }
<c:forEach var="item" items="${sessionScope.itemList}">
		<tr>
			<td>
				<a href="viewItem?itemId=${item.itemId}">${
   item.itemId}</a>
			</td>
			<td>
					${
   item.product.productId}
			</td>
			<td>
					${
   item.attribute1} ${
   item.attribute2} ${
   item.attribute3}
					${
   item.attribute4} ${
   item.attribute5} ${
   sessionScope.product.name}
			</td>
			<td>
				<fmt:formatNumber value="${item.listPrice}" pattern="$#,##0.00" />
			</td>
			<td>
				<a class="Button" href="addItemToCart?workingItemId=${item.itemId}">Add to Cart</a>
			</td>
		</tr>
	</c:forEach>
  • ViewItem servlet
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
   
        itemId = request.getParameter("itemId");
        CatalogService service = new CatalogService();
        Item item=service.getItem(itemId);
        HttpSession session =request.getSession();
        session.setAttribute("item",item);
        request.getRequestDispatcher(VIEW_ITEM).forward(request,response);

    }
<table>
	<tr>
		<td>${
   sessionScope.product.description}</td>
	</tr>
	<tr>
		<td><b> ${
   sessionScope.item.itemId} </b></td>
	</tr>
	<tr>
		<td>
			<b >
				<font size="4">
					${
   sessionScope.item.attribute1}${
   sessionScope.item.attribute2}
					${
   sessionScope.item.attribute3}${
   sessionScope.item.attribute4}
					${
   sessionScope.item.attribute5}${
   sessionScope.product.name}
				</font>
			</b>
		</td>
	</tr>
	<tr>
			<td>1</td>
	</tr>
	<tr>
		<td>
			<c:if test="${sessionScope.item.quantity <= 0}">
        		Back ordered.
     		 </c:if>
			<c:if test="${sessionScope.item.quantity > 0}">
      			${
   sessionScope.item.quantity} in stock.
	 		 </c:if>
		</td>
	</tr>
	<tr>
		<td>
			<fmt:formatNumber value="${sessionScope.item.listPrice}" pattern="$#,##0.00" />
		</td>
	</tr>

	<tr>
		<td>
			<a class="Button" href="addItemToCart?workingItemId=${sessionScope.item.itemId }"> Add to Cart</a>
		</td>
	</tr>
</table>
  • 搜索功能 SearchProduct servlet & jsp
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
   
        keyword = request.getParameter("keyword");

        CatalogService service = new CatalogService();

        List<Product> productList = service.searchProductList(keyword);

        HttpSession session = request.getSession();

        session.setAttribute("productList",productList);

        request.getRequestDispatcher(VIEW_SEARCH_PRODUCT).forward(request,response);

    }
<c:forEach var="product" items="${sessionScope.productList}">
		<tr>
			<td>
                <a href="viewProduct?productId=${product.productId}">${
   product.description}</a>
            </td>
			<td><b>
                <a href="viewProduct?productId=${product.productId}"><font color="BLACK"> ${
   product.productId} </font></a>
            </b></td>
			<td>${
   product.name}</td>
		</tr>
	</c:forEach>
2.用户管理业务模块,包括用户注册、登录、修改用户信息、查询用户相关订单等业务功能。
1)运行界面
  • 注册
    在这里插入图片描述

  • 注册成功,数据库更新数据
    在这里插入图片描述

  • 登录
    在这里插入图片描述

  • 修改用户信息
    在这里插入图片描述

  • 数据库中显示修改后的用户信息
    在这里插入图片描述

  • 查询当前用户订单

在这里插入图片描述

2)核心代码
  • Register servlet & jsp
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
   
        HttpSession session = request.getSession();

        List<String> languages = new ArrayList<String>();
        languages.add("english");
        languages.add("japanese");
        session.setAttribute("languages",languages);

        List<String> categories = new ArrayList<>();
        categories.add("FISH");
        categories.add("DOGS");
        categories.add("REPTILES");
        categories.add("CATS");
        categories.add("BIRDS");
        session.setAttribute("categories",categories);

        request.getRequestDispatcher(REGISTER).forward(request,response);
    }
<form action="newAccount" method="post">
        <h3>User Information</h3>

        <div class="ui-form-item ui-border-b">
        <label>
            User ID
        </label>
        <input type="text"  name="username"  id="username" onblur="checkUsername();" placeholder="Enter the username">
        </div>
        <div>
            <p id="isExistInfo"></p>
        </div>

        <div class="ui-form-item ui-border-b">
            <label>
                New Password
            </label>
            <input type="password" name="password" id="password1" placeholder="Enter the password!"/>
        </div>

        <div class="ui-form-item ui-border-b">
            <label>
                Repeat password
            </label>
            <input type="password" name="repeatedPassword" id="password2" onblur="checkPassword()" placeholder="Enter the password again!"/>        </div>

        <div>
            <p id="password_Consist"></p>
        </div>
  • Login servlet & jsp
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
   
        username = request.getParameter("username");
        password = request.getParameter("password");
        randomCode =(String)request.getParameter("randomCode");
        AccountService accountService = new AccountService();
        account = accountService.getAccount(username,password);
//        System.out.println(account.getUsername());

        HttpSession session=request.getSession();
        randomUtils=(RandomUtils)session.getAttribute("randomUtils");
        if(randomUtils==null){
   
            randomUtils=new RandomUtils();
        }
        validRandomCode=randomUtils.code;
        System.out.println(validRandomCode);
        System.out.println(randomCode);

        if(account == null) {
   
            request.getRequestDispatcher(SIGNON).forward(request, response);
            System.out.println("yes");
        }else{
   
            if(validRandomCode.equals(randomCode)) {
   
                session.setAttribute("account", account);
                request.getRequestDispatcher(MAIN).forward(request, response);
                System.out.println("yanzhengchenggong");
            }else{
   

                request.getRequestDispatcher(SIGNON).forward(request, response);
            }
        }

    }
<form action="login" method="post" >
        <div class="ui-form-item ui-border-b">
            <label>
                Username
            </label>
            <input type="text"  name="username" placeholder
  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值