ShowProductServlet
/**
* 功能:显示商品列表的控制程序
* 通过业务层访问后台数据,
* 然后将数据返回给前台页面
*/
package net.hjh.shop.servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import net.hjh.shop.bean.Product;
import net.hjh.shop.service.CategoryService;
import net.hjh.shop.service.ProductService;
import net.hjh.shop.service.CategoryService;
import net.hjh.shop.service.ProductService;
@WebServlet("/showProduct")
public class ShowProductServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 获取类别标识符
int categoryId = Integer.parseInt(request.getParameter("categoryId"));
// 创建商品类别服务对象
CategoryService categoryService = new CategoryService();
// // 由类别标识符获取类别名
String categoryName = categoryService.findCategoryById(categoryId).getName();
// 创建商品服务对象
ProductService productService = new ProductService();
// 获取指定商品类别的商品列表
List<Product> products = productService.findProductsByCategoryId(categoryId);
// 获取session对象
HttpSession session = request.getSession();
// 把商品列表对象以属性的方式保存到session里
session.setAttribute("products", products);
// 重定向到显示商品信息页面
response.sendRedirect(request.getContextPath() + "/frontend/showProduct.jsp?categoryName=" + categoryName);
//在服务器端控制台输出测试信息
for (int i=0;i<products.size();i++){
System.out.println(products.get(i));
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
在地址栏http://localhost:8080/simonshop/后面输入showProduct?categoryId=1
去服务器端控制台查看信息