测试Servlet程序,查询所有的商品,但是最终查询结果始终为空。
package com.aistar.servlet.product;
import com.aistar.pojo.Product;
import com.aistar.service.ProductService;
import com.aistar.service.impl.ProductServiceImpl;
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 java.io.IOException;
import java.util.List;
@WebServlet(name = "ProductAllServlet",urlPatterns = "/all")
public class ProductAllServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//直接调用service层方法
ProductService service = new ProductServiceImpl();
List<Product> lists = service.getAll();
if (lists.size() != 0 ){
request.setAttribute("all",lists);
request.getRequestDispatcher("/product/all.jsp").forward(request,response);
}else {
request.setAttribute("all","商品不存在");
request.getRequestDispatcher("/product/all.jsp").forward(request,response);
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
原因是
开始位置这里没写,连接默认的是调用get请求,所以一直为空,没有结果。头大