从数据库中动态读取数据在页面进行显示

我们先是在servlet中调用方法
获取一个热门商品 也就是外键pid为1的商品,一个是最新商品,也就是排名靠前的几个商品
在获取的时候将他们的类型封装成

    return runner.query(sql, new BeanListHandler(Product.class),1,0,9);

这种形式在上一篇文章中提到过
然后放进request域 转发进入index.jsp
然后在jsp中使用el表达式 引入相关标签 进行循环遍历获取

package com.pandy.dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.pandy.domain.Product;
import com.pandy.utils.DataSourceUtils;

/**
* @author Pandy
* @version 2018年11月15日 下午10:07:14
*query更多的时候用来执行 无需参数的选择查询
*update 被用来指定插入 删除 更新的操作
*/
public class ProductDao {
    //查找热门商品
    public List findHotProductList() throws SQLException {
        // TODO Auto-generated method stub
        QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
        String sql = "select * from product where is_hot = ? limit ?,?";
        return runner.query(sql, new BeanListHandler(Product.class),1,0,9);
        
    }
    //查找最新商品
    public List findNewProductList() throws SQLException {
        // TODO Auto-generated method stub
        QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
        String sql = "select * from product order by pdate desc limit ?,?";
        return runner.query(sql, new BeanListHandler(Product.class),0,9);
    }

}

package com.pandy.web.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.pandy.domain.Product;
import com.pandy.service.ProductService;

/**
 * Servlet implementation class IndexServlet
 */
public class IndexServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public IndexServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.getWriter().append("Served at: ").append(request.getContextPath());
        response.setCharacterEncoding("utf8");
        ProductService service = new ProductService();
        //准备热门商品 List
        List hotProductList =  service.findHotProductList();
        
        //准备最新商品
        List newProductList =  service.findNewProductList();
        
        //将获取到的值放进request域  然后转发给index.jsp
        request.setAttribute("hotProductList", hotProductList);
        request.setAttribute("newProductList", newProductList);
        
        //
        request.getRequestDispatcher("/index.jsp").forward(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>


    
        
        黑马商城首页
        
        
        
    

    
        

热门商品  

${hotPro.pname }

¥${hotPro.shop_price }

最新商品  

${newPro.pname }

¥${newPro.shop_price }

冬瓜

¥299.00

从数据库中动态读取数据在页面进行显示_第1张图片
image.png

从数据库中动态读取数据在页面进行显示_第2张图片
image.png

成功获取到了数据
页面排版问题与访问数据库字符乱码问题。。。。。。
前者不解决 后者正在看

你可能感兴趣的:(从数据库中动态读取数据在页面进行显示)