最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JAVAEE model1模型實(shí)現(xiàn)商品瀏覽記錄(去除重復(fù)的瀏覽記錄)(一)

 更新時(shí)間:2016年11月16日 11:23:22   作者:scx_white  
這篇文章主要為大家詳細(xì)介紹了JAVAEE model1模型實(shí)現(xiàn)商品瀏覽記錄,去除重復(fù)的瀏覽記錄,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

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

Model1的可維護(hù)性  可擴(kuò)展性都是較差的  只適合小項(xiàng)目。

首先運(yùn)行結(jié)果

goods.jsp

<%@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) { 
              //循環(huán)遍歷集合 并顯示 
              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"> 
                產(chǎn)地:<%=item.getCity()%> 價(jià)格:¥ 
                <%=item.getPrice()%></dd> 
            </dl> 
          </div> <% 
  } 
  } 
 %> 
        </td> 
 
      </tr> 
    </table> 
  </center> 
</body> 
</html>

  在代碼中 表示商品的圖片

<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> 

通過點(diǎn)擊商品的圖片  把當(dāng)前商品的id傳值給details頁面
details.jsp通過商品的id來顯示詳細(xì)商品  ,而瀏覽記錄由cookies維護(hù)

<%@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(); 
                //根據(jù)request傳來的商品id 向dao中獲得相對(duì)應(yīng)的商品對(duì)象 
                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">產(chǎn)地:<%=item.getCity()%></td> 
              </tr> 
              <tr> 
                <td id="pricename">價(jià)格:<%=item.getPrice()%> ¥</td> 
              </tr> 
              <tr> 
                <td id="pricename">價(jià)格:<%=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); 
                //重新設(shè)置cookies 
                response.addCookie(c); 
              %> 
            </table> 
          </center></td> 
 
        <td width="30%" valign="top" id="historyview"> 
          <center> 
            <table> 
              <tr> 
                <td><b>你瀏覽過的商品</b></td> 
              </tr> 
              <% 
                //根據(jù)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>產(chǎn)地:<%=historyItem.getCity()%></td> 
              </tr> 
              <% 
                } 
                } 
              %> 
            </table> 
          </center> 
        </td> 
      </tr> 
    </table> 
 
  </center> 
</body> 
</html>

dao層  負(fù)責(zé)商品在數(shù)據(jù)庫中的查詢操作

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; 
 
//商品的業(yè)務(wù)邏輯類 
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(); 
      // 將查詢的結(jié)果依次加入集合 
      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 { 
      // 關(guān)閉資源 
      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; 
  } 
 
  // 根據(jù)商品編號(hào)獲取商品資料 
 
  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對(duì)象初始化 
      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 { 
      // 關(guān)閉資源 
      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; 
  } 
 
  // 根據(jù)cookie 獲得瀏覽的最后三個(gè)商品 
  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])); 
        /* 
         * 首先判斷集合中是否存在當(dāng)前物品 如果存在 counts+1 多讀取一次(保證list集合中有3個(gè)對(duì)象) 不添加此物品 
         */ 
        if (list.contains(item)) { 
          counts++; 
          continue; 
        } 
        list.add(item); 
      } 
    } 
    return list; 
  } 
} 

商品的實(shí)體類 Items

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都是一個(gè)新的對(duì)象 即使兩個(gè)商品的內(nèi)容全部一樣也不會(huì)相等  。所以要修改比較方式)
因?yàn)閷?duì)于瀏覽記錄而言  我們不能通過刷新當(dāng)前商品  瀏覽記錄全部都是該商品 我們只要保證該商品在瀏覽記錄中 只有一個(gè)即可
所以在dao層中的getHistoryView方法有這句代碼

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

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

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; 
  // 靜態(tài)塊代碼負(fù)責(zé)加載驅(qū)動(dòng) 
  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; 
  } 
} 

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java調(diào)用Deepseek-R1.1.5b大模型的超詳細(xì)教程(附代碼)

    Java調(diào)用Deepseek-R1.1.5b大模型的超詳細(xì)教程(附代碼)

    這篇文章主要為大家介紹了Java調(diào)用Deepseek-R1.1.5b大模型的超詳細(xì)教程并附上了代碼,文中的教程講解詳細(xì),有需要的小伙伴可以參考一下
    2025-03-03
  • Java繪圖庫JFreeChart的使用教程

    Java繪圖庫JFreeChart的使用教程

    圖表是一種以簡單方式顯示信息的圖形,JFreeChart允許創(chuàng)建各種交互式和非交互式圖表,本文主要介紹了Java繪圖庫JFreeChart的使用教程,感興趣的可以了解一下
    2023-09-09
  • Java四種訪問控制修飾符知識(shí)點(diǎn)總結(jié)

    Java四種訪問控制修飾符知識(shí)點(diǎn)總結(jié)

    本篇文章給大家詳細(xì)分析了Java四種訪問控制修飾符的相關(guān)知識(shí)點(diǎn),有興趣的朋友可以參考學(xué)習(xí)下。
    2018-03-03
  • 解決mac最新版intellij idea崩潰閃退crash的問題

    解決mac最新版intellij idea崩潰閃退crash的問題

    這篇文章主要介紹了解決mac最新版intellij idea崩潰閃退crash的問題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-09-09
  • Java定義泛型方法實(shí)例分析

    Java定義泛型方法實(shí)例分析

    這篇文章主要介紹了Java定義泛型方法,結(jié)合實(shí)例形式分析了java定義泛型的相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下
    2019-07-07
  • java如何獲取實(shí)體類的屬性名和屬性值

    java如何獲取實(shí)體類的屬性名和屬性值

    這篇文章主要介紹了java如何獲取實(shí)體類的屬性名和屬性值問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • 詳解SpringBoot禁用Swagger的三種方式

    詳解SpringBoot禁用Swagger的三種方式

    在生產(chǎn)環(huán)境下,我們需要關(guān)閉swagger配置,避免暴露接口的這種危險(xiǎn)行為。本文就詳細(xì)的介紹了3種情況,感興趣的可以了解一下
    2021-11-11
  • MyBatis中的自定義TypeHandler詳解

    MyBatis中的自定義TypeHandler詳解

    這篇文章主要介紹了MyBatis中的自定義TypeHandler詳解,定義的?typeHandler?泛型為?String,顯然我們要把數(shù)據(jù)庫的數(shù)據(jù)類型轉(zhuǎn)化為?String?型,然后實(shí)現(xiàn)設(shè)置參數(shù)和獲取結(jié)果集的方法,需要的朋友可以參考下
    2023-07-07
  • gson對(duì)象序列化的示例

    gson對(duì)象序列化的示例

    本文介紹如何將Java對(duì)象序列化為Json文件,然后讀取該Json文件讀取回Java對(duì)象。在下面的示例中,我們創(chuàng)建了一個(gè)Student類。然后生成一個(gè)student.json文件,該文件將具有Student對(duì)象的json數(shù)據(jù)。
    2020-11-11
  • Java單鏈表反轉(zhuǎn)圖文教程

    Java單鏈表反轉(zhuǎn)圖文教程

    這篇文章主要給大家介紹了關(guān)于Java單鏈表反轉(zhuǎn)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04

最新評(píng)論

滨州市| 厦门市| 绥宁县| 宜良县| 永宁县| 施甸县| 阜城县| 饶阳县| 灵宝市| 东丽区| 文山县| 沈阳市| 湟中县| 屏东市| 龙州县| 牙克石市| 麻城市| 舒兰市| 宁明县| 七台河市| 龙海市| 永仁县| 重庆市| 阿瓦提县| 呼玛县| 洛南县| 阳曲县| 乐业县| 绥芬河市| 白水县| 凤凰县| 阳高县| 东方市| 渭源县| 皮山县| 那坡县| 阜城县| 贵德县| 天长市| 顺义区| 通道|