一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術|正則表達式|

服務器之家 - 編程語言 - JAVA教程 - JAVAEE model1模型實現商品瀏覽記錄(去除重復的瀏覽記錄)(一)

JAVAEE model1模型實現商品瀏覽記錄(去除重復的瀏覽記錄)(一)

2020-07-05 13:32scx_white JAVA教程

這篇文章主要為大家詳細介紹了JAVAEE model1模型實現商品瀏覽記錄,去除重復的瀏覽記錄,具有一定的參考價值,感興趣的小伙伴們可以參考一下

在javaee中Model1模型是以jsp頁面為中心的,jsp既要對瀏覽器的request做出邏輯處理(使用javabean),訪問數據庫也要顯示出相關的頁面。
在model1模型中,沒有servlet。
Model1結果圖如下:

JAVAEE model1模型實現商品瀏覽記錄(去除重復的瀏覽記錄)(一)

Model1的可維護性  可擴展性都是較差的  只適合小項目。

首先運行結果

JAVAEE model1模型實現商品瀏覽記錄(去除重復的瀏覽記錄)(一)

goods.jsp

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<%@page import="entity.Items"%>
<%@page import="dao.ItemsDao"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
  String path = request.getContextPath();
  String basePath = request.getScheme() + "://"
      + request.getServerName() + ":" + request.getServerPort()
      + path + "/";
%>
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
 
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
  <link rel="stylesheet" type="text/css" href="styles.css">
  -->
<style type="text/css">
div {
  float: left;
  margin: 10px;
}
 
div dd {
  margin: 0px;
  font-size: 10pt;
}
 
div dd.dd_name {
  color: blue;
}
 
div dd.dd_city {
  color: #000;
}
</style>
</head>
 
<body>
  <center>
    <h1>商品展示</h1>
    <hr>
    <table width="800" height="60" cellpadding="0" cellspacing="0"
      border="0">
      <tr>
        <td>
          <%
            ItemsDao dao = new ItemsDao();
            ArrayList<Items> list = new ArrayList<Items>();
            //從dao中獲取所有的商品 并保存到list集合中
            list = dao.getAllItems();
            if (list != null && list.size() > 0) {
              //循環遍歷集合 并顯示
              for (int i = 0; i < list.size(); i++) {
                Items item = list.get(i);
          %>
          <div>
            <dl>
              <dt>
                <a href="details.jsp?id=<%=item.getId()%>"><img
                  src="images/<%=item.getPicture()%>" width="120" height="90"
                  border="1" />
                </a>
              </dt>
              <dd class="dd_name"><%=item.getName()%></dd>
              <dd class="dd_city">
                產地:<%=item.getCity()%> 價格:¥
                <%=item.getPrice()%></dd>
            </dl>
          </div> <%
  }
  }
 %>
        </td>
 
      </tr>
    </table>
  </center>
</body>
</html>

  在代碼中 表示商品的圖片

?
1
2
3
4
5
6
7
<span style="white-space:pre">               </span>
 
<a href="details.jsp?id=<%=item.getId()%>">
 
<img src="images/<%=item.getPicture()%>" width="120" height="90"  border="1" />
 
</a>

通過點擊商品的圖片  把當前商品的id傳值給details頁面
details.jsp通過商品的id來顯示詳細商品  ,而瀏覽記錄由cookies維護

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<%@page import="org.apache.taglibs.standard.tag.common.xml.ForEachTag"%>
<%@page import="entity.Items"%>
<%@page import="dao.ItemsDao"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
  String path = request.getContextPath();
  String basePath = request.getScheme() + "://"
      + request.getServerName() + ":" + request.getServerPort()
      + path + "/";
%>
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
 
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<style type="text/css">
#historyview {
  border: 1;
  background: #EAEAEE;
}
 
#historyview td {
  font-size: 10px;
}
</style>
</head>
 
<body>
  <center>
    <h1>商品詳情</h1>
    <hr>
    <table width="750" border="0" cellpadding="0" cellspacing="0">
      <tr>
        <td width="70%">
          <center>
            <table border="0">
              <%
                ItemsDao dao = new ItemsDao();
                //根據request傳來的商品id 向dao中獲得相對應的商品對象
                Items item = dao.getItemById(Integer.parseInt(request
                    .getParameter("id")));
                if (item != null) {
              %>
              <tr>
                <td rowspan="5"><img src="images/<%=item.getPicture()%>"
                  width="200" height="150"></td>
              </tr>
              <tr>
                <td><b><%=item.getName()%></b>
                </td>
              </tr>
              <tr>
                <td id="cityname">產地:<%=item.getCity()%></td>
              </tr>
              <tr>
                <td id="pricename">價格:<%=item.getPrice()%> ¥</td>
              </tr>
              <tr>
                <td id="pricename">價格:<%=item.getPrice()%> ¥</td>
              </tr>
              <%
                }
                //將該商品加入cookies
                Cookie[] cookies = request.getCookies();
                String historyStr = "";
                for (Cookie c : cookies) {
                  if (c.getName().equals("history")) {
                    historyStr = c.getValue();
                  }
                }
                historyStr += item.getId() + ",";
                Cookie c = new Cookie("history", historyStr);
                //重新設置cookies
                response.addCookie(c);
              %>
            </table>
          </center></td>
 
        <td width="30%" valign="top" id="historyview">
          <center>
            <table>
              <tr>
                <td><b>你瀏覽過的商品</b></td>
              </tr>
              <%
                //根據cookie 從dao獲取最后瀏覽的三次記錄 并保存到list集合
                ArrayList<Items> historyItems = dao.getHistoryView(historyStr);
                if (historyItems != null && historyItems.size() > 0) {
                  //遍歷集合
                  for (Items historyItem : historyItems) {
              %>
              <tr>
                <td><a href="details.jsp?id=<%=historyItem.getId()%>"><img
                    src="images/<%=historyItem.getPicture()%>" width="100"
                    height="80" border="1"> </a></td>
              </tr>
              <tr>
                <td><b><%=historyItem.getName()%></b>
                </td>
              </tr>
              <tr>
                <td>產地:<%=historyItem.getCity()%></td>
              </tr>
              <%
                }
                }
              %>
            </table>
          </center>
        </td>
      </tr>
    </table>
 
  </center>
</body>
</html>

dao層  負責商品在數據庫中的查詢操作

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package dao;
 
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import util.DBHelper;
import entity.Items;
 
//商品的業務邏輯類
public class ItemsDao {
  // 獲得所有商品信息
  public ArrayList<Items> getAllItems() {
    // 商品集合
    ArrayList<Items> list = new ArrayList<Items>();
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
 
    try {
      conn = DBHelper.getConnection();
      String sql = "select * from items";// sql 語句
      ps = conn.prepareStatement(sql);
      rs = ps.executeQuery();
      // 將查詢的結果依次加入集合
      while (rs.next()) {
        Items item = new Items();
        item.setId(rs.getInt("id"));
        item.setName(rs.getString("name"));
        item.setCity(rs.getString("city"));
        item.setPrice(rs.getDouble("price"));
        item.setPicture(rs.getString("picture"));
        item.setNumber(rs.getInt("number"));
        list.add(item);
      }
    } catch (SQLException e) {
 
      e.printStackTrace();
    } finally {
      // 關閉資源
      if (rs != null) {
        try {
          rs.close();
        } catch (SQLException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      }
      if (ps != null) {
        try {
          ps.close();
        } catch (SQLException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      }
 
    }
    return list;
  }
 
  // 根據商品編號獲取商品資料
 
  public Items getItemById(int id) {
    Items item = new Items();
    Connection con = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    String sql = "select * from items where id = ?";
    try {
      con = DBHelper.getConnection();
      ps = con.prepareStatement(sql);
      ps.setInt(1, id);
      rs = ps.executeQuery();
      // 如果找到該id 為item對象初始化
      if (rs.next()) {
        item.setId(rs.getInt("id"));
        item.setName(rs.getString("name"));
        item.setCity(rs.getString("city"));
        item.setPrice(rs.getDouble("price"));
        item.setPicture(rs.getString("picture"));
        item.setNumber(rs.getInt("number"));
      }
 
    } catch (SQLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } finally {
      // 關閉資源
      if (rs != null) {
        try {
          rs.close();
        } catch (SQLException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      }
      if (ps != null) {
        try {
          ps.close();
        } catch (SQLException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      }
    }
    return item;
  }
 
  // 根據cookie 獲得瀏覽的最后三個商品
  public ArrayList<Items> getHistoryView(String cookie) {
    ArrayList<Items> list = new ArrayList<Items>();
    String ids[] = cookie.split(",");
    int counts = 3;// 瀏覽的最后三條記錄
    if (ids != null && ids.length > 0) {
      for (int i = ids.length - 1; i >= 0 && i > ids.length - counts - 1; i--) {
        Items item = getItemById(Integer.parseInt(ids[i]));
        /*
         * 首先判斷集合中是否存在當前物品 如果存在 counts+1 多讀取一次(保證list集合中有3個對象) 不添加此物品
         */
        if (list.contains(item)) {
          counts++;
          continue;
        }
        list.add(item);
      }
    }
    return list;
  }
}

商品的實體類 Items

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package entity;
 
public class Items {
  private int id;
  private String name;
  private String city;
  private double price;
  private int number;
  private String picture;
 
  public int getId() {
    return id;
  }
 
  public void setId(int id) {
    this.id = id;
  }
 
  public String getName() {
    return name;
  }
 
  public void setName(String name) {
    this.name = name;
  }
 
  public String getCity() {
    return city;
  }
 
  public void setCity(String city) {
    this.city = city;
  }
 
  public double getPrice() {
    return price;
  }
 
  public void setPrice(double price) {
    this.price = price;
  }
 
  public int getNumber() {
    return number;
  }
 
  public void setNumber(int number) {
    this.number = number;
  }
 
  public String getPicture() {
    return picture;
  }
 
  public void setPicture(String picture) {
    this.picture = picture;
  }
   
  @Override
  public int hashCode() {
    // TODO Auto-generated method stub
    return this.getId()+this.getName().hashCode();
  }
  @Override
  public boolean equals(Object obj) {
    if(this==obj)
    {
      return true;
    }
    else
    {
      if(obj instanceof Items)
      {
        Items item=(Items) obj;
        if(this.getId()==item.getId()&&this.getName().equals(item.getName()))
        {
          return true;
        }
      }
    }
    return false;
  }
}

在這里  重寫了hasCode和equals方法  來修改比較方式(所有的item都是一個新的對象 即使兩個商品的內容全部一樣也不會相等  。所以要修改比較方式)
因為對于瀏覽記錄而言  我們不能通過刷新當前商品  瀏覽記錄全部都是該商品 我們只要保證該商品在瀏覽記錄中 只有一個即可
所以在dao層中的getHistoryView方法有這句代碼

?
1
2
3
4
<span style="white-space:pre">       </span>if (list.contains(item)) {
          counts++;
          continue;
        }

然后是工具類
DBHelpher 單例模式獲得connection對象

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package util;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
 
public class DBHelper {
  private static final String driver = "com.mysql.jdbc.Driver";
  private static final String url = "jdbc:mysql://localhost:3306/shopping?useUnicode=true&charcterEncoding=UTF-8";
  private static final String username = "root";
  private static final String password = "123";
  private static Connection con = null;
  // 靜態塊代碼負責加載驅動
  static {
    try {
      Class.forName(driver);
    } catch (ClassNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
 
  public static Connection getConnection() {
 
    if (con == null) {
      try {
        con = DriverManager.getConnection(url, username, password);
      } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
    return con;
  }
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:http://blog.csdn.net/su20145104009/article/details/53150005

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产香蕉一区二区在线网站 | 国产日产欧产精品精品软件 | 免费观看韩剧网站在线观看 | 精品欧美一区二区在线观看欧美熟 | 果冻传媒天美传媒乌鸦传媒 | 午夜A级理论片左线播放 | 成人精品视频一区二区在线 | 国产综合久久久久 | 国产v在线在线观看羞羞答答 | 韩国三级大全 | 欧美特级特黄a大片免费 | 91久| 亚洲国产在线99视频 | 韩国最新理论三级在线观看 | 91大神在线观看精品一区 | 亚洲日本久久一区二区va | 成年男女免费视频网站 | 狠狠躁夜夜躁人人爽天天miya | kkkk4444在线看片 | 欧美亚洲国产一区二区三区 | 国产精品久久久久久久久久久久 | 美女被吸乳老师羞羞漫画 | 男人把j放进女人的p里视频 | 99热久久这里只精品国产www | 男人桶女下面60分钟视频 | www.99精品视频在线播放 | 99久久这里只有精品 | 娇妻被健身教练挺进小说阅读 | 国产精品久久香蕉免费播放 | 亚洲成人aa | 交换朋友夫妇3中文字幕 | 黑帮大佬与我的365天2标清中文 | 精品无码国产AV一区二区三区 | 99精品在免费线视频 | 99在线视频观看 | 九九九九在线视频播放 | 天堂樱桃bt在线www | 国产精品热久久毛片 | 二区免费视频 | 成人网中文字幕色 | 男人在女人下面狂躁 |