1 web层
1 IndexServlet
package www.test.web.servlet; import java.io.IOException; import java.sql.SQLException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import www.test.domain.Product; import www.test.service.ProductService; public class IndexServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ProductService service = new ProductService(); //获取热门商品-----List<Product> List<Product> hotProductList = null; try { hotProductList = service.findHotProductList(); } catch (SQLException e) { e.printStackTrace(); } //获取最新商品-----List<Product> List<Product> newProductList = null; try { newProductList = service.findNewProductList(); } catch (SQLException e) { e.printStackTrace(); } //将获取的数据存入request域 request.setAttribute("hotProductList", hotProductList); request.setAttribute("newProductList", newProductList); //转发 request.getRequestDispatcher("/index.jsp").forward(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
2 service层
1 ProductService
package www.test.service; import java.sql.SQLException; import java.util.List; import www.test.dao.ProductDao; import www.test.domain.Product; public class ProductService { // 获取热门商品 public List<Product> findHotProductList() throws SQLException { ProductDao dao = new ProductDao(); return dao.findHotProductList(); } // 获取最新商品商品 public List<Product> findNewProductList() throws SQLException { ProductDao dao = new ProductDao(); return dao.findNewProductList(); } }
3 dao层
1 ProductDao
package www.test.dao; import java.sql.SQLException; import java.util.List; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanListHandler; import www.test.domain.Product; import www.test.utils.C3P0Utils; public class ProductDao { //获取热门商品 public List<Product> findHotProductList() throws SQLException { QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource()); String sql = "select * from product where is_hot=? limit ?,?"; List<Product> hotProductList = qr.query(sql, new BeanListHandler<Product>(Product.class), 1,0,9); return hotProductList; } //获取最新商品 public List<Product> findNewProductList() throws SQLException { QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource()); String sql = "select * from product order by pdate desc limit ?,?"; List<Product> newProductList = qr.query(sql, new BeanListHandler<Product>(Product.class), 0,9); return newProductList; } }
4 domain层
1 product
package www.test.domain; import java.util.Date; public class Product { /*`pid` varchar(32) NOT NULL, `pname` varchar(50) DEFAULT NULL, `market_price` double DEFAULT NULL, `shop_price` double DEFAULT NULL, `pimage` varchar(200) DEFAULT NULL, `pdate` date DEFAULT NULL, `is_hot` int(11) DEFAULT NULL, `pdesc` varchar(255) DEFAULT NULL, `pflag` int(11) DEFAULT NULL, `cid` varchar(32) DEFAULT NULL*/ private String pid; private String pname; private double market_price; private double shop_price; private String pimage; private Date pdate; private int is_hot; private String pdesc; private int pflag; private Category category; 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 Date getPdate() { return pdate; } public void setPdate(Date 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 Category getCategory() { return category; } public void setCategory(Category category) { this.category = category; } }
2 Category
package www.test.domain; public class Category { private String cid; private String cname; public String getCid() { return cid; } public void setCid(String cid) { this.cid = cid; } public String getCname() { return cname; } public void setCname(String cname) { this.cname = cname; } }
5 WebContent
1 index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>黑马商城首页</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="${hotProductList }" var="hotPro"> <div class="col-md-2" style="text-align:center;height:200px;padding:10px 0px;"> <a href="product_info.htm"> <img src="${pageContext.request.contextPath }/${hotPro.pimage}" width="130" height="130" style="display: inline-block;"> </a> <p><a href="product_info.html" style='color:#666'>${hotPro.pname}</a></p> <p><font color="#E4393C" style="font-size:16px">¥${hotPro.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> <c:forEach items="${newProductList }" var="newPro"> <div class="col-md-2" style="text-align:center;height:200px;padding:10px 0px;"> <a href="product_info.htm"> <img src="${pageContext.request.contextPath }/${newPro.pimage}" width="130" height="130" style="display: inline-block;"> </a> <p><a href="product_info.html" style='color:#666'>${newPro.pname}</a></p> <p><font color="#E4393C" style="font-size:16px">¥${newPro.shop_price }</font></p> </div> </c:forEach> </div> </div> <!-- 引入footer.jsp --> <jsp:include page="/footer.jsp"></jsp:include> </div> </body> </html>
2 default.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <% response.sendRedirect(request.getContextPath()+"/index"); %> </body> </html>
3 web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>WEBTestShop28</display-name> <servlet> <servlet-name>CheckImgServlet</servlet-name> <servlet-class>www.test.web.servlet.CheckImgServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>CheckImgServlet</servlet-name> <url-pattern>/checkImg</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <description></description> <display-name>RegisterServlet</display-name> <servlet-name>RegisterServlet</servlet-name> <servlet-class>www.test.web.servlet.RegisterServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>RegisterServlet</servlet-name> <url-pattern>/register</url-pattern> </servlet-mapping> <servlet> <description></description> <display-name>ActiveServlet</display-name> <servlet-name>ActiveServlet</servlet-name> <servlet-class>www.test.web.servlet.ActiveServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>ActiveServlet</servlet-name> <url-pattern>/active</url-pattern> </servlet-mapping> <servlet> <description></description> <display-name>CheckUsernameServlet</display-name> <servlet-name>CheckUsernameServlet</servlet-name> <servlet-class>www.test.web.servlet.CheckUsernameServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>CheckUsernameServlet</servlet-name> <url-pattern>/checkUsername</url-pattern> </servlet-mapping> <servlet> <description></description> <display-name>CheckVCServlet</display-name> <servlet-name>CheckVCServlet</servlet-name> <servlet-class>www.test.web.servlet.CheckVCServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>CheckVCServlet</servlet-name> <url-pattern>/checkVC</url-pattern> </servlet-mapping> <servlet> <description></description> <display-name>LoginServlet</display-name> <servlet-name>LoginServlet</servlet-name> <servlet-class>www.test.web.servlet.LoginServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>LoginServlet</servlet-name> <url-pattern>/login</url-pattern> </servlet-mapping> <filter> <display-name>AutoLoginFilter</display-name> <filter-name>AutoLoginFilter</filter-name> <filter-class>www.test.web.filter.AutoLoginFilter</filter-class> </filter> <filter-mapping> <filter-name>AutoLoginFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <description></description> <display-name>IndexServlet</display-name> <servlet-name>IndexServlet</servlet-name> <servlet-class>www.test.web.servlet.IndexServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>IndexServlet</servlet-name> <url-pattern>/index</url-pattern> </servlet-mapping> </web-app>