欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JAVAEE model1模型实现商品浏览记录(去除重复的浏览记录)(一)

程序员文章站 2024-03-12 10:18:02
在javaee中model1模型是以jsp页面为中心的,jsp既要对浏览器的request做出逻辑处理(使用javabean),访问数据库也要显示出相关的页面。 在mod...

在javaee中model1模型是以jsp页面为中心的,jsp既要对浏览器的request做出逻辑处理(使用javabean),访问数据库也要显示出相关的页面。
在model1模型中,没有servlet
model1结果图如下:

JAVAEE model1模型实现商品浏览记录(去除重复的浏览记录)(一)

model1的可维护性  可扩展性都是较差的  只适合小项目。

首先运行结果

JAVAEE model1模型实现商品浏览记录(去除重复的浏览记录)(一)

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) { 
              //循环遍历集合 并显示 
              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>

  在代码中 表示商品的图片

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

<%@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层  负责商品在数据库中的查询操作

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

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方法有这句代码

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

然后是工具类
dbhelpher 单例模式获得connection对象

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。