• Cookie 简单使用记录浏览记录


    ItemsDAO.java

    package dao;
    import java.util.* ;
    import java.sql.* ;
    
    import util.DBHelper;
    
    import entity.Items;
    //商品的业务逻辑类
    public class ItemsDAO {
        static public   ArrayList<Items> getAllItems()
        {
            Connection conn = null ;
            PreparedStatement stmt = null ;
            ResultSet rs = null ;    //数据集
            ArrayList<Items> list = new ArrayList<>() ;
            try {
                conn = DBHelper.getConnection();
                String sql = "select * from items;" ;
                stmt = conn.prepareStatement(sql);
                rs = stmt.executeQuery();
                while(rs.next()){
                    Items item = new Items() ;
                    item.setId(rs.getInt("id"));
                    item.setName(rs.getString("name"));
                    item.setCity(rs.getString("city"));
                    item.setPricce(rs.getInt("pricce"));
                    item.setPicture(rs.getString("picture"));
                    
                    list.add(item);
                }
                return list ;
                
            } catch (Exception e){
                e.printStackTrace() ;
                return null ;
            } 
            
            finally{
                //释放数据集对象
                if(rs != null)
                {
                    try{
                        rs.close() ;
                        rs = null ;
                    } catch (Exception ex){
                        ex.printStackTrace() ;
                    }
                }
                //释放语句对象
                
                if(stmt != null)
                {
                    try{
                        stmt.close() ;
                        stmt = null ;
                    } catch (Exception ex){
                        ex.printStackTrace() ;
                    }
                }
                
            }
            
            
        }
        
        
        //根据商品编号获得商品资料
        public Items getItemsById(int id){
            Connection conn = null ;
            PreparedStatement stmt = null ;
            ResultSet rs = null ;    //数据集
            try {
                conn = DBHelper.getConnection();
                String sql = "select * from items where id = ?;" ;
                stmt = conn.prepareStatement(sql);
                stmt.setInt(1,id);
                rs = stmt.executeQuery();
                if(rs.next()){
                    Items item = new Items() ;
                    item.setId(rs.getInt("id"));
                    item.setName(rs.getString("name"));
                    item.setCity(rs.getString("city"));
                    item.setPricce(rs.getInt("pricce"));
                    item.setPicture(rs.getString("picture"));
                    
                    return item;
                }
                else
                    return null ;
                
            } catch (Exception e){
                e.printStackTrace() ;
                return null ;
            } 
            
            finally{
                //释放数据集对象
                if(rs != null)
                {
                    try{
                        rs.close() ;
                        rs = null ;
                    } catch (Exception ex){
                        ex.printStackTrace() ;
                    }
                }
                //释放语句对象
                
                if(stmt != null)
                {
                    try{
                        stmt.close() ;
                        stmt = null ;
                    } catch (Exception ex){
                        ex.printStackTrace() ;
                    }
                }
        }
        
        }
        //获取最近浏览的前五条
        public ArrayList<Items> getViewList(String list){
            ArrayList<Items> itemlist = new ArrayList<Items> () ;
            int iCount = 5 ;
            if(list != null && list.length()>0){
                String [] arr = list.split(",");
                
                if(arr.length >= 5){            
                    for(int i = arr.length-1 ; i >= arr.length-iCount-1 ; i--){
                         itemlist.add(getItemsById(Integer.parseInt(arr[i])));
                    }
                } else {
                    for(int i = arr.length-1 ; i>=0 ; i --){
                         itemlist.add(getItemsById(Integer.parseInt(arr[i])));
                    }
                }
                
                return itemlist ;
            } else {
                return null ;
            }
        }
    }

    Items.java

    package entity;
    
    public class Items {
        private int id;
        private String name;
        private String city;
        private int pricce;
        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 int getPricce() {
            return pricce;
        }
    
        public void setPricce(int pricce) {
            this.pricce = pricce;
        }
    
        public String getPicture() {
            return picture;
        }
    
        public void setPicture(String picture) {
            this.picture = picture;
        }
    
    }

    DBHElper.java

    package util;
    import java.sql.Connection;
    import java.sql.DriverManager;
    
    
    public class DBHelper {
        
        private static final String driver = "com.mysql.jdbc.Diver";
        private static final String url = "jdbc:mysql://localhost:3306/zhuopengDB?useUnicode=true&characterEncoding=GB2312"; 
        private static final String username = "zhuopeng" ;
        private static final String password = "zhuopeng" ;
        private static Connection conn = null ;
        //静态代码块加载驱动
        static 
        {
            try
            {
                Class.forName("com.mysql.jdbc.Driver"); 
            } catch (Exception e)
            {
                e.printStackTrace();
                System.out.println("驱动");
            }
            
        }
        //单例模式 
        public static Connection getConnection() throws Exception
        {
            if(conn == null)
            {
                conn = DriverManager.getConnection(url,username,password);
            }
            return conn ;
        }
        
    }

    index.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@ page import = "dao.ItemsDAO"  %>
    <%@ page import = "java.util.*" %>
    <%@ page import = "entity.Items"   %>
    
    <!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>商品</title>
    </head>
    <body>
        <h1>商品展示</h1>
        <hr> 
        <center>
            <table width="550" height = "60 "cellpadding = "0" celpacing = "0" border = "0">
            <tr>
                <td>
                    <%
                        ItemsDAO  itemsDao = new  ItemsDAO() ;
                        ArrayList<Items> list = itemsDao.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 ="E:javawebTestMyfirstWebappWebContentWEB-INFimages<%=item.getPicture() %>"   width = "100" heigh="100"/></a>
                            </dt>
                            <dd class = "dd_name" > <%=item.getName() %> </dd>
                            <dd class = "dd_city" > <%=item.getCity() %>  &nbsp; &nbsp; 价格:<%=item.getPricce() %></dd>
                    
                        </dl>                    
                    </div>
                    <!-- 循环结束哦 -->
                    <%
                            }
                            
                        }        
                    %>
                </td>
            </tr>        
            </table>    
        </center>    
    </body>
    </html>

    details.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@ page import = "dao.ItemsDAO"  %>
    <%@ page import = "java.util.*" %>
    <%@ page import = "entity.Items"   %>
    <!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>
        <%
            ItemsDAO itemDao = new ItemsDAO () ;
            Items item = itemDao.getItemsById(Integer.parseInt(request.getParameter("id")));
            if(item != null)
            {
        %>
            <img src ="E:javawebTestMyfirstWebappWebContentWEB-INFimages<%=item.getPicture() %>"  width = "400" heigh="400"/><br>
            产地:<%=item.getCity() %><br>
            价格:<%=item.getPricce() %><br>
            
        <%
            }
        %>
        
        <%
            String list = "" ;
            Cookie [] cookies = request.getCookies() ;
            if(cookies !=null && cookies.length>0)
            for(Cookie c :cookies){
                if(c.getName() . equals("ListViewCookie"))
                {
                    list = c.getValue() ;
                }
            }
            
            list += request.getParameter("id")+"," ;
            String [] arr = list.split(",");
            if(arr!=null && arr.length>0 ){
                if(arr.length>=100)
                {
                    list = "" ;
                }
            }
            Cookie cookie = new Cookie("ListViewCookie" ,list) ;    //创建Cookie
            cookie.setMaxAge(8000);
            response.addCookie(cookie);                                //添加Cookie
        %>
        <hr>
        浏览过的商品:<br>
        <%
            ArrayList<Items> itemslist = itemDao.getViewList(list);
            if(itemslist!= null && itemslist.size()>0){
                for(Items i : itemslist){
            
        %>
            <img src ="E:javawebTestMyfirstWebappWebContentWEB-INFimages<%=i.getPicture() %>"  width = "200" heigh="200"/><br>
            产地:<%=i.getCity() %><br>
            价格:<%=i.getPricce() %><br>
        <%
                }
            }
        %>
    </body>
    </html>
  • 相关阅读:
    N个数求和
    求整数段和
    连续因子
    L1-005 考试座位号
    PTA Java tips(转载)
    个位数统计
    flink编译支持CDH6.2.0(hadoop3.0.0)
    hdfs/hbase 程序利用Kerberos认证超过ticket_lifetime期限后异常
    spring boot通过@Bean注解定义一个Controller
    【TypeScript】TypeScript 学习 2——接口
  • 原文地址:https://www.cnblogs.com/da-peng/p/5434179.html
Copyright © 2020-2023  润新知