实现公告板和本周热卖功能

任务:

公告板用于发布与网站相关的信息,本周热卖用于展示本周内销售数量最多的两本图书

一、创建Servlet

index.jsp中存在<jsp:forward page="ShowIndexServlet"></jsp:forward>

/**
 *	前台页面展示的servlet 
 *	1、展示最新添加或修改的一条公告
 *  2、展示本周热销商品
 */
public class ShowIndexServlet extends HttpServlet{
	private static final long serialVersionUID = 1L;
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doPost(req, resp);
	}
	
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		//查询最近一条公告,传递到index.jsp页面进行展示
		NoticeService nService = new NoticeService();
		Notice notice = nService.getRecentNotice();
		req.setAttribute("n", notice);
		
		//查询本周热销的两条商品,传递到index.jsp页面进行展示
		ProductService pService = new ProductService();
		List<Object[]> pList =  pService.getWeekHotProduct();
		/*for(Object[] os:pList){
			for(Object o:os){
				System.out.println(o);
			}
			System.out.println("---------------------");
		}*/
		req.setAttribute("pList", pList);
		
		//请求转发
		req.getRequestDispatcher("/client/index.jsp").forward(req, resp);
	}
}

二、编写dao层代码

NoticeDao

//前台系统,查询最新添加或修改的一条公告
	public Notice getRecentNotice() throws SQLException {
		String sql = "select * from notice order by n_time desc limit 0,1";
		QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
		return runner.query(sql, new BeanHandler<Notice>(Notice.class));
	}

ProductDao

//前台,获取本周热销商品
	public List<Object[]> getWeekHotProduct() throws SQLException {
		String sql = "SELECT products.id,products.name, "+
                             " products.imgurl,SUM(orderitem.buynum) totalsalnum "+
                     " FROM orderitem,orders,products "+
                     " WHERE orderitem.order_id = orders.id "+
                             " AND products.id = orderitem.product_id "+
                             " AND orders.paystate=1 "+
                             " AND orders.ordertime > DATE_SUB(NOW(), INTERVAL 7 DAY) "+
                     " GROUP BY products.id,products.name,products.imgurl "+
                     " ORDER BY totalsalnum DESC "+
                     " LIMIT 0,2 ";
		QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
		return runner.query(sql, new ArrayListHandler());
	}
运行环境 操作系统:Windows XP。 Java平台:JDK 1.5。 Web服务器:Tomcat v 5.5.23,下载地址:http://tomcat.apache.org/。 数据库服务器:MySQL v 5.0.45,下载地址:http://www.mysql.com/。 开发平台:Eclipse SDK v 3.2.2,下载地址:http://www.eclipse.org/download/index.jsp。 Eclipse插件TomcatPlugins v 3.2.1,下载地址:http://www.eclipse-plugins.info/eclipse/index.jsp。 Eclipse插件ResourceBundleEditor v 0.7.7,下载地址:http://resourcebundleeditor.com/。 Eclipse插件MyEclipse v 5.5.1,下载地址:http://www.myeclipseide.com/ Spring 采用 2.0 版本 Hibernate 采用3.0版本 ====================================================== 请注意:如出现中文乱码,检查如下配置是否正确。 (1)MySql 数据库是否是utf-8 格式(在安装时选择支持多语言),数据是否正常。 (2)项目是否为utf-8格式(同时看看的源代码文件中,中文是否乱码)。 (3)JSP页面是否是utf-8 格式。 (4)在web.xml 是否配置了编码过滤器。 (5)数据源配置的url(?useUnicode=true&characterEncoding=UTF-8),具体请看项目实例。 如果上面5步都没问题,你就不存在中文乱码问题。 ====================================================== 数据库使用的是MySQL,其版本为5.0.45 版本。 数据库的用户名及密码均为root。 使用的时候,请参考附件数据库导入一节。或将需要用到的某章的数据库目录复制到“mysql安装根目录\data”文件夹下就可以了。 ★★★★★★★★★★★★★★★★★★★★★★★★★ 该实例来源下面这本书 《精通Java EE项目案例-基于Eclipse Spring Struts Hibernate》 本书下载链接,电驴,迅雷皆可 ed2k://|file|%E7%B2%BE%E9%80%9AJava.EE%E9%A1%B9%E7%9B%AE%E6%A1%88%E4%BE%8B-%E5%9F%BA%E4%BA%8EEclipse.Spring.Struts.Hibernate%E5%85%89%E7%9B%98%E6%BA%90%E7%A0%81.rar|70436209|475e7c3548acf955e89e378e760cf894|h=caud4x2auasf3pl2ln27e35fjw2jwxyb|/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值