首页热门商品展示
实现功能:在网站首页显示热门商品或最新上架商品(登录/不登录状态下都会显示)—使用Filter实现
LoginFilter.java(src/com.oracle.filter)
package com.oracle.filter;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import com.oracle.bean.Product;
import com.oracle.service.ProductService;
public class LoginFilter implements Filter{
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
// TODO Auto-generated method stub
try {
//收集首页数据
ProductService ps = new ProductService();
List<Product> list = ps.findProList();
request.setAttribute("list", list);
chain.doFilter(request, response);//放行
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}
Product.java(src/com.oracle.bean)
package com.oracle.bean;
public class Product {
//
//Field Type Comment
//pidvar char(64) NOT NULL
//pname varchar(50) NULL
//market_price double NULL
//shop_price double NULL
//pimage varchar(200) NULL
//pdate date NULL
//is_hot int(11) NULL
//pdesc varchar(255) NULL
//pflag int(11) NULL
//cid varchar(64) NULL
private String pid;
private String pname;
private double market_price;
private double shop_price;
private String pimage;
private String pdate;
private int is_hot;
private String pdesc;
private int pflag;
private String cid;
public Product() {
super();
// TODO Auto-generated constructor stub
}
public Product(String pid, String pname, double market_price, double shop_price, String pimage, String pdate,
int is_hot, String pdesc, int pflag, String cid) {
super();
this.pid = pid;
this.pname = pname;
this.market_price = market_price;
this.shop_price = shop_price;
this.pimage = pimage;
this.pdate = pdate;
this.is_hot = is_hot;
this.pdesc = pdesc;
this.pflag = pflag;
this.cid = cid;
}
public String getPid() {
return pid;
}
public void setPid(String pid) {
this.pid = pid;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public double getMarket_price() {
return market_price;
}
public void setMarket_price(double market_price) {
this.market_price = market_price;
}
public double getShop_price() {
return shop_price;
}
public void setShop_price(double shop_price) {
this.shop_price = shop_price;
}
public String getPimage() {
return pimage;
}
public void setPimage(String pimage) {
this.pimage = pimage;
}
public String getPdate() {
return pdate;
}
public void setPdate(String pdate) {
this.pdate = pdate;
}
public int getIs_hot() {
return is_hot;
}
public void setIs_hot(int is_hot) {
this.is_hot = is_hot;
}
public String getPdesc() {
return pdesc;
}
public void setPdesc(String pdesc) {
this.pdesc = pdesc;
}
public int getPflag() {
return pflag;
}
public void setPflag(int pflag) {
this.pflag = pflag;
}
public String getCid() {
return cid;
}
public void setCid(String cid) {
this.cid = cid;
}
}
ProductService.java(src/com.oracle.service)
package com.oracle.service;
import java.sql.SQLException;
import java.util.List;
import com.oracle.bean.Product;
import com.oracle.dao.ProductDao;
public class ProductService {
public List<Product> findProList() throws SQLException {
// TODO Auto-generated method stub
ProductDao pd = new ProductDao();
List<Product> list = pd.findProList();
return list;
}
}
ProductDao.java(src/com.oracle.dao)
package com.oracle.dao;
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import com.oracle.bean.Product;
import com.oracle.utils.DataSourceUtils;
public class ProductDao {
public List<Product> findProList() throws SQLException {
// TODO Auto-generated method stub
QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "select * from product where pflag=? order by pdate desc limit 0,9";
List<Product> list = qr.query(sql, new BeanListHandler<>(Product.class), 0);
return list;
}
}
index.jsp(WebContent/)
在热门商品部分使用jstl标签和el表达式解析查询到的数据。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>oracle商城首页</title>
<link rel="stylesheet" href="css/bootstrap.min.css" type="text/css" />
<script src="js/jquery-1.11.3.min.js" type="text/javascript"></script>
<script src="js/bootstrap.min.js" type="text/javascript"></script>
</head>
<body>
<div class="container-fluid">
<!-- 引入header.jsp -->
<jsp:include page="/header.jsp"></jsp:include>
<!-- 轮播图 -->
<div class="container-fluid">
<div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
<!-- 轮播图的中的小点 -->
<ol class="carousel-indicators">
<li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
<li data-target="#carousel-example-generic" data-slide-to="1"></li>
<li data-target="#carousel-example-generic" data-slide-to="2"></li>
</ol>
<!-- 轮播图的轮播图片 -->
<div class="carousel-inner" role="listbox">
<div class="item active">
<img src="img/1.jpg">
<div class="carousel-caption">
<!-- 轮播图上的文字 -->
</div>
</div>
<div class="item">
<img src="img/2.jpg">
<div class="carousel-caption">
<!-- 轮播图上的文字 -->
</div>
</div>
<div class="item">
<img src="img/3.jpg">
<div class="carousel-caption">
<!-- 轮播图上的文字 -->
</div>
</div>
</div>
<!-- 上一张 下一张按钮 -->
<a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
</div>
<!-- 热门商品 -->
<div class="container-fluid">
<div class="col-md-12">
<h2>热门商品 <img src="img/title2.jpg"/></h2>
</div>
<div class="col-md-2" style="border:1px solid #E7E7E7;border-right:0;padding:0;">
<img src="products/hao/big01.jpg" width="205" height="404" style="display: inline-block;"/>
</div>
<div class="col-md-10">
<div class="col-md-6" style="text-align:center;height:200px;padding:0px;">
<a href="product_info.htm">
<img src="products/hao/middle01.jpg" width="516px" height="200px" style="display: inline-block;">
</a>
</div>
<c:forEach items="${list}" var="pro">
<div class="col-md-2" style="text-align:center;height:200px;padding:10px 0px;">
<a href="product_info.htm">
<img src="${pageContext.request.contextPath}/${pro.pimage}" width="130" height="130" style="display: inline-block;">
</a>
<p><a href="product_info.html" style='color:#666'>${pro.pname}</a></p>
<p><font color="#E4393C" style="font-size:16px">¥${pro.shop_price}</font></p>
</div>
</c:forEach>
</div>
</div>
<!-- 广告条 -->
<div class="container-fluid">
<img src="products/hao/ad.jpg" width="100%"/>
</div>
<!-- 最新商品 -->
<div class="container-fluid">
<div class="col-md-12">
<h2>最新商品 <img src="img/title2.jpg"/></h2>
</div>
<div class="col-md-2" style="border:1px solid #E7E7E7;border-right:0;padding:0;">
<img src="products/hao/big01.jpg" width="205" height="404" style="display: inline-block;"/>
</div>
<div class="col-md-10">
<div class="col-md-6" style="text-align:center;height:200px;padding:0px;">
<a href="product_info.htm">
<img src="products/hao/middle01.jpg" width="516px" height="200px" style="display: inline-block;">
</a>
</div>
<div class="col-md-2" style="text-align:center;height:200px;padding:10px 0px;">
<a href="product_info.htm">
<img src="products/hao/small03.jpg" width="130" height="130" style="display: inline-block;">
</a>
<p><a href="product_info.html" style='color:#666'>冬瓜</a></p>
<p><font color="#E4393C" style="font-size:16px">¥299.00</font></p>
</div>
<div class="col-md-2" style="text-align:center;height:200px;padding:10px 0px;">
<a href="product_info.htm">
<img src="products/hao/small04.jpg" width="130" height="130" style="display: inline-block;">
</a>
<p><a href="product_info.html" style='color:#666'>冬瓜</a></p>
<p><font color="#E4393C" style="font-size:16px">¥299.00</font></p>
</div>
<div class="col-md-2 yes-right-border" style="text-align:center;height:200px;padding:10px 0px;">
<a href="product_info.htm">
<img src="products/hao/small05.jpg" width="130" height="130" style="display: inline-block;">
</a>
<p><a href="product_info.html" style='color:#666'>冬瓜</a></p>
<p><font color="#E4393C" style="font-size:16px">¥299.00</font></p>
</div>
<div class="col-md-2" style="text-align:center;height:200px;padding:10px 0px;">
<a href="product_info.htm">
<img src="products/hao/small03.jpg" width="130" height="130" style="display: inline-block;">
</a>
<p><a href="product_info.html" style='color:#666'>冬瓜</a></p>
<p><font color="#E4393C" style="font-size:16px">¥299.00</font></p>
</div>
<div class="col-md-2" style="text-align:center;height:200px;padding:10px 0px;">
<a href="product_info.htm">
<img src="products/hao/small04.jpg" width="130" height="130" style="display: inline-block;">
</a>
<p><a href="product_info.html" style='color:#666'>冬瓜</a></p>
<p><font color="#E4393C" style="font-size:16px">¥299.00</font></p>
</div>
<div class="col-md-2 yes-right-border" style="text-align:center;height:200px;padding:10px 0px;">
<a href="product_info.htm">
<img src="products/hao/small05.jpg" width="130" height="130" style="display: inline-block;">
</a>
<p><a href="product_info.html" style='color:#666'>冬瓜</a></p>
<p><font color="#E4393C" style="font-size:16px">¥299.00</font></p>
</div>
<div class="col-md-2" style="text-align:center;height:200px;padding:10px 0px;">
<a href="product_info.htm">
<img src="products/hao/small03.jpg" width="130" height="130" style="display: inline-block;">
</a>
<p><a href="product_info.html" style='color:#666'>冬瓜</a></p>
<p><font color="#E4393C" style="font-size:16px">¥299.00</font></p>
</div>
<div class="col-md-2" style="text-align:center;height:200px;padding:10px 0px;">
<a href="product_info.htm">
<img src="products/hao/small04.jpg" width="130" height="130" style="display: inline-block;">
</a>
<p><a href="product_info.html" style='color:#666'>冬瓜</a></p>
<p><font color="#E4393C" style="font-size:16px">¥299.00</font></p>
</div>
<div class="col-md-2 yes-right-border" style="text-align:center;height:200px;padding:10px 0px;">
<a href="product_info.htm">
<img src="products/hao/small05.jpg" width="130" height="130" style="display: inline-block;">
</a>
<p><a href="product_info.html" style='color:#666'>冬瓜</a></p>
<p><font color="#E4393C" style="font-size:16px">¥299.00</font></p>
</div>
</div>
</div>
<!-- 引入footer.jsp -->
<jsp:include page="/footer.jsp"></jsp:include>
</div>
</body>
</html>