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